简体   繁体   English

编译C ++代码时出现链接器错误

[英]Linker error while compiling C++ code

First of all, excuse me for my poor knowledge on C++. 首先,对不起我对C ++的了解。 I am a real Beginner! 我是一个真正的初学者!

I am trying to compile one C++ code on MS VS2005. 我正在尝试在MS VS2005上编译一个C ++代码。 I am getting a linker error as follows for below piece of code:- 我收到以下代码段的链接器错误:-

In one function(some class method) definition, it has code for memory allocation like: 在一个函数(某些类方法)的定义中,它具有用于内存分配的代码,例如:

CDecoderUnit *du = new CDecoderUnit(); //<<error is shown at this line 

This CDecoderUnit is a class defined in a header file as something like 这个CDecoderUnit是一个在头文件中定义的类,例如

class CDecoderUnit : public IPrepareDecoderUnit
{
   CDecoderUnit();
   ~CDecoderUnit();
...
...
other class definition.

..
..
}

The actual error is:- 实际错误是:

error LNK2019: unresolved external symbol "public: __thiscall CDecoderUnit::CDecoderUnit(void)" (??0CDecoderUnit@@QAE@XZ) referenced in function "private: long __thiscall CLSDEnc::CreateIPrepareDecoderUnit(struct IPrepareDecoderUnit * &)const " (?CreateIPrepareDecoderUnit@CLSDEnc@@ABEJAAPAUIPrepareDecoderUnit@@@Z) lsdenc.obj 错误LNK2019:未解析的外部符号“公共:__thiscall CDecoderUnit :: CDecoderUnit(void)”(?? 0CDecoderUnit @@ QAE @ XZ)在函数“私有:long __thiscall CLSDEnc :: CreateIPrepareDecoderUnit(struct IPrepareDecoderUnit *&)const”中引用”(? CreateIPrepareDecoderUnit @ CLSDEnc @@ ABEJAAPAUIPrepareDecoderUnit @@@ Z)lsdenc.obj

Can anyone point me to resolve this? 谁能指出我要解决的问题?

When you write CDecoderUnit(); 当您编写CDecoderUnit(); you are doing 2 things: 您正在做2件事:

  • Declaring a default constructor (not implementing it, just declaring it). 声明一个默认的构造函数(不实现它,只声明它)。
  • Causing the compiler not to generate a default constructor on its own. 导致编译器自行生成默认构造函数。

You need to either define the constructor if it needs to initialize anything, or remove the declaration which will let the compiler generate one itself. 如果需要初始化任何内容,则需要定义构造函数,或者删除使编译器自己生成的声明。 Note that if you do implement the constructor, you'll have to make it public if you want to create objects from outside the class. 请注意,如果要实现构造函数,则要从类外部创建对象,必须将其公开。

Possible problems: 可能的问题:

  • You didn't define the constructer in the .cpp 您没有在.cpp中定义构造函数
  • You didn't include the right header in your .cpp 您没有在.cpp中包含正确的标题
  • You forgot to make the constructor public (class methods are private by default) 您忘记了公开构造函数(默认情况下,类方法是私有的)

Try to: 尝试:

  • Rebuild the project 重建项目
  • Check that you include the right header in the .cpp (spelling! ;) ) 检查是否在.cpp中包含正确的标题(拼写!;))
  • Remember that your constructor should go like this: CDecoderUnit::CDecoderUnit(){} in the .cpp 请记住,您的构造函数应如下所示:.cpp中的CDecoderUnit :: CDecoderUnit(){}
  • Add public: to the top of your class definition in the .h (Remember to do private: for your privates) 在.h中,将public:添加到类定义的顶部(记住要进行private:针对您的private)
  • You forgot the ; 你忘了; after the class definition (Don't think this is it. The compiler ussually generates a different error for this) 在类定义之后(不要以为是这样。编译器通常会为此生成一个不同的错误)

To make your constructor public, declare the class like this: 为了使您的构造函数公开,请声明以下类:

class MyClass
{
public:
   //Public stuff goes here
   MyClass();
   void publicMethod();
private:
   //Private stuff goes here
};

Do you have the source file containing the method bodies for methods such as CDecoderUnit::CDecoderUnit() included in your project? 您的源文件中是否包含项目中包括的CDecoderUnit :: CDecoderUnit()之类的方法的方法主体?

If so, check the properties on that source file to make sure that it is actually being used - right-click in the Solution Explorer to choose properties and check the General property Excluded from Build is set to No. 如果是这样,请检查该源文件上的属性以确保它实际上已在使用-在解决方案资源管理器中右键单击以选择属性,然后将“ 从构建中排除的常规”属性设置为“否”。

A more subtle problem can arise with line endings. 行尾可能会出现更细微的问题。 The editor understands different line ending styles and will correctly show your file. 编辑器了解不同的行尾样式,并将正确显示您的文件。 The compiler only understands the Windows (CR LF) style. 编译器仅了解Windows(CR LF)样式。 If the first line is a // comment then the compiler sees the entire file as being commented out. 如果第一行是//注释,则编译器将整个文件视为已注释掉。 In this case, the fix is to save it as Windows style - see File - Advanced Save Options. 在这种情况下,解决方法是将其保存为Windows样式-请参见文件-高级保存选项。

Are these two things in two different dlls ? 这两个东西是否在两个不同的dll中? For example, if you have defined CDecoderUnit in one dll and creating an instance in another one. 例如,如果您在一个dll中定义了CDecoderUnit,并在另一个dll中创建了一个实例。 If that is the case, then you have to export the class from the first dll so as to create the object in the other one. 如果是这种情况,则必须从第一个dll导出类,以便在另一个dll中创建对象。

The default accessibility in a class is private. 类中的默认可访问性是私有的。 Declare your constructor like this: 像这样声明您的构造函数:

public: CDecoderUnit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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