Visual C++ 错误

问题描述 投票:0回答:2

有谁知道如何修复这个错误?我所拥有的只是一个类的头文件和源文件。

1>MSVCRTD.lib(exe_main.obj):错误 LNK2019:函数“int __cdecl invoke_main(void)”中引用的未解析的外部符号 _main (?invoke_main@@YAHXZ)

#ifndef POINT_H
#define POINT_H

#include <iostream>

class Point {
public:
    Point();
    Point(unsigned int x, unsigned int y);
    bool operator== (const Point &other) const;

    unsigned int m_x;
    unsigned int m_y;

    friend std::ostream& operator<<(std::ostream &sout, const Point &pt);
};

#endif

并且

#include "Point.h"


Point::Point() {}  // Needed because of existence of other constructors

Point::Point(unsigned int x, unsigned int y) {
    m_x = x;
    m_y = y;
}

// Note: automatically get copy constructor

bool
Point::operator== (const Point &other) const {
    return (m_x == other.m_x && m_y == other.m_y);
}

std::ostream& operator<<(std::ostream &sout, const Point &pt) {
    sout << "(" << pt.m_x << ", " << pt.m_y << ")";
    return sout;
}
visual-c++
2个回答
0
投票

这似乎是一个链接器错误,可能与#include或冲突的定义有关。 没有代码就无法说更多。


0
投票

几乎晚了 6 年,但我看到两个可能的原因(鉴于所提供的代码)

希望这对偶然发现类似问题的其他人有用。

  1. 您正在尝试构建库,但项目类型设置为Application(.exe)
  2. 您正在构建一个应用程序(.exe),但忽略了提供主要功能。

我已经这样做了很多次,因为我创建了一个项目,将其用作 Visual Studio 的模板,其默认设置为 Application(.exe),有时我会忽略在需要时将其更改为 Library(.lib)建立一个图书馆。

这里发生的事情是,C 运行时(代码位于 MSVCRTD.LIB 中)包含一个名为

invoke_main()
的函数,其工作是在应用程序特定代码中调用 main() 函数。由于源代码不包含名为
main()
的函数,因此链接器给出“无法解析的外部符号...”错误

© www.soinside.com 2019 - 2024. All rights reserved.