Error ' Has no member...'??
Feb 18, 2013 at 10:30pm UTC
Please someone help me get rid of these errors!
here is my container code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef CONTAINER_H
#define CONTAINER_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "item.h"
using namespace std;
class Container
{
public :
Container();
void add_item(Item item);
void numberofitems();
void displayitems();
void removeitems(int id);
private :
string _items[100];
int _count;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "container.h"
Container::Container()
{
_count=0;
}
void Container::add_item(Item item)
{
_items[_count] = item;
_count++;
}
void Container::displayitems()
{
for (int i=0; i < _count; i++)
{
cout<<_items[i].toString()<<endl;
}
}
void Container::removeitems(int id)
{
for (int i=0; i < _count; i++)
{
if (id==_items[i].getId())
{
_items[i] = _items[i+1];
_count--;
}
}
}
void Container::numberofitems()
{
cout<<_count<<endl;
}
my item code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef ITEMH
#define ITEMH
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
class Item
{
public :
Item();
Item( int item_id, string desc, double price );
int getId();
string getDescription();
double getPrice();
string toString();
private :
int _item_id;
string _desc;
double _price;
};
#endif
and my main code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream>
#include <string>
#include "auto.h"
#include "item.h"
#include "container.h"
using namespace std;
int main()
{
Container c; //Container <Auto> c;
Item i( 1567, "White bread" , .99 );
cout << i.toString() << endl;
Item i1( 1568, "Wheat bread" , 1.99 );
cout << i1.toString() << endl;
Item i2( 1569, "Italian bread" , 2.99 );
cout << i2.toString() << endl;
c.add_item(i);
c.add_item(i1);
c.add_item(i2);
c.displayitems();
system("PAUSE" );
return 0;
}
The errors are on the container.cpp code when i refer to the item methos 'toString' and 'getId'. Please help
Feb 18, 2013 at 10:35pm UTC
"
std::string " doesn't have "
toString( ) " and "
getID " as members. It seems as though you're trying to access the member functions of "
::Item ". If that's the case, then you should replace "
Container::_item "'s type with "
::Item ". For example:
1 2 3 4 5 6 7 8 9 10
class Item
{
// ...
};
class Container
{
private :
::Item MItems_[...];
};
Wazzak
Feb 18, 2013 at 10:36pm UTC
Do you have a function for them though? and string toString should be like char toString or something like that. This post is 143 letters long!
Feb 18, 2013 at 10:47pm UTC
Framework you were right...
1 2 3 4 5
class Container
{
private :
Item _item[100] // change string to 'Item' in order to access the functions in the Item.h file
}
Topic archived. No new replies allowed.