Writings Photos Code Contact Resume Me
Help! What's wrong with this C++ code ?
Submitted by msameer on Mon, 06/03/2006 - 4:18pm

Why won't this shit compile ? What's wrong ? I don't get it!

#include <vector>
#include <iostream>

using namespace std;

class foo {
public:
   virtual void write();
};

class fubar : public foo {
public:
  void write() { cout << __PRETTY_FUNCTION__ << endl; }
};

class fubar2 : public foo {
public:
  void write() { cout << __PRETTY_FUNCTION__ << endl; }
};

int main()
{
  vector<foo> v;
  fubar one;
  v.push_back(one);
}

g++ -o foo foo.cc
/tmp/cchSMAqr.o:(.gnu.linkonce.r._ZTI5fubar[typeinfo for fubar]+0x8): undefined reference to `typeinfo for foo'
/tmp/cchSMAqr.o: In function `foo::foo()':foo.cc:(.gnu.linkonce.t._ZN3fooC2Ev[foo::foo()]+0x4): undefined reference to `vtable for foo'
/tmp/cchSMAqr.o: In function `foo::foo(foo const&)':foo.cc:(.gnu.linkonce.t._ZN3fooC1ERKS_[foo::foo(foo const&)]+0x4): undefined reference to `vtable for foo'
/tmp/cchSMAqr.o: In function `foo::~foo()':foo.cc:(.gnu.linkonce.t._ZN3fooD1Ev[foo::~foo()]+0x4): undefined reference to `vtable for foo'
collect2: ld returned 1 exit status

Syndicate content  digg  bookmark

Submitted by MohammedGamal (not verified) on Mon, 06/03/2006 - 10:12pm.

You just need to add braces after the write() function in the foo class, the correct code is:
class foo {
public:
virtual void write() {};
};
You can write whatever you want in the function body or leave it empty.

Submitted by Ayman Hourieh (not verified) on Tue, 07/03/2006 - 11:09am.

Either that, or mark it as a pure abstract function by adding "= 0":
virtual void write() = 0;

But in this case, you won't be able to create instances of the base class, so use pointers in the vector instead.

Submitted by msameer on Tue, 07/03/2006 - 12:11pm.

If I do:

class foo {
public:
virtual void write() {}
};     

Then it'll be called instead of fubar::write()

I thought that the whole point of polymorphism is that whenever I call foo::write() fubar::write() will be called instead, Looks like I'm missing something.

Submitted by MohammedGamal (not verified) on Tue, 07/03/2006 - 4:47pm.

You are right, this:

int main()
{
  vector<foo> v;
  fubar one;
  v.push_back(one);
}

will cause the "one" object to be type casted as an object of its super/parent class (which is "foo"). In order to enjoy polymorphism you can do this:

int main()
{
  vector<foo*> v;
  fubar one;
  v.push_back(&one);
}

this'll cause foobar::write() to be called instead.

Submitted by msameer on Tue, 07/03/2006 - 10:33pm.

Gah!

I hate pointers but nevermind ;-)

Thanks!

Submitted by Green Data (not verified) on Wed, 08/03/2006 - 7:10pm.

I hate C++, C rulezzzzzz

Submitted by MohammedGamal (not verified) on Wed, 08/03/2006 - 7:29pm.


I hate pointers but nevermind ;-)

Who doesn't! ;-)

Submitted by msameer on Wed, 08/03/2006 - 8:24pm.

No way, I used to say that before, What do you hate about C++ ?

Submitted by Green Data (not verified) on Fri, 17/03/2006 - 8:14pm.

Pointer are not that bad, ok most of the modern languages like perl, python, java don't have pointers ... but some stuff can be done much easier with pointers than without them. Imagine the case of a sniffer (tcpdump, or snort) where you need to get the different fields from different TCP/IP headers.

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <dd> <dl> <dt> <i> <s> <li> <ol> <u> <ul> <br> <br />
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Lines and paragraphs break automatically.
  • You may write mixed Arabic and English freely, line direction will be computed automaticaly

More information about formatting options