简体   繁体   中英

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

I keep getting this error when I try to compile my project in visual studio 2015 and I have tried countless alternatives but none seems to work.

What I have tried so far:

  • Made sure I am running the correct visual studio project (console application)
  • Inspected the header files and made sure they are put in the right order.

Here's the code with the main function, the main function is a hello world, I just want it to compile first so I narrowed down the main function to a simple hello world so that I can focus on the header files. I really need an extra set of eyes here,this error has been a nightmare, any debugging ideas are welcome. Thanks in advance.

code:

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

Error:

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

Unlike Java and C#, main() , in C++ needs to be a global free function. Right now you have

class TestKNN {
public:
    int  main() {

        cout << "HELLO wORLD";

        system("PAUSE");
        return 0;
    }
};

Which will not work as member functions and functions inside namespaces are not considered. You need to remove it from the class and just have

int  main() {

    cout << "HELLO wORLD";

    system("PAUSE");
    return 0;
}

In C++, the main function of the program must be a function named main , in the global namespace, with one of these two signatures:

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

Your program doesn't have anything like this. It has a member function named main in the class TestKNN , but that is something totally unrelated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM