简体   繁体   中英

C++ unresolved external symbol

I have a class setup and from that class I am using inheritance.

In file ah

class a
{
public:
    virtual void print();
};

In file bh:

#include "a.h"
#include <iostream>
class b: public a
{
public:
    void print();
};

And in b.cpp

#include "a.h"
#include "b.h"
void b::print(){};

In the main file I am including both of these files:

#include "a.h"
#include "b.h"

Yet I get an unresolved symbol for the virtual function print. The file a.obj is listed as the file generating the error What am I doing wrong? If I move b.cpp into bh below the class definition it works fine.

You have an implementation for b::print but not for a::print. What would happen if instantiated an object of class a and called print() on it? ie

a o;
o.print();

b::print overrides a::print but you still need to have an implementation of a::print (unless you make it pure virtual).

to make print pure virtual in a, define it like this:

virtual void print() = 0;

When a class has pure virtual functions, you cannot instantiate objects of that class. You must derive from that class and provide an implementation of any pure virtual functions before you have a class that can actually be instantiated.

它可能是一个拼写错误,但是在b.cpp你已经在名为Add而不是b类的类中实现了print()函数。

我认为在类接口的末尾你需要一个半冒号

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM