error LNK 2019 Unresolved externals Build 在c++编译时出错
error LNK 2019 Unresolved externals Build error during compilation in c++
我在 visual studio 2015 年尝试编译我的项目时不断收到此错误,我尝试了无数替代方案,但 none 似乎有效。
到目前为止我已经尝试过:
- 确保我是 运行 正确的 visual studio 项目(控制台应用程序)
- 检查了头文件并确保它们的顺序正确。
这是带有 main 函数的代码,main 函数是一个 hello world,我只是想让它先编译,所以我将 main 函数缩小到一个简单的 hello world 以便我可以专注于头文件。我真的需要多一双眼睛,这个错误一直是一场噩梦,欢迎任何调试想法。提前致谢。
代码:
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;
}
};
KNN.h
#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));
}
};
Main.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:\users\underdog\documents\visual studio 2015\Projects\KNearestNeighbour\Debug\KNearestNeighbour.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
在 C++ 中,程序的主要函数必须是全局命名空间中名为 main
的函数,具有以下两个签名之一:
int main()
int main(int, char**)
你的程序没有这样的东西。它在 class TestKNN
中有一个名为 main
的成员函数,但那是完全不相关的东西。
与Java和C#不同,main()
在C++中需要是全局自由函数。现在你有
class TestKNN {
public:
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}
};
这将不起作用,因为不考虑名称空间内的成员函数和函数。您需要将其从 class 中删除,只需要
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}
我在 visual studio 2015 年尝试编译我的项目时不断收到此错误,我尝试了无数替代方案,但 none 似乎有效。
到目前为止我已经尝试过:
- 确保我是 运行 正确的 visual studio 项目(控制台应用程序)
- 检查了头文件并确保它们的顺序正确。
这是带有 main 函数的代码,main 函数是一个 hello world,我只是想让它先编译,所以我将 main 函数缩小到一个简单的 hello world 以便我可以专注于头文件。我真的需要多一双眼睛,这个错误一直是一场噩梦,欢迎任何调试想法。提前致谢。
代码:
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;
}
};
KNN.h
#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));
}
};
Main.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:\users\underdog\documents\visual studio 2015\Projects\KNearestNeighbour\Debug\KNearestNeighbour.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
在 C++ 中,程序的主要函数必须是全局命名空间中名为 main
的函数,具有以下两个签名之一:
int main()
int main(int, char**)
你的程序没有这样的东西。它在 class TestKNN
中有一个名为 main
的成员函数,但那是完全不相关的东西。
与Java和C#不同,main()
在C++中需要是全局自由函数。现在你有
class TestKNN {
public:
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}
};
这将不起作用,因为不考虑名称空间内的成员函数和函数。您需要将其从 class 中删除,只需要
int main() {
cout << "HELLO wORLD";
system("PAUSE");
return 0;
}