可能重复: What is an undefined reference/unresolved external symbol error and how do I fix it?
我正在学习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();
...这个方法mail->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;
};
#endifSendMail.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发布于 2012-11-08 14:11:59
你的意思是
void SendMail::Check(int iStatus, char *szFunction)
void SendMail::SendNow()而不是
void Check(int iStatus, char *szFunction)
void SendNow()发布于 2012-11-08 14:15:45
基本上,这个错误意味着您有一个函数,您承诺在头中实现它,但是当它到达它实际需要的部分时,它没有找到它。
如果您注释掉函数调用,您将实现该函数的承诺仍然存在。然而,没有人真正地使用这个函数,所以你没有兑现你的承诺并不重要。
一旦你知道了它的意思,就很容易找出问题所在:
您将该函数定义为:
void SendNow()这是一个全局函数,而不是一个类函数,因此您还没有实现您承诺实现的类函数。
您可以通过将其转换为:
void SendMail::SendNow()请注意,您在Check()中也有同样的问题,即使这还没有导致错误。
https://stackoverflow.com/questions/13290584
复制相似问题