문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top