Class Definition

Can anyone see whats wrong this class definition. The compiler is throwing out the following error:

/tmp/cccWQe0N.o: In function `Item::Item(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
qu7.cpp:(.text+0xc): undefined reference to `vtable for Item'
/tmp/cccWQe0N.o:(.rodata._ZTI8Painting[typeinfo for Painting]+0x8): undefined reference to `typeinfo for Item'
collect2: ld returned 1 exit status



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 class Item
{
private: 
	string sellername;
	int price;
public: 
	Item(string sellername1, int price1);
virtual void display();
friend class Painting;
};

Item::Item(string sellername1, int price1)
{
sellername = sellername1;
price = price1;
}
Basically its something to do with the friend class. I whipped out the friend class and moved private members to public and the same error was thrown out.

Here is the derived class definition:

1
2
3
4
5
6
7
8
class Painting : public Item{
	private:
	int medium;

	public:
	Painting(string sellername1, int price, int medium);
	virtual void display();
};


and the derived class constructor:

1
2
3
4
Painting::Painting(string sellername1, int price1, int medium1):Item(sellername1, price1)
{
	medium = medium1;
}
The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]/7.

Yeah so i defined virtual void display() and everything was cool.

Basically if you see undefined reference to vtable then define ur virtual classes unless they are pure virtual!!!
Last edited on
closed account (zb0S216C)
Are you providing a definition for "Item::display( )"? Also, if your class contains a virtual member-function, the chances are you need a virtual destructor as well.

Edit: I see you did eventually provide a definition for "Item::display( )". Also, pure-virtual functions have to have a definition.

Wazzak
Last edited on
Topic archived. No new replies allowed.