错误 LNK 2019 在 c++ 中编译期间未解析的外部生成错误
error LNK 2019 Unresolved externals Build error during compilation in c++
当我尝试在Visual Studio 2015中编译我的项目时,我不断收到此错误,我已经尝试了无数的替代方案,但似乎没有一种有效。
到目前为止我尝试过:
- 确保我正在运行正确的视觉工作室项目(控制台应用程序(
- 已检查头文件并确保它们按正确的顺序排列。
我只想先编译它,所以我将 main 函数缩小到一个简单的 hello 世界,以便我可以专注于头文件。我真的需要一双额外的眼睛,这个错误是一场噩梦,欢迎任何调试想法。提前谢谢。
法典:
KNNNode.h
#pragma once
#include <string>
using namespace std;
class KNNNode {
private:int index;
double distance;
string c;
public:
KNNNode(int index, double distance, string c) {
index = index;
distance = distance;
c = c;
}
int getIndex() {
return index;
}
void setIndex(int lindex) {
index = lindex;
}
double getDistance() {
return distance;
}
void setDistance(double ldistance) {
distance = ldistance;
}
string getC() {
return c;
}
void setC(string lc) {
c = lc;
}
};
克恩·
#pragma once
#include <vector>
#include <queue>
#include <unordered_map>
#include "KNNNode.h"
struct compareDistance {
bool operator()(KNNNode lhs, KNNNode rhs) {
return (lhs.getDistance() >= rhs.getDistance());
}
};
class KNN {
bool contains(vector<int> vect, int num) {
for (int i = 0; i < vect.size(); i++) {
if (vect[i] == num)
return true;
}
return false;
}
public:
vector<int> getRandKNum(int k, int max) {
vector<int> randomList = vector<int>(k);
for (int i = 0; i < randomList.size(); i++) {
int temp = static_cast<int>(rand() * max);
if (!contains(randomList, temp)) {
randomList.push_back(temp);
}
else {
i--;
}
}
return randomList;
}
double calDistance(vector<double> d1, vector<double> d2) {
double distance = 0.00;
for (int i = 0; i < (d1.size() - 1); i++) {
distance += (d1[i] - d2[i]) * (d1[i] - d2[i]);
}
return distance;
}
string knn(vector<vector<double>> datas, vector<double> testData, int k) {
priority_queue< KNNNode, vector<KNNNode>, compareDistance> pq;
vector<int> randNum = getRandKNum(k, datas.size());
for (int i = 0; i < k; i++) {
int index = randNum[i];
vector<double> currData = datas[index];
string c = to_string(currData[currData.size() - 1]);
KNNNode node = KNNNode(index, calDistance(testData, currData), c);
pq.push(node);
}
for (int i = 0; i < datas.size(); i++) {
vector<double> t = datas[i];
double distance = calDistance(testData, t);
KNNNode top = pq.top();
if (top.getDistance() > distance) {
pq.pop();
pq.push(KNNNode(i, distance, to_string(t[t.size() - 1])));
}
}
return getMostClass(pq);
}
string getMostClass(priority_queue<KNNNode, vector<KNNNode>, compareDistance> pq) {
unordered_map<string, int> classCount;
for (int i = 0; i < pq.size(); i++) {
KNNNode node = pq.top();
pq.pop();
string c = node.getC();
if (!(classCount.find(c) == classCount.end())) {
int num = classCount.find(c)->second;
classCount.insert({ { c, num + 1 } });
}
else {
classCount.insert({ { c, 1 } });
}
}
int maxCount = 0;
string maxString;
for (auto& x : classCount) {
if (x.second > maxCount) {
maxCount = x.second;
maxString = x.first;
}
}
return to_string(classCount.at(maxString));
}
};
主.cpp
#include "KNN.h"
#include <iostream>
class TestKNN {
public:
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}
};
错误:
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>c:usersunderdogdocumentsvisual studio 2015ProjectsKNearestNeighbourDebugKNearestNeighbour.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
与 Java 和 C# 不同,main()
C++ 中需要是一个全局自由函数。 现在你有
class TestKNN {
public:
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}
};
这不会作为成员函数工作,并且不考虑命名空间内的函数。 您需要将其从类中删除,然后只需
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}
在C++中,程序的主函数必须是全局命名空间中名为 main
的函数,具有以下两个签名之一:
-
int main()
-
int main(int, char**)
你的程序没有这样的东西。它在类TestKNN
中有一个名为main
的成员函数,但这是完全不相关的。
相关文章:
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- Visual Studio 2017 中的 QtMetaObject 未解决的外部错误
- 外部"C"拆卸导致奇怪的链接器错误
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 每次我要使用外部资源时,我都会得到:collect2。exe:错误 ld 返回 1 退出状态
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- C#WPF和C++/CLI以及C++在C++/CLI中添加外部库错误
- 函数 LNK2019 "int __cdecl __scrt_common_main_seh(void)" 中引用的未解析外部符号主错误 (?__scrt_common_main_seh@@YAHXZ
- C++:在"try"外部创建的类型会导致错误,但在内部不会
- 编译错误外部接口Simulink
- 为什么这会给出多个定义错误:外部 int i;国际 i;.