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?