为什么链接器看不到我的(明确定义的)外部?
Why can't linker see my (definitely defined) externals?
我有一个带有标头和.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
相关文章:
- 具有外部"c"和程序集的未定义函数
- C++编程从外部文本文件定义数组大小
- 良好做法:如何定义用于编译的外部库的路径
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名
- 模板类外部的长定义的替代项
- 使用带有 CMake 和 Conan 的外部库的未定义引用
- 可视化C++外部定义内联函数
- 具有外部定义的默认特殊成员函数
- C++ 为什么在定义的编译和链接之前引用外部实例的程序
- 删除 PCL 可视化工具中定义的 3D 框外部的点
- 在成员函数外部封闭类的定义中需要默认成员初始值设定项
- 为什么在类外部(但在头文件中)定义的类成员函数必须内联?
- 如何在命名空间中声明外部全局,然后定义它?
- 使用静态变量未解析的外部符号/未定义的引用时出错
- 自定义对象构造函数在循环外部循环
- 我可以有一个从外部不可见但未在标头中定义的静态友元函数吗?
- 创建类时已定义的未解析外部符号
- 定义外部时未解析的外部符号"struct API Api"
- 为将来的'unset'变量定义外部变量是否是一种好的做法?
- 在同一头文件中定义外部变量