#include <iostream>
#include "item.h"
#include "inventory.h"
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
using namespace std;
void AddItem(const inventory& inv,char* name,double weight)
{
cout << "Adding " << name << " with a weight of " << weight << "." << endl;
inv.AddItem(item(name,weight));
}
void RemoveItem(inventory& inv,char* name)
{
cout << "Removing " << name << "." << endl;
inv.RemoveItem(name);
}
void doTest()
{
inventory inv;
// Make sure printing an empty inventory works
inv.PrintInventory();
// Make sure adding the first one works
AddItem(inv,"helmet",5);
inv.PrintInventory();
// Add some more items
AddItem(inv,"braclet of power",1);
AddItem(inv,"red potion",2);
inv.PrintInventory();
// Add some duplicates
AddItem(inv,"braclet of power",1);
inv.PrintInventory();
// Add some heavy stuff
AddItem(inv,"bag of gold coins",50);
AddItem(inv,"bag of gold coins",50);
// Now some removes
RemoveItem(inv,"short sword");
RemoveItem(inv,"helmet");
RemoveItem(inv,"braclet of power");
inv.PrintInventory();
RemoveItem(inv,"braclet of power");
RemoveItem(inv,"red potion");
inv.PrintInventory();
}
int main() {
doTest();
#ifdef _WIN32
if (_CrtDumpMemoryLeaks()) {
cout << "Memory leaks!" << endl;
}
#endif
return 0;
}
item.h :
#ifndef ITEM_H
#define ITEM_H
#include <cstring>
#include <string>
const int MAXCHAR = 101;
const int MAXWEIGHT = 100;
class item
{
private:
char *name;
double weight;
int count;
public:
//constructors
item();
item(char [], double);
//destructors
~item();
item.cpp
#include "item.h"
#include <iostream>
#include <cstring>
int main(){}
using namespace std;
//private data members
//char *name;
//double weight;
//int count;
//default constructor
item::item()
{
name = new char[MAXCHAR];
strcpy(name, " ");
weight = 0;
}
//constructor with intial parameters
item::item(char initName[], double initWeight){
name = new char[strlen(initName) + 1];
strcpy(name, initName);
weight = initWeight;
}
//Destructor
item::~item()
{
if(name)
{
delete [] name;
}
name = NULL;
}
void item::GetName(char *returnName) const
{
strcpy(returnName, name);
}
double item::GetWeight() const
{
return weight;
}
int item::GetCount() const
{
return count;
}
void item::SetName(char newName[])
{
if(name)
{
delete [] name;
name = NULL;
}
name = new char[strlen(newName) + 1];
strcpy(name, newName);
}
void item::SetWeight(double newWeight)
{
weight = newWeight;
}
void item::SetCount(int newCount)
{
count = newCount;
}
inventory.h
//class: inventory
//data members: head, tail
#ifndef INVENTORY_H
#define INVENTORY_H
#include "item.h"
const int CAP = 2;
const int GROWTH = 2;
struct Node
{
item data;
Node *next;
};
class inventory
{
private:
//data members
Node *head;
Node *tail;
public:
//constructors
inventory();
//copy constructor
inventory(const inventory &);
//destructor
~inventory();
//database functions
void AddItem(char* name, double weight);;
void Add(const item & anItem);
void RemoveItem(char []);
void PrintInventory() const;
};
//others functions
void lowerCase(char []);
#endif
inventory.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include "inventory.h"
using namespace std;
//private data members
//Node *head;
//Node *tail;
//default constructor
inventory::inventory()
{
head = NULL;
tail = NULL;
}
//copy constructor
inventory::inventory(const inventory& anInventory)
{
char tempName[MAXCHAR];
//anInventory is empty
if(anInventory.head == NULL)
{
head = NULL;
}
else
{
//copy the first node
head = new Node;
anInventory.head->data.GetName(tempName);
head->data.SetName(tempName);
head->data.SetWeight(anInventory.head->data.GetWeight());
head->data.SetCount(anInventory.head->data.GetCount());
//copy the rest of the linked list
Node *currSrc = anInventory.head->next;
Node *currDest = head;
while(currSrc)
{
currDest->next = new Node;
currDest = currDest->next;
currSrc->data.GetName(tempName);
currDest->data.SetName(tempName);
currDest->data.SetWeight(currSrc->data.GetWeight());
currDest->data.SetCount(currSrc->data.GetCount());
currSrc = currSrc->next;
}
currDest->next = NULL;
}
}
//destructor
inventory::~inventory()
{
Node *curr;
if(head)
{
curr = head;
}
while(curr)
{
curr = curr->next;
delete head;
head = curr;
}
head = NULL;
curr = NULL;
}
void inventory::Add(const item & anItem)
{
char tempName[MAXCHAR], newNodeName[MAXCHAR];
Node *newNode, *curr, *prev;
newNode = new Node;
anItem.GetName(newNodeName);
newNode->data.SetName(newNodeName);
lowerCase(newNodeName);
newNode->data.SetWeight(anItem.GetWeight());
newNode->next = NULL;
//check max weight
Node *tempcurr;
double totalWeight = 0;
for(tempcurr = head; tempcurr; tempcurr = tempcurr->next)
{
totalWeight += tempcurr->data.GetWeight();
}
if(newNode->data.GetWeight() <= MAXWEIGHT)
{
if(!head)
{
head = newNode;
tail = newNode;
newNode->data.SetCount(1);
}
else
{
//reset previous to NULL
prev = NULL;
curr = head;
//check to see where to insert
curr->data.GetName(tempName);
lowerCase(tempName);
while(curr && (strcmp(tempName, newNodeName) < 0))
{
prev = curr;
curr = curr->next;
curr->data.GetName(tempName);
lowerCase(tempName);
}
//increase weight and count
if(strcmp(tempName, newNodeName) == 0)
{
curr->data.SetWeight(curr->data.GetWeight()+newNode->data.GetWeight());
curr->data.SetCount(curr->data.GetCount()+1);
}
//add a new node
else
{
newNode->next = curr;
if(prev)
{
prev->next = newNode;
newNode->data.SetCount(1);
}
else
{
head = newNode;
newNode->data.SetCount(1);
}
if(!curr)
{
tail = newNode;
newNode->data.SetCount(1);
newNode->next = NULL;
}
}
}
}
cout << "You picked up a " << newNodeName << "." << endl;
}
void inventory::RemoveItem(char searchName[])
{
Node *curr, *prev;
char tempName[MAXCHAR];
bool found = false;
lowerCase(searchName);
curr = head;
prev = NULL;
while(curr)
{
curr->data.GetName(tempName);
lowerCase(tempName);
if(strcmp(tempName, searchName) == 0)
{
if(!head)
cout << "Your inventory is empty. Nothing to remove!" << endl;
else
{
if(prev)
prev->next = curr->next;
else
head = curr->next;
curr->next = NULL;
delete curr;
found = true;
}
}
prev = curr;
curr = curr->next;
}
if(found)
{
cout << "You dropped a " << searchName << " ." << endl;
}
if(!found)
{
cout << "You don't have a " << searchName << " in your inventory." << endl;
}
}
void inventory::PrintInventory() const
{
char tempName[MAXCHAR];
int totalItem = 0;
double totalWeight = 0;
cout << endl;
cout << "Current inventory:" << endl;
if(!head)
{
cout << "\t(no items)" << endl;
}
else
{
Node *curr;
for(curr = head; curr; curr = curr->next)
{
curr->data.GetName(tempName);
cout << "\t[" << curr->data.GetCount() << "] " << tempName << endl;
totalItem += curr->data.GetCount();
totalWeight += curr->data.GetWeight();
}
cout << "Total items: " << totalItem << endl;
cout << "Total weight: " << totalWeight << endl;
}
cout << endl;
}
void lowerCase(char word[])
{
int i = 0;
while(word[i] != '\0')
{
word[i] = tolower(word[i]);
i++;
}
}
Errors:
When compiling main.cpp :
main.cpp||In function ‘void AddItem(const inventory&, char*, double)’:| main.cpp|16|error: no matching function for call to ‘inventory::AddItem(item) const’| main.cpp|16|note: candidate is:| inventory.h|35|note: void inventory::AddItem(char*, double)| inventory.h|35|note: candidate expects 2 arguments, 1 provided| main.cpp||In function ‘void doTest()’:| main.cpp|33|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|37|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|38|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|42|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|46|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|47|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|50|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|51|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|52|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|55|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| main.cpp|56|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]| ||=== Build finished: 4 errors, 11 warnings ===|
When compiling item.cpp :
Compiling: /media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.cpp Linking console executable: /media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item Process terminated with status 0 (0 minutes, 8 seconds) 0 errors, 0 warnings Checking for existence: /media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item Executing: xterm -T '/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item' -e /usr/bin/cb_console_runner "/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item" (in /media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1) Process terminated with status 0 (0 minutes, 19 seconds)
when compiling inventory.cpp :
[code]
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o||In function `_start':|
(.text+0x18)||undefined reference to `main'|
inventory.o||In function `inventory::inventory(inventory const&)':|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.cpp|| undefined reference to `item::SetName(char*)'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::SetWeight(double)'|
inventory.cpp|| undefined reference to `item::GetCount() const'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.cpp|| undefined reference to `item::SetName(char*)'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::SetWeight(double)'|
inventory.cpp|| undefined reference to `item::GetCount() const'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.o||In function `inventory::Add(item const&)':|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.cpp|| undefined reference to `item::SetName(char*)'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::SetWeight(double)'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
inventory.cpp|| undefined reference to `item::SetWeight(double)'|
inventory.cpp|| undefined reference to `item::GetCount() const'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.cpp|| undefined reference to `item::SetCount(int)'|
inventory.o||In function `inventory::RemoveItem(char*)':|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.o||In function `inventory::PrintInventory() const':|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
inventory.cpp|| undefined reference to `item::GetCount() const'|
inventory.cpp|| undefined reference to `item::GetCount() const'|
inventory.cpp|| undefined reference to `item::GetWeight() const'|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.o||In function `Node::Node()':|
inventory.cpp:(.text._ZN4NodeC2Ev[_ZN4NodeC5Ev]+0xd)||undefined reference to `item::item()'|
inventory.o||In function `Node::~Node()':|
inventory.cpp:(.text._ZN4NodeD2Ev[_ZN4NodeD5Ev]+0xd)||undefined reference to `item::~item()'|
||=== Build finished: 37 errors, 0 warnings ===|
[/code
I don't even know which errors to attack first.

New Topic/Question
Reply




MultiQuote



|