静态方法的Xcode中存在C++链接器错误
C++ Linker error in Xcode for a Static Method
我确信这非常简单,但我就是看不出来。我在C++Xcode中得到了以下链接器错误。
Undefined symbols for architecture x86_64:
"Random::NextInt(int, int)", referenced from:
Helpers::MakeData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int) in Helpers.o
帮助者::MakeData
#include "Helpers.h"
#include "Random.h"
void Helpers::MakeData(string dataFile, int numLines)
{
vector<double> weights = { -0.1, 0.2, -0.3, 0.4, -0.5,
0.6, -0.7, 0.8, -0.9, 1.0,
-1.1, 1.2, -1.3, 1.4, -1.5,
1.6, -1.7, 1.8, -1.9, 2.0,
-0.5, 0.6, -0.7, 0.8, -0.9,
1.5, -1.4, 1.3,
-1.2, 1.1, -1.0,
0.9, -0.8, 0.7,
-0.6, 0.5, -0.4,
0.3, -0.2, 0.1,
0.1, -0.3, 0.6 };
NeuralNetwork * nn = new NeuralNetwork(4, 5, 3);
nn->SetWeights(weights);
ofstream myFile;
myFile.open(dataFile);
for (int i = 0; i < numLines; ++i)
{
vector<double> inputs;
for (int j = 0; j < inputs.size(); ++j)
inputs[j] = Random::NextInt(10, 1);
vector<double> outputs = nn->ComputeOutputs(inputs);
string color = "";
int idx = Helpers::IndexOfLargest(outputs);
if (idx == 0) { color = "red"; }
else if (idx == 1) { color = "green"; }
else if (idx == 2) { color = "blue"; }
myFile << inputs[0] << " " << inputs[1] << " " << inputs[2] << " " << inputs[3] << " " << color;
}
myFile.close();
} // MakeData
随机.h
#ifndef __NeuralClassificationProgram__Random__
#define __NeuralClassificationProgram__Random__
#include <iostream>
class Random{
public:
static double NextDouble();
static int NextInt(int high, int low);
};
#endif /* defined(__NeuralClassificationProgram__Random__) */
随机.cpp
#include "Random.h"
#include <time.h>
#include <stdlib.h>
double NextDouble()
{
double rnd;
srand(static_cast<unsigned int>(time(NULL)));
rnd = rand() % 1+0;
return rnd;
}
int NextInt(int high, int low)
{
int rnd;
srand(static_cast<unsigned int>(time(NULL)));
rnd = rand() % high + low;
return rnd;
}
这是因为您从未定义过Random::NextInt(int, int)
,而是定义了NextInt(int, int)
。
换句话说,您忘记了类作用域运算符
尝试
int Random::NextInt(int high, int low)
{
return rand() % high + low;
}
哦,而且不要在程序中多次调用srand
。
您没有在实现中包含类名限定符,因此这些"静态方法"被编译为简单的全局函数。
例如,您有:
int NextInt(int high, int low)
但你需要:
int Random::NextInt(int high, int low)
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误