C++ 错误 LNK2001:未解析的外部符号函数 _main

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13290584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:09:24  来源:igfitidea点击:

C++ error LNK2001: unresolved external symbol function _main

c++main

提问by Carlos Landeras

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

可能的重复:
什么是未定义的引用/未解析的外部符号错误,我该如何解决?

I'm learning C++ and I have a compiling problem in my project. I have read tons of post with this error on the title but I cant find where the problem is.

我正在学习 C++,但我的项目中有一个编译问题。我已经阅读了大量标题上有此错误的帖子,但我找不到问题所在。

I have a method call in my Main function that is responsible for the error. Whenever I comment the line the project compiles perfect.

我的 Main 函数中有一个方法调用负责该错误。每当我评论该行时,该项目都会完美编译。

The code is the following:

代码如下:

Main.cpp

主程序

#pragma once 
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <WinSock.h>
#include <Windows.h>
#include <string.h>
#include "NetUtils.h"
#include "Utils.h"
#include "FileUtils.h"
#include "SendMail.h"
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{   

    SendMail *mail = new SendMail("[email protected]","Envio de C++","Cuerpo del mail");    
    char* msg="";   
    mail->SendNow();
    ...

This method mail->SendNow is the one I comment to solve the problem, so I guess I have some kind of header declaration problem inside of SendMail.cpp or SendMail.h

这个方法mail->SendNow是我评论解决问题的方法,所以我猜我在SendMail.cpp或SendMail.h内部有某种标题声明问题

Now the rest of the classes and headers:

现在剩下的类和标题:

SendMail.h

发送邮件.h

#pragma once
#ifndef SENDMAIL_H
#define SENDMAIL_H


class SendMail

{
public:
    SendMail(char* c_to, char* c_subject, char* c_body);
    void Check(int iStatus, char *szFunction);
    void SendNow();
    char * to;
    char * subject;
    char * body;    
};


#endif

SendMail.cpp

发送邮件.cpp

#define WIN32_LEAN_AND_MEAN

#pragma once
#include "SendMail.h"
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <windows.h>
#include <winsock2.h>



#pragma comment(lib, "ws2_32.lib")
using namespace std;

// Insist on at least Winsock v1.1
const int VERSION_MAJOR = 1;
const int VERSION_MINOR = 1;

#define CRLF "\r\n"                 // carriage-return/line feed pair



SendMail::SendMail(char* c_to, char* c_subject, char* c_body)
{
    to = c_to;
    subject= c_subject;
    body = c_body;

}

// Basic error checking for send() and recv() functions
void Check(int iStatus, char *szFunction)
{
  if((iStatus != SOCKET_ERROR) && (iStatus))
    return;

  cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
}

void SendNow()
{
    // WSADATA     WSData;  

    ///* Attempt to intialize WinSock (1.1 or later)*/
    //  if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
    //  {
    //  cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
    //  ErrMsg="Cannot find Winsock v";
    //  return;     
    //  }
}

AS you can see the method Send is commented out so I cant figure out what the problem is.

如您所见,方法 Send 已被注释掉,因此我无法弄清楚问题是什么。

The compiler output is:

编译器输出是:

Error   6   error LNK1120: 1 unresolved externals   C:\Users\clanderasm\Documents\Visual Studio 2010\Projects\LandeTestConsole\Debug\LandeCplusConsole.exe  LandeCplusConsole
Error   5   error LNK2019: unresolved external symbol "public: void __thiscall SendMail::SendNow(void)" (?SendNow@SendMail@@QAEXXZ) referenced in function _main    C:\Users\clanderasm\Documents\Visual Studio 2010\Projects\LandeTestConsole\LandeCplusConsole\LandeCplusConsole.obj  LandeCplusConsole

回答by Jasper

Basically what that error means is that you have a function that you promise to implement in your header, but when it got to the part where it actually needed the function, it didn't find it.

基本上这个错误意味着你有一个你承诺在你的头文件中实现的函数,但是当它到达它实际需要该函数的部分时,它没有找到它。

If you comment out the function call, the promise that you will implement that function is still there. However, nobody is actually using the function, so it doesn't matter that you don't come through on your promise.

如果您注释掉函数调用,您将实现该函数的承诺仍然存在。但是,实际上没有人使用该功能,因此您不兑现承诺并不重要。

Once you know that it means, it's pretty easy to find what is wrong:

一旦你知道这意味着什么,就很容易找出问题所在:

You are defining the function as:

您将函数定义为:

void SendNow()

This is a global function and not a class function, as such you have not implemented the class function you promised to implement.

这是一个全局函数而不是类函数,因此您尚未实现您承诺实现的类函数。

You can fix this by turning it into:

您可以通过将其转换为以下内容来解决此问题:

void SendMail::SendNow()

Do note that you have the same problem in Check()even though that's not causing an error yet.

请注意,Check()即使尚未导致错误,您也遇到了同样的问题。

回答by Luchian Grigore

Did you mean

你的意思是

void SendMail::Check(int iStatus, char *szFunction)
void SendMail::SendNow()

instead of

代替

void Check(int iStatus, char *szFunction)
void SendNow()