gpt4 book ai didi

c++ - Eclipse C++ 项目未生成 : Constructor Destructor Issue

转载 作者:搜寻专家 更新时间:2023-10-31 01:54:13 27 4
gpt4 key购买 nike

我在一个头文件中有所有的类定义:ModelModule.h。我在下面提供了该文件的示例代码,其中我给出了 2 个类及其成员函数的声明:

#pragma once

#if !defined( MODELMODULE_H )
#define MODELMODULE_H


//Required header files

class CModelModule;
class COrdProbitMM;

class CModelModule
// virtual base class for all types of modeling modules
{
friend class CSimCoordinator;
friend class CHouseholdCoordinator;
friend class CGenericHousehold;

public:
CModelModule(void);
~CModelModule(void);

protected:
std::string m_Label;
std::vector<int> m_AvailEndAttr;
void GetVarValues(std::vector<int>&, std::vector<double> &);


public:


virtual void Configure(void){};
virtual void loadXmlString(xmlNodePtr pXmlNode, xmlDocPtr pXmlDoc, xmlChar * con);
virtual void SaveXml(std::ofstream& fout){};

double mrand(void);
double UniformRand (); // returns a U[0,1] random number
double StdNormalRand (); // returns a N(0,1) random number
};

class COrdProbitMM : public CModelModule
// Class represent the ordered-probit models
{
friend class CSimCoordinator;
friend class CHouseholdCoordinator;
friend class CMMRunner;

public:
COrdProbitMM(CSimCoordinator& simcord, std::string& sLabel);
COrdProbitMM(CSimCoordinator& simcord, std::string& sLabel, int nAlts);
~COrdProbitMM(void);

private:

int m_Max_nAlts;
std::vector<double> m_Thresholds;

public:
void Configure(void);
void copyConfigure(COrdProbitMM* that);

int Run(CHouseholdObject*);
int Run(CPersonObject*);


void loadXmlString(xmlNodePtr pConfNode, xmlDocPtr pXmlDoc, xmlChar* con);

private:
int Run(void);
};

现在函数定义已经在 .cpp 文件中给出:ModelModule.cpp。注:已包含头文件

#include "ModelModule.h"
//Other header files

//Code for all the other functions defined here

//Given below are the code for how the constructors and destructors are defined

COrdProbitMM::~COrdProbitMM(void)
{
}

CModelModule::CModelModule(void)
{
}

CModelModule::~CModelModule(void)
{
}

我已经删除了所有语法错误的代码。但是,当我构建代码时,出现错误 make: *[ProjectName] Error1。在检查控制台时,我发现显示以下内容:

Building target: Project Name
Invoking: GCC C++ Linker
g++ -o "XYZ" ./src/XYZ.o ./src/DataCache\ -\ Copy.o ./src/DataCache.o ./src/DataCoordinator.o ./src/DataObject.o ./src/HouseholdCoordinator.o ./src/
LinearEquation.o ./src/MMRunner.o ./src/MainFrm.o ./src/ModelModule.o ./src/SimCoordinator.o ./src/main.o -lxml2 -lsqlite3

./src/ModelModule.o: In function `CModelModule::CModelModule()':
ModelModule.cpp:(.text._ZN12CModelModuleC2Ev[CModelModule::CModelModule()]+0xd): undefined reference to `vtable for CModelModule'
./src/ModelModule.o: In function `CModelModule::~CModelModule()':
ModelModule.cpp:(.text._ZN12CModelModuleD2Ev[CModelModule::~CModelModule()]+0xd): undefined reference to `vtable for CModelModule'

./src/ModelModule.o:(.rodata._ZTI12COrdProbitMM[typeinfo for COrdProbitMM]+0x8): undefined reference to `typeinfo for CModelModule'

collect2: ld returned 1 exit status
make: *** [Project Name] Error 1

**** Build Finished ****

我检查了这个论坛的 vtable 错误,有人提到问题是当我们声明一个构造函数/析构函数但从未定义它时。但在本例中这似乎不是问题,因为它已在 ModelModule.cpp 中明确完成。似乎这里发生了一些非常基本的事情,正在引起我的注意。

  • 我缺少什么?
  • 你能告诉我虚函数是什么以及它是如何导致错误的吗?
  • 它是否以某种方式与构造函数和析构函数联系起来?

最佳答案

根本原因:
你得到这个错误是因为 C++ 标准要求一个类的所有虚方法,除了纯虚方法必须有一个定义[#1] .

解决方案:
为所有 virtual 提供定义方法或使它们纯净virtual .

解释:
gcc 在这种情况下生成的错误充其量只是误导。这是一个sample program这证明了您遇到的问题:

class MyClass
{
public:
virtual void doSomething() { }
virtual void doSomethingMore();
};

int main()
{
MyClass obj;
obj.doSomething();
obj.doSomethingMore();
return 0;
}

编译信息:

/home/4VqWl0/ccMjLi2V.o: In function main':<br/>
prog.cpp:(.text+0x19): undefined reference to
vtable for MyClass.<br/>
prog.cpp:(.text+0x1e): undefined reference to
MyClass::doSomethingMore()'
collect2: ld returned 1 exit status

如您所见,GCC 因报告此类特定问题的错误而臭名昭著。

它是否以某种方式与构造函数和析构函数联系起来?

gcc faq 也记录了它:

The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined, but does not require any diagnostic for violations of this rule [class.virtual]/8. Based on this assumption, GCC will only emit the implicitly defined constructors, the assignment operator, the destructor and the virtual table of a class in the translation unit that defines its first such non-inline method.

Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols. Unfortunately, in order to improve this error message, it might be necessary to change the linker, and this can't always be done.

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]/7.

好读:

What does it mean that the "virtual table" is an unresolved external?


[#1]C++03 标准:10.3 虚函数 [class.virtual]

A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).

关于c++ - Eclipse C++ 项目未生成 : Constructor Destructor Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10006711/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com