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

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

本文关键字:定义 外部 链接 看不到 我的 为什么      更新时间:2023-10-16

我有一个带有标头和.cpp文件的类。我在标头中声明我的函数,并像您一样在.cpp文件中定义它们。

页眉:

#pragma once
// my #includes 
class CDNAGenerator
{
private:
    // stuff 
public:
    CDNAGenerator(int, int);
    ~CDNAGenerator();
    void FilterMeasurementsForOutliers(std::vector<double>& measurement_values);
    // plenty more things
};

.CPP:

CDNAGenerator::CDNAGenerator( int genes, int chromosomes )
{
    // constructor code
}
void CDNAGenerator::FilterMeasurementsForOutliers(std::vector<double>& measurement_values)
{
    // function code
}

然后,从同一解决方案中的单独项目中,我引用了 .h 文件(但不是 .cpp - 这似乎会导致多个定义错误):

#include "..CalibrationToolDNAGenerator.h"

并调用这些函数:

CDNAGenerator* dnaGenerator = new CDNAGenerator(30, 0);
dnaGenerator->FilterMeasurementsForOutliers(values);
但是我得到了CDNAGenerator::CDNAGenerator

(int,int)和CDNAGenerator::FilterMeasurementsForOutliers(class std::vector> &)的未解决的外部错误

我以为我已经正确连接了所有内容,所以谁能建议为什么我会得到这个链接器错误?

将 CPP 文件添加到项目中

你用的是什么编译器?Gcc (mingw) 不支持#pragma once 使用代码防护来避免"多重定义"。

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