错误LNK 2019未解析的外部在c ++编译期间构建错误

时间:2017-05-22 14:30:51

标签: c++ visual-studio-2012 visual-studio-2015 lnk2019 unresolved-external

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

到目前为止我尝试过的内容:

  • 确保我正在运行正确的visual studio项目(控制台应用程序)
  • 检查了头文件并确保它们按正确的顺序排列。

这是带有main函数的代码,main函数是一个hello world,我只想让它先编译,所以我把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;
    }
};

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 ==========

2 个答案:

答案 0 :(得分:1)

与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;
}

答案 1 :(得分:0)

在C ++中,程序的主要功能必须是全局命名空间中名为main的函数,具有以下两个签名之一:

  • int main()
  • int main(int, char**)

你的程序没有这样的东西。它在类main中有一个名为TestKNN的成员函数,但这是完全不相关的。