C++ C++;错误LNK2001:未解析的外部符号函数\u main

C++ C++;错误LNK2001:未解析的外部符号函数\u main,c++,main,C++,Main,可能重复: 我正在学习C++,我的项目中有编译问题。 我读过很多文章,标题上有这个错误,但我找不到问题出在哪里 我在我的主函数中有一个方法调用,它对错误负责。每当我对这行进行注释时,项目的编译就完美无瑕 代码如下: Main.cpp #pragma once #include "stdafx.h" #include <iostream> #include <sstream> #include <WinSock.h> #include <Windows

可能重复:

我正在学习C++,我的项目中有编译问题。 我读过很多文章,标题上有这个错误,但我找不到问题出在哪里

我在我的主函数中有一个方法调用,它对错误负责。每当我对这行进行注释时,项目的编译就完美无瑕

代码如下:

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("somemail@mail.com","Envio de C++","Cuerpo del mail");    
    char* msg="";   
    mail->SendNow();
    ...
#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;     
    //  }
}
SendMail.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("somemail@mail.com","Envio de C++","Cuerpo del mail");    
    char* msg="";   
    mail->SendNow();
    ...
#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;     
    //  }
}
你是说

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

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

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

如果您注释掉函数调用,您将实现该函数的承诺仍然存在。然而,实际上没有人在使用这个函数,所以你不履行承诺也没关系

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

您将函数定义为:

void SendNow()
这是一个全局函数而不是类函数,因此您没有实现承诺要实现的类函数

您可以将其转换为:

void SendMail::SendNow()

请注意,您在
Check()
中遇到了相同的问题,即使这还没有导致错误。

我会检查它。如果问题在声明中,为什么在注释掉方法时它会编译call@CarlosLande你读过相关问题了吗?是的@Luchian Grigore。非常感谢您的完整解释,mate
#pragma once
用于头文件;你的
#include
方式超出了必要的范围,你使用了非标准形式的
main
,你在
std::string
做得更好的地方使用了非常量字符指针,你将c-include与c++-include混合在一起,包括:善意的建议:获得一本好的介绍性书籍。