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

时间:2012-11-08 14:07:49

标签: c++ main

  

可能重复:
  What is an undefined reference/unresolved external symbol error and how do I fix it?

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

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

代码如下:

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();
    ...

这个方法mail-&gt; SendNow是我评论解决问题的方法,所以我猜 我在SendMail.cpp或SendMail.h

中有一些头声明问题

现在剩下的类和标题:

SendMail.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

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

如您所见,发送方法被注释掉了,所以我无法弄清问题是什么。

编译器输出为:

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

2 个答案:

答案 0 :(得分:11)

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

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

一旦你知道这意味着,很容易找到错误:

您将函数定义为:

void SendNow()

这是一个全局函数而不是类函数,因此你没有实现你承诺实现的类函数。

您可以通过将其转换为:

来解决此问题
void SendMail::SendNow()

请注意,Check()中存在同样的问题,即使这还没有导致错误。

答案 1 :(得分:10)

你的意思是

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

而不是

void Check(int iStatus, char *szFunction)
void SendNow()
相关问题