如何解决链接器错误?

How to solve linker errors?

我收到这样的错误

LNK2019 函数“int __cdecl invoke_main(void)”中引用的未解析外部符号_main (?invoke_main@@YAHXZ)

LNK1120 1 个未解决的外部问题

我没有使用任何奇怪的库,很简单:

#include <iostream>
using namespace std;
class myClass {
    int* iP;
    float f = 3.0;
public:
    myClass(int i, float f_) {
        iP = new int(i);
        f = f_;
        cout << "A" << *iP * f;
    }

    myClass(int i) {
        iP = new int(i);
        cout << "B" << *iP * f;
    }

    myClass(const myClass& m) {
        iP = new int(*m.iP + 1);
        f = m.f;
        if (m) {
            cout << "C" << *iP + 1;
        }
        else {
            cout << "C" << *iP;
        }
    }
    myClass& operator=(const myClass& m) {
        if (this != &m) {
            delete iP;
            cout << "CA" << *iP * f;
        }
        return *this;
    }
    bool operator!() const {
        return f != 3.0;
    };

    operator bool() const {
        return f == 3.0;
    };
    virtual ~myClass() {
        cout << "X" << *iP;
        delete iP;
    };
    int main() {
        myClass M = 10;
        myClass N = myClass(10, 5.0);
        myClass O = myClass(M);
        return 0;
    }

};

谁能告诉我为什么要这样做?

您将 int main() 函数放在 class decleration 中。

p.s - 如果有帮助,请务必接受此答案:D

我认为您不小心将 main 函数放在了 class 范围内。 尝试移动最后一个 };在你的 int 主线上方