错误 LNK 2019 在 c++ 中编译期间未解析的外部生成错误

error LNK 2019 Unresolved externals Build error during compilation in c++

本文关键字:错误 外部 2019 LNK c++ 编译      更新时间:2023-10-16

当我尝试在Visual Studio 2015中编译我的项目时,我不断收到此错误,我已经尝试了无数的替代方案,但似乎没有一种有效。

到目前为止我尝试过:

  • 确保我正在运行正确的视觉工作室项目(控制台应用程序(
  • 已检查头文件并确保它们按正确的顺序排列。
这是带有 main 函数的代码,main 函数是一个 hello 世界,

我只想先编译它,所以我将 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的成员函数,但这是完全不相关的。

相关文章: