C++ inventory

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

91 Replies - 2857 Views - Last Post: 28 January 2013 - 12:14 AM Rate Topic: -----

#16 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 08:15 PM

You need to change the C-strings into std::string in your inventory class as well.

Jim
Was This Post Helpful? 0
  • +
  • -

#17 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: C++ inventory

Posted 23 January 2013 - 08:28 PM

The prototype should agree with the function head - you're missing a const.

item.cpp

22      const std::string& item::GetName() const
23      {
24          return name;
25      }




item.h

28      const std::string& item::GetName();


Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 08:52 PM

I actually recommend that you don't return a reference to your string just return the string:
std::string item::GetName() const
{
    return name;
}



Jim
Was This Post Helpful? 1
  • +
  • -

#19 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 23 January 2013 - 10:58 PM

When working on the header on line 30 i get an error saying string does not name a type





    //accessor functions
    double  GetWeight() const;
    int     GetCount() const;
    std::string GetName() const;

    




Was This Post Helpful? 0
  • +
  • -

#20 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: C++ inventory

Posted 23 January 2013 - 11:53 PM

It is a strange error seems specific to your compiler, perhaps, a long shot, try swapping the string includes around.

#include <cstring>
#include <string>




With your orginal files, setting the function prototype and function head to the same seems to solve the problems, so you could try that if you wish.

item.h

//class: item
//data members: name, weight, count

#ifndef ITEM_H
#define ITEM_H

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();

    //accessor functions
    void    GetName(char *) const;
    double  GetWeight() const;
    int     GetCount() const;

    //mutator functions
    void SetName(char []);
    void SetWeight(double);
    void SetCount(int);
};

#endif




item.cpp

#include "itemCh.h"
#include <iostream>
#include <cstring>

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;
}

/************************************************************************************************************/
/* accessor functions                                                                                       */
/************************************************************************************************************/

/****************************************************************************************/
/* Name:            GetName()                                                           */
/* Description:     returns name from the object                                        */
/* Input:           none                                                                */
/* Output:          object's name                                                       */
/* Returns:         none                                                                */
/****************************************************************************************/
void item::GetName(char *returnName) const 
{
    strcpy(returnName, name);
}

/****************************************************************************************/
/* Name:            GetWeight()                                                         */
/* Description:     returns weight value from the object                                */
/* Input:           none                                                                */
/* Output:          object's weight value                                               */
/* Returns:         double                                                              */
/****************************************************************************************/
double item::GetWeight() const
{
    return weight;
}

/****************************************************************************************/
/* Name:            GetCount()                                                          */
/* Description:     returns count value from the object                                 */
/* Input:           none                                                                */
/* Output:          object's count value                                                */
/* Returns:         int                                                                 */
/****************************************************************************************/
int item::GetCount() const
{
    return count;
}

/************************************************************************************************************/
/* mutator functions                                                                                        */
/************************************************************************************************************/

/****************************************************************************************/
/* Name:            SetName()                                                           */
/* Description:     set name of object to new name                                      */
/* Input:           new name                                                            */
/* Output:          none                                                                */
/* Returns:         none                                                                */
/****************************************************************************************/
void item::SetName(char newName[])
{
    if(name)
    {
        delete [] name;
        name = NULL;
    }
    name = new char[strlen(newName) + 1];
    strcpy(name, newName);
}

/****************************************************************************************/
/* Name:            SetWeight()                                                         */
/* Description:     set weight to object                                                */
/* Input:           new weight                                                          */
/* Output:          none                                                                */
/* Returns:         none                                                                */
/****************************************************************************************/
void item::SetWeight(double newWeight)
{
    weight = newWeight;
}

/****************************************************************************************/
/* Name:            SetCount()                                                          */
/* Description:     set count to object                                                 */
/* Input:           new count                                                           */
/* Output:          none                                                                */
/* Returns:         none                                                                */
/****************************************************************************************/
void item::SetCount(int newCount)
{
    count = newCount;
}


Was This Post Helpful? 0
  • +
  • -

#21 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 24 January 2013 - 01:11 AM

Check it out I got it to compile without errors by doing this :


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;
}

/************************************************************************************************************/
/* accessor functions                                                                                       */
/************************************************************************************************************/

/****************************************************************************************/
    /* Name:            GetName()                                                           */
	/* Description:     returns name from the object                                        */
    /* Input:           none                                                                */
    /* Output:          object's name                                                       */
	/* Returns:         none                                                                */
/****************************************************************************************/
	void item::GetName(char *returnName) const
	{
	    strcpy(returnName, name);
	}

	/****************************************************************************************/
	/* Name:            GetWeight()                                                         */
	/* Description:     returns weight value from the object                                */
	/* Input:           none                                                                */
	/* Output:          object's weight value                                               */
	/* Returns:         double                                                              */
	/****************************************************************************************/
	double item::GetWeight() const
	{
	    return weight;
	}

	/****************************************************************************************/
	/* Name:            GetCount()                                                          */
	/* Description:     returns count value from the object                                 */
	/* Input:           none                                                                */
	/* Output:          object's count value                                                */
	/* Returns:         int                                                                 */
	/****************************************************************************************/
	int item::GetCount() const
	{
	    return count;
	}

	/************************************************************************************************************/
	/* mutator functions                                                                                        */
	/************************************************************************************************************/

	/****************************************************************************************/
	/* Name:            SetName()                                                           */
	/* Description:     set name of object to new name                                      */
	/* Input:           new name                                                            */
	/* Output:          none                                                                */
	/* Returns:         none                                                                */
/****************************************************************************************/
	void item::SetName(char newName[])
	{
	    if(name)
	    {
	        delete [] name;
	        name = NULL;
	    }
	    name = new char[strlen(newName) + 1];
	    strcpy(name, newName);
	}

/****************************************************************************************/
	/* Name:            SetWeight()                                                         */
	/* Description:     set weight to object                                                */
	/* Input:           new weight                                                          */
	/* Output:          none                                                                */
	/* Returns:         none                                                                */
/****************************************************************************************/
	void item::SetWeight(double newWeight)
	{
	    weight = newWeight;
	}

/****************************************************************************************/
	/* Name:            SetCount()                                                          */
	/* Description:     set count to object                                                 */
	/* Input:           new count                                                           */
	/* Output:          none                                                                */
	/* Returns:         none                                                                */
/****************************************************************************************/
	void item::SetCount(int newCount)
	{
	    count = newCount;
	}


Was This Post Helpful? 0
  • +
  • -

#22 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 24 January 2013 - 01:25 AM

inventory.h :
//class: inventory
	//data members: head, tail
	#include "item.h"
	#ifndef INVENTORY_H
	#define INVENTORY_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(inventory&, char*, double);
        void RemoveItem(char []);
	    void PrintInventory() const;
	};
	 
	//others functions
	void lowerCase(char []);
	 
	#endif



Im getting some similar errors in this header

inventory.cpp

#include "inventory.h"
#include <iostream>
#include <iomanip>
#include <cstring>

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;
	}

	/************************************************************************************************************/
	/* database functions                                                                                       */
	/************************************************************************************************************/

	/****************************************************************************************/
	/* Name:            AddItem()                                                           */
	/* Description:     add an item to the sorted database                                  */
	/* Input:           an item                                                             */
	/* Output:          none                                                                */
	/* Returns:         none                                                                */
	/****************************************************************************************/
	void inventory::AddItem(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;
	}

	/****************************************************************************************/
	/* Name:            RemoveItem()                                                        */
	/* Description:     remove the items from the database                                  */
	/* Input:           none                                                                */
	/* Output:          none                                                                */
	/* Returns:         none                                                                */
	/****************************************************************************************/
	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;
	    }
	}

	/****************************************************************************************/
	/* Name:            PrintInventory()                                                    */
	/* Description:     print the items in the database                                     */
	/* Input:           none                                                                */
	/* Output:          list of items                                                       */
	/* Returns:         none                                                                */
	/****************************************************************************************/
	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;
	}

	/************************************************************************************************************/
	/* others functions                                                                                         */
	/************************************************************************************************************/

	/****************************************************************************************/
	/* Name:            lowerCase()                                                         */
	/* Description:     convert case to lowercase                                           */
	/* Input:           char                                                                */
	/* Output:          lowercase char                                                      */
	/* Returns:         none                                                                */
	/****************************************************************************************/
	void lowerCase(char word[])
	{
	    int i = 0;
	    while(word[i] != '\0')
	    {
	        word[i] = tolower(word[i]);
	        i++;
	    }
	}




/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o||In function `_start':|
(.text+0x18)||undefined reference to `main'|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/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)'|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.o||In function `inventory::AddItem(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)'|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.o||In function `inventory::RemoveItem(char*)':|
inventory.cpp|| undefined reference to `item::GetName(char*) const'|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/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()'|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.o||In function `Node::~Node()':|
inventory.cpp:(.text._ZN4NodeD2Ev[_ZN4NodeD5Ev]+0xd)||undefined reference to `item::~item()'|
||=== Build finished: 37 errors, 0 warnings ===|


Was This Post Helpful? 0
  • +
  • -

#23 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 24 January 2013 - 01:47 AM

item.h :

//class: item
	//data members: name, weight, count

	#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();

	    //accessor functions
	    void    GetName(char *) const;
	    double  GetWeight() const;
	    int     GetCount() const;

	    //mutator functions
	    void SetName(char []);
	    void SetWeight(double);
	    void SetCount(int);
	};

	#endif



Was This Post Helpful? 0
  • +
  • -

#24 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: C++ inventory

Posted 24 January 2013 - 12:17 PM

Usually the undefined reference to error means the linker cannot find the function - sometimes a spelling mistake, unwritten function or file not linked in.

It looks as if item.o (from item.cpp) has not been linked to the program (nor main).


In inventory.h if you move the includes inside the guards then the compiler doesn't need to load item.h everytime.

03	    #include "item.h"
04	    #ifndef INVENTORY_H
05	    #define INVENTORY_H




In inventory.cpp I would move your includes such as inventory.h after the standard ones (convention mostly).

001	#include "inventory.h"
002	#include <iostream>
003	#include <iomanip>
004	#include <cstring>




inventory.h

34	        void AddItem(inventory&, char*, double);




and inventory.cpp don't match :

085	    void inventory::AddItem(const item & anItem)


Was This Post Helpful? 0
  • +
  • -

#25 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 24 January 2013 - 04:09 PM

I guess that I am unsure how to change the AddItem method from inventory.cpp :

void inventory::AddItem(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;
	    }












In order to meet the prototype from inventory.h
void AddItem(inventory&, char*, double);



Because each item, as indicated by the prototype, has to be stored in the inventory, has a name, and a weight that is of type double
Was This Post Helpful? 0
  • +
  • -

#26 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: C++ inventory

Posted 24 January 2013 - 05:10 PM

This doesn't make much sense.

  void AddItem(inventory&, char*, double);



You have an object of class inventory, so having a reference to another object is unnecessary. The data (item) is being added to the calling inventory object.


So you want to do :

  inventory my_inventory;
  char name[] = "goggles";
  double weight = 1;

  my_inventory.AddItem(name, weight);




You could have both functions

  void AddItem(const item & anItem);
  void AddItem(char* n, double w);



The first function doesn't need to do much just call the second.
Was This Post Helpful? 0
  • +
  • -

#27 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 24 January 2013 - 05:23 PM

I understand having both headers but where do I use this code and how will that work for the call in main.cpp?


inventory my_inventory;
char name[] = "goggles";
double weight = 1;

my_inventory.AddItem (name, weight);




from main.cpp :

void AddItem(const inventory& inv,char* name,double weight)
	{
	    cout << "Adding " << name << " with a weight of " << weight << "." << endl;
	    inv.AddItem(item(name,weight));
	}
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);


Was This Post Helpful? 0
  • +
  • -

#28 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: C++ inventory

Posted 24 January 2013 - 05:50 PM

Oh, sorry must have got confused somewhere.

With a small change or two you could add the AddItem function to your inventory class (only if you want) :

void inventory::AddItem(char*  name, 
                        double weight)
{
    cout << "Adding " << name 
         << " with a weight of " << weight 
         << "." << endl;
    AddItem(item(name,weight));
}




If that is the case maybe change the name of the other function.

void Add(char* name, double weight);




Then in main.cpp

13    // Make sure adding the first one works
14    inv.AddItem("helmet", 5);
15    inv.PrintInventory();




Have you solved the errors?

This post has been edited by #define: 24 January 2013 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#29 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 24 January 2013 - 06:09 PM

Put the whole definition for AddItem in the inventory class in the header? and then where do I change the name of the other function? in inventory.cpp?
can you show me more specifically?
Was This Post Helpful? 0
  • +
  • -

#30 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: C++ inventory

Posted 24 January 2013 - 06:33 PM

View Postsydewayzlocc, on 25 January 2013 - 03:09 AM, said:

Put the whole definition for AddItem in the inventory class in the header?


No, just put the head/prototype of the function in inventory.h, and put the function code in the implementation file inventory.cpp.


View Postsydewayzlocc, on 25 January 2013 - 03:09 AM, said:

and then where do I change the name of the other function? in inventory.cpp?


In both the header and the cpp file. The names and parameters of the functions need to be the same in the cpp file as it is in the header file.


View Postsydewayzlocc, on 25 January 2013 - 03:09 AM, said:

can you show me more specifically?


Have a go at changing inventory.h at post what you think it is.


:detective:
Your post #22 had

34      void AddItem(inventory&, char*, double);


in the class.

This post has been edited by #define: 24 January 2013 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »