C++ inventory

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

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

#1 sydewayzlocc  Icon User is offline

  • D.I.C Head

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

C++ inventory

Posted 23 January 2013 - 11:46 AM

https://compilr.com/...0/lab1/main.cpp

My project is at the above link. I cant get it to compile i get errors like invalid conversion and deprecated error messages. Im not quite sure where i made i need to change char * to const char * or maybe use std::string instead of string literals. Please help.
Is This A Good Question/Topic? 0
  • +

Replies To: C++ inventory

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: C++ inventory

Posted 23 January 2013 - 12:15 PM

Ok...here is what I would suggest, remove the char * completely and replace with String...as your calling function seems to be using strings for the same.

Regarding your strcmp error, you need to include the following header.
#include <string.h>


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 12:27 PM

Please post your actual code into this forum, don't use an external link.


Spoiler


In the following snippet:
    AddItem(inv,"helmet",5);

"helmet" is the constant string your compiler is complaining about. It expects a non-constant character string not the constant.

Have you considered using std::string instead of the C-strings? With the std::string you can do away with the pointers and dynamic memory you are currently using.

Jim

This post has been edited by jimblumberg: 23 January 2013 - 12:31 PM

Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 12:32 PM

View Postraghav.naganathan, on 23 January 2013 - 01:15 PM, said:

Ok...here is what I would suggest, remove the char * completely and replace with String...as your calling function seems to be using strings for the same.

Regarding your strcmp error, you need to include the following header.
#include <string.h>


regards,
Raghav

Actually that should be <cstring> remember this is a C++ program.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: C++ inventory

Posted 23 January 2013 - 12:36 PM

View Postjimblumberg, on 24 January 2013 - 01:02 AM, said:

Actually that should be <cstring> remember this is a C++ program.

Jim


Ah...thanks :) Surprised how that slipped from my mind :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 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 - 02:14 PM

So I am going to change all my pointer to character locations to strings? I will start in my inventory.cpp?

I included the <cstring> header and tried to build again and I am getting the undefined reference error inventory.cpp|| undefined reference to `item::GetName(char*) const'|, its giving me the same error for other functions as well.

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



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


Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,865
  • Joined: 19-February 09

Re: C++ inventory

Posted 23 January 2013 - 02:24 PM

Your main problem is probably item. If you change name to a string instead of a pointer it will likely reduce complexity and problems.
Was This Post Helpful? 0
  • +
  • -

#8 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 - 02:34 PM

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

#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <cstring>

using namespace std;

const int MAXCHAR = 101;
const int MAXWEIGHT = 100;

class item
{
private:
    string name;
    double  weight;
    int     count;

public:
    //constructors
    item();
    item(const 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



So I changed name to a string but for some reason I get an error that says string does not name a type
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 03:00 PM

What compiler are you using?

Please post the complete error message, exactly as it appears in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 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 - 03:09 PM

I believe its GCC. I am using linux.

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



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(const item &);
    void RemoveItem(char []);
    void PrintInventory() const;
};

//others functions
void lowerCase(char []);

#endif



item.cpp :
#include "item.h"
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
#pragma warning(disable:4996) //allow strcpy

item::item():name(NULL), weight(0), count(0)
{}

item::item(const char* name, double weight): name(NULL), weight(weight), count(count)
{
	SetName(name);
}

item::item(const item& object):name(NULL)
{
	SetWeight(object.weight);
}

item::~item()
{
	if(name)
		delete [] name;
}

const char* const item::GetName() const
{
	return name;
}

double item::GetWeight(void) const
{
	return weight;
}

void item::SetName(const char * mame)
{
	if(this->name)
		delete [] this->name;

	this->name = new char[strlen(mame)+1];
	strcpy(this->name, mame);
}

void item::SetWeight(double weight)
{
	this->weight = weight;
}

ostream& operator<<(ostream& out, const item& object)
{
	out << setw(20) << object.name
		<< setw(15) << object.weight;
	return out;
}

void item::SetCount(int newCount)
{
	count = newCount;
}

int item::GetCount() const
{
	return count;
}



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

#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <cstring>

const int MAXCHAR = 101;
const int MAXWEIGHT = 100;

class item
{
private:
    string   name;
    double  weight;
    int     count;

public:
    //constructors
    item();
    item(const 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



main.cpp :
#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(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;
}



Here is my error output from my IDE when trying to run main.cpp:
main.cpp||In function ‘void AddItem(inventory&, char*, double)’:|
main.cpp|16|error: no matching function for call to ‘item::item(char*&, double&)’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|16|note: candidates are:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp||In function ‘void AddItem(inventory&, char*, double)’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|16|error: no matching function for call to ‘item::item(char*&, double&)’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|16|note: candidates are:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|22|note: item::item(std::string*, double)|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|22|note: no known conversion for argument 1 from ‘char*’ to ‘std::string* {aka std::basic_string<char>*}’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|21|note: item::item()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|21|note: candidate expects 0 arguments, 2 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|12|note: item::item(const item&)|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|12|note: candidate expects 1 argument, 2 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp||In function ‘void doTest()’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|33|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|37|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|38|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|42|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|46|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|47|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|50|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|51|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|52|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|55|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/main.cpp|56|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
||=== Build finished: 8 errors, 11 warnings ===|
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 03:36 PM

If you are going to convert your C-string variable name to a std::string then you must properly scope the standard string class by either using the "using" clause or using the scope resolution operator::. You will also need to change the type of variable for name from char* to std::string in every function that will access this variable.
class item
{
private:
    string   name;  // need to properly scope the string class.
    double  weight;
    int     count;

public:
    //constructors
    item();
    item(const char [], double); // need to change to std::string

    //destructors
    ~item();

    //accessor functions
    void    GetName(char []) const; // Should change return type to std::string and eliminate the parameter.
    double  GetWeight() const;
    int     GetCount() const;

    //mutator functions
    void SetName(char []);  // Need to change to std::string.
    void SetWeight(double);
    void SetCount(int);
};

Also you need to be careful with your constructors and class member function parameters. Right now in several places you have named your parameters with the same name as your class member variables. This can cause problems because the parameters will hide the member variables. I recommed you never use the same names for parameters and class member variables.



Jim
Was This Post Helpful? 0
  • +
  • -

#12 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 - 05:30 PM

Ok, I have changed the character pointers to std::string but am still getting error messages. Also, I can't seem to locate which parameters/member variables have the same name.

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

#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <cstring>

const int MAXCHAR = 101;
const int MAXWEIGHT = 100;

class item
{
private:
    std::string   name;
    double  weight;
    int     count;

public:
    //constructors
    item();
    item(std::string, double);

    //destructors
    ~item();

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

    //mutator functions
    void SetName(std::string);
    void SetWeight(double);
    void SetCount(int);
};

#endif


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: C++ inventory

Posted 23 January 2013 - 06:06 PM

Quote

Ok, I have changed the character pointers to std::string but am still getting error messages.

Paste the complete error messages, exactly as they appear in your development environment.

From you prior posts:
item::item(const char* name, double weight): name(NULL), weight(weight), count(count)
{
    SetName(name);
}

Notice that you have a parameter named "name', you also have a member variable with this name.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 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 - 07:13 PM

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



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(const item &);
    void RemoveItem(char []);
    void PrintInventory() const;
};

//others functions
void lowerCase(char []);

#endif



item.cpp:

#include "item.h"
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
#pragma warning(disable:4996) //allow strcpy

item::item():name(NULL), weight(0), count(0)
{}

item::item(std::string name, double weight): name(NULL), weight(weight), count(count)
{
	SetName(name);
}

item::item(const item& object):name(NULL)
{
	SetWeight(object.weight);
}


const std::string& item::GetName() const
{
	return name;
}

double item::GetWeight(void) const
{
	return weight;
}

void item::SetName(std::string name)
{
	if(this->name)
		delete [] this->name;

	this->name = new char[strlen(mame)+1];
	strcpy(this->name, mame);
}

void item::SetWeight(double weight)
{
	this->weight = weight;
}

ostream& operator<<(ostream& out, const item& object)
{
	out << setw(20) << object.name
		<< setw(15) << object.weight;
	return out;
}

void item::SetCount(int newCount)
{
	count = newCount;
}

int item::GetCount() const
{
	return count;
}



item.h :

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

#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <cstring>

const int MAXCHAR = 101;
const int MAXWEIGHT = 100;

class item
{
private:
    std::string   name;
    double  weight;
    int     count;

public:
    //constructors
    item();
    item(std::string, double);
    item(const item&);



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

    //mutator functions
    void SetName(std::string);
    void SetWeight(double);
    void SetCount(int);
};

#endif


Was This Post Helpful? 0
  • +
  • -

#15 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 - 07:29 PM

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




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(const item &);
    void RemoveItem(char []);
    void PrintInventory() const;
};

//others functions
void lowerCase(char []);

#endif




item.cpp :
#include "item.h"
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
#pragma warning(disable:4996) //allow strcpy

item::item():name(NULL), weight(0), count(0)
{}

item::item(std::string name, double weight): name(NULL), weight(weight), count(count)
{
	SetName(name);
}

item::item(const item& object):name(NULL)
{
	SetWeight(object.weight);
}


const std::string& item::GetName() const
{
	return name;
}

double item::GetWeight(void) const
{
	return weight;
}

void item::SetName(std::string name)
{
	if(this->name)
		delete [] this->name;

	this->name = new char[strlen(mame)+1];
	strcpy(this->name, mame);
}

void item::SetWeight(double weight)
{
	this->weight = weight;
}

ostream& operator<<(ostream& out, const item& object)
{
	out << setw(20) << object.name
		<< setw(15) << object.weight;
	return out;
}

void item::SetCount(int newCount)
{
	count = newCount;
}

int item::GetCount() const
{
	return count;
}




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

#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <cstring>

const int MAXCHAR = 101;
const int MAXWEIGHT = 100;

class item
{
private:
    std::string   name;
    double  weight;
    int     count;

public:
    //constructors
    item();
    item(std::string, double);
    item(const item&);



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

    //mutator functions
    void SetName(std::string);
    void SetWeight(double);
    void SetCount(int);
};

#endif




main.cpp :

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




My errors after working a little are :
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|33|error: no matching function for call to ‘item::GetName(char [101])’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|33|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|45|error: no matching function for call to ‘item::GetName(char [101])’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|45|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp||In member function ‘void inventory::AddItem(const item&)’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|91|error: no matching function for call to ‘item::GetName(char [101]) const’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|91|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|119|error: no matching function for call to ‘item::GetName(char [101])’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|119|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|125|error: no matching function for call to ‘item::GetName(char [101])’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|125|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp||In member function ‘void inventory::RemoveItem(char*)’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|178|error: no matching function for call to ‘item::GetName(char [101])’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|178|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp||In member function ‘void inventory::PrintInventory() const’:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|233|error: no matching function for call to ‘item::GetName(char [101])’|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/inventory.cpp|233|note: candidate is:|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: const string& item::GetName()|
/media/8c51a03d-e51b-4391-bab9-90222b696438/School/CS260/lab1/item.h|28|note: candidate expects 0 arguments, 1 provided|
||=== Build finished: 28 errors, 0 warnings ===|

I have tried taking the arguments out of GetName and have been unsucessful at compiling.
Was This Post Helpful? 0
  • +
  • -

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