Thread: using a vector of user defined objects

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Question using a vector of user defined objects

    I'm trying to make a customized storage vector for my application but keep getting errors at my vector declaration. I searched around and found that it might be caused by lacking certain item functions (assignment and copy). Unfortunately those are having problems as well.


    Here's the header I am concerned with:

    Code:
    #include<string>
    #include<fstream>
    //I know, don't do this in a header, but at this point I am just trying to get it to work
    using namespace std;
    class item
    {
        public:
        /*
        This constructor accepts a string and creates an object with that name
        @parameter- inputName: The name of the of the item
        */
        item(string inputName);
    
        /*
        This function intializes a blank object
        */
        item();
        /*
        This function returns the name of the item
        @return: The name of the item
        */
        string getName();
        /*
        This function outputs the members of item
        @param stream: The stream the output is being written to
        @param inputItem:  The item that is being written to the stream
        @return: The outputstream
        */
        friend ostream &operator<<(ostream &stream, item inputItem);
        /*
        this function outputs the size of the name string followed by the name of the item to the stream given
        @param outputStream:  The stream to be written to
        */
        void save(ofstream &outputStream);
        /*
        This function inputs the size of the name vector and then loads the name vector
        @param inputStream: the file stream to be read from
        @return: Whether the load was successful or not
        */
        bool load(ifstream &inputStream);
    
    
        /*
        This function constructs an item with the contents of another item
        @param inputItem: The item to be copied from
        */
        item(item const& inputItem);
        
        /*
        This operator assigns the values of another item to itself and returns a pointer to itself
        @param inputItem:  The item the values are being taken from
        @return:  a pointer to this item
        */
        item& operator = (item &inputItem);
        private:
        string name;
    };
    
    
        /*
        This constructor accepts a string and creates an object with that name
        @parameter- inputName: The name of the of the item
        */
    item::item(string inputName)
    {
        name=inputName;
    }
    
        /*
        This function intializes a blank object
        */
        item::item()
    {
    }
    
        /*
        This function returns the name of the item
        @return: The name of the item
        */
    string item::getName()
    {
        return name;
    }
        /*
        This function outputs the members of item, first the length of the vector and then the vector name
        @param stream: The stream the output is being written to
        @param inputItem:  The item that is being written to the stream
        @return: The outputstream
        */
    ostream &operator<<(ostream &stream, item inputItem)
    {
        stream<<inputItem.name.length() << inputItem.name;
        return stream;
    }
    
     /*
        this function outputs the size of the name string followed by the name of the item to the stream given
        @param outputStream:  The stream to be written to
        */
    void item::save(ofstream &outputStream)
    {
        outputStream << *this;
    }
        /*
        This function inputs the size of the name vector and then loads the name vector
        @param inputStream: the file stream to be read from
        @return: Whether the load was successful or not
        */
        bool item::load(ifstream &inputStream)
        {
            int i;
            char ch;
            if(inputStream)
            {
                inputStream>>i;
            }
            else
            {
                return false;
            }
            for(int ii=0; ii<i && inputStream; ii++)
            {
                inputStream.get(ch);
                name.push_back(ch);
            }
            if(i!=name.length())
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        /*
        This function constructs an item with the contents of another item
        @param inputItem: The item to be copied from
        */
        item::item(item const& inputItem)
        {
        name=inputItem.getName();
        }
        
        /*
        This operator assigns the values of another item to itself and returns a pointer to itself
        @param inputItem:  The item the values are being taken from
        @return:  a pointer to this item
        */
        item& item::operator = (item &inputItem)
        {
        name=inputItem.getName();
        return this*;
        }
    
    
    
    
    
    //This is a menu made of items
    class itemMenu
    {
    public:
    /*
    This function loads all of the items in the file and saves them in the listOfItems
    @Param inputStream: The file stream that the items are to be read from
    */
    itemMenu(ifstream &inputStream);
    /*
    This loads an empty menu
    */
    itemMenu();
    /*
    This function adds an item to the listOfItems
    @param inputItem: the item to be added
    */
    void addItem(item inputItem);
    /*
    This function erases the item with the given name in the listOfItems
    @param itemName: The name of the item to be deleted
    */ 
    void deleteItem(string itemName);
    
    /*
    This function erases the last item on the menu
    */
    void deleteItem();
    
    /*
    This function couts all of the items in the listOfItems
    */
    void showItems();
    
    /*
    This function saves all of the items in the listOfItems
    @param outputStream: The file stream for the items to be saved to
    */
    void saveMenu(ofstream &outputStream);
    
    /*
    This function returns a copy of an item in the menu and a blank item if the index is wrong
    @param inputIndex:  The index of the item to be accessed
    */
    item getItem(int inputIndex);
    private:
    vector<item> listOfItems;
    
    };
    
    
    
    /*
    This function loads all of the items in the file and saves them in the listOfItems
    @Param inputStream: The file stream that the items are to be read from
    */
    itemMenu::itemMenu(ifstream &inputStream)
    {
    listOfItems.push_back();
    while(listOfItems[listOfItems.size-1].load(inputStream))
    {
    listOfItems.push_back();
    }
    listOfItems.pop_back();
    }
    /*
    This loads an empty menu
    */
    itemMenu::itemMenu()
    {
    }
    /*
    This function adds an item to the listOfItems
    @param inputItem: the item to be added
    */
    void itemMenu::addItem(item inputItem)
    {
    listOfItems.push_back(inputItem.getName());
    }
    /*
    This function erases the item with the given name in the listOfItems
    @param itemName: The name of the item to be deleted
    */ 
    void itemMenu::deleteItem(string itemName)
    {
    for(int i=0; i<listOfItems.size(); i++)
    {
    if(listOfItems[i].getName()==itemName)
    {
    listOfItems.erase(listOfItems.begin()+i-1);
    }
    }
    }
    
    /*
    This function erases the last item on the menu
    */
    void itemMenu::deleteItem()
    {
    listOfItems.pop_back();
    }
    
    /*
    This function couts all of the items in the listOfItems
    */
    void itemMenu::showItems()
    {
    for(int i=0; i<listOfItems.size(); i++)
    {
    cout << listOfItems[i];
    }
    }
    
    /*
    This function saves all of the items in the listOfItems
    @param outputStream: The file stream for the items to be saved to
    */
    void itemMenu::saveMenu(ofstream &outputStream)
    {
    for(int i=0; i<listOfItems.size(); i++)
    {
    listOfItems[i].save(outputStream);
    }
    }
    
    /*
    This function returns a copy of an item in the menu and a blank item if the index is wrong
    @param inputIndex:  The index of the item to be accessed
    */
    item itemMenu::getItem(int inputIndex)
    {
    if(inputIndex<listOfItems.size())
    {
    return listOfItems.at(inputIndex);
    }
    else
    {
    item noName();
    return noName;
    }
    }
    Here are the errors I get:
    In file included from main.cpp:4:
    /home/hewhosurvives/c++/order/item.h: In copy constructor ‘item::item(const item&)’:
    /home/hewhosurvives/c++/order/item.h:143: error: passing ‘const item’ as ‘this’ argument of ‘std::string item::getName()’ discards qualifiers
    /home/hewhosurvives/c++/order/item.h: In member function ‘item& item:perator=(item&)’:
    /home/hewhosurvives/c++/order/item.h:154: error: expected primary-expression before ‘;’ token
    /home/hewhosurvives/c++/order/item.h: At global scope:
    /home/hewhosurvives/c++/order/item.h:207: error: ISO C++ forbids declaration of ‘vector’ with no type
    /home/hewhosurvives/c++/order/item.h:207: error: expected ‘;’ before ‘<’ token
    /home/hewhosurvives/c++/order/item.h: In constructor ‘itemMenu::itemMenu(std::ifstream&)’:
    /home/hewhosurvives/c++/order/item.h:219: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h: In member function ‘void itemMenu::addItem(item)’:
    /home/hewhosurvives/c++/order/item.h:238: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h: In member function ‘void itemMenu::deleteItem(std::string)’:
    /home/hewhosurvives/c++/order/item.h:246: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h: In member function ‘void itemMenu::deleteItem()’:
    /home/hewhosurvives/c++/order/item.h:260: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h: In member function ‘void itemMenu::showItems()’:
    /home/hewhosurvives/c++/order/item.h:268: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h: In member function ‘void itemMenu::saveMenu(std:fstream&)’:
    /home/hewhosurvives/c++/order/item.h:280: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h: In member function ‘item itemMenu::getItem(int)’:
    /home/hewhosurvives/c++/order/item.h:292: error: ‘listOfItems’ was not declared in this scope
    /home/hewhosurvives/c++/order/item.h:299: error: conversion from ‘item (*)()’ to non-scalar type ‘item’ requested
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:17: error: ‘orderMenu’ was not declared in this scope
    main.cpp:17: error: expected `;' before ‘menu’
    main.cpp:18: error: ‘menu’ was not declared in this scope

    Any ideas?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that a function tries to call a non-const function on a const object.
    It's in the item(const item&) (copy constructor). All functions that does not modify the state of the object should be const. So getName should be const since it does not modify the object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The first errors are related to inconsistency in using the const keyword. In particular, getName should be a const method:
    Code:
    string getName() const;
    Otherwise you can't call it with a constant object (as you have in the copy constructor).

    Another thing is operator=, which should also take a const reference (not modify the right-hand value). Also look at what and how you are returning.

    (Further, if your copy constructor/operator= don't do anything else than memberwise assignment, you shouldn't bother implementing them at all - the compiler will produce ones that do exactly that on its own.)

    Then a lot of errors might be caused by not including <vector>.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Thanks, the vector declaration fixed most of the errors. You just fixed something that caused me a lot of frustration.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined Variable name
    By mat429 in forum C Programming
    Replies: 4
    Last Post: 08-28-2008, 08:07 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Help! User defined functions
    By AdrenalinFlow in forum C Programming
    Replies: 3
    Last Post: 02-22-2003, 07:36 PM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM