Question

This question is possibly a duplicate, but I am not sure because my program is in a single file.

// my-program.cpp

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

class B : public A
{
public:
  void foo() {}
};

int main()
{
  B myB;
}

Then I type g++ my-program.cpp into the terminal, and the compiler gives me this warning:

Undefined symbols for architecture x86_64: "typeinfo for A", referenced from: typeinfo for Bin cce8BmNY.o "vtable for A", referenced from: A::A() in cce8BmNY.o NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

Was it helpful?

Solution

It says there is no definition because there is no definition. You've declared A::foo(), but not defined it.

Perhaps you want it to be pure virtual (making the base class A abstract):

virtual void foo() = 0;

In this case, it doesn't need a definition, since it will always be overridden in any class that can be instantiated.

Or perhaps you want to be able to instantiate A directly, in which case it will need a definition.

OTHER TIPS

If you want to have a function with no definition you should write virtual void foo() = 0;.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top