简体   繁体   English

链接器为什么看不到我的(定义明确的)外部对象?

[英]Why can't linker see my (definitely defined) externals?

I have a class with a header and a .cpp file. 我有一个带有标头和.cpp文件的类。 I declare my functions in the header, and define them in the .cpp file, as you would. 我在标头中声明了我的函数,然后按照需要在.cpp文件中定义了它们。

Header: 标头:

#pragma once

// my #includes 

class CDNAGenerator
{
private:
    // stuff 
public:
    CDNAGenerator(int, int);
    ~CDNAGenerator();

    void FilterMeasurementsForOutliers(std::vector<double>& measurement_values);

    // plenty more things

};

CPP: CPP:

CDNAGenerator::CDNAGenerator( int genes, int chromosomes )
{
    // constructor code
}

void CDNAGenerator::FilterMeasurementsForOutliers(std::vector<double>& measurement_values)
{
    // function code
}

Then, from a separate project in the same solution I reference the .h file (but not the .cpp - that seems to lead to multiple definition errors): 然后,在同一解决方案的一个单独项目中,我引用了.h文件(但没有引用.cpp,这似乎会导致多个定义错误):

#include "..\CalibrationTool\DNAGenerator.h"

And call those functions: 并调用这些函数:

CDNAGenerator* dnaGenerator = new CDNAGenerator(30, 0);
dnaGenerator->FilterMeasurementsForOutliers(values);

But I get unresolved external errors for CDNAGenerator::CDNAGenerator(int, int) and for CDNAGenerator::FilterMeasurementsForOutliers(class std::vector > &) 但是我遇到了CDNAGenerator :: CDNAGenerator(int,int)和CDNAGenerator :: FilterMeasurementsForOutliers(class std :: vector>&)的无法解决的外部错误

I thought that I had hooked everything up correctly, so can anyone suggest why I would be getting this linker error? 我以为我已正确连接了所有内容,所以谁能建议我为什么会出现此链接器错误?

将CPP文件添加到项目

What compiler are you using? 您正在使用什么编译器? Gcc (mingw) does'n supprort #pragma once Use code guards to avoid 'multiple definitions'. Gcc(mingw) #pragma once支持#pragma once使用代码防护来避免“多个定义”。

#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass {
...
}
#endif

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

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