Writings Photos Code Contact Resume
Help! What's wrong with this C++ code ?

You are here

Submitted by msameer on Mon, 06/03/2006 - 2: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

Comments

Submitted by MohammedGamal (not verified) on Mon, 06/03/2006 - 8: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 - 9: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 - 10:11am

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 - 2: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 - 8:33pm

Gah!

I hate pointers but nevermind ;-)

Thanks!

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

I hate C++, C rulezzzzzz

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

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

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


I hate pointers but nevermind ;-)

Who doesn't! ;-)

Submitted by Green Data (not verified) on Fri, 17/03/2006 - 6: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.

Add new comment

Subscribe to /  digg  bookmark