C ++ LNK2001 error: unresolved external _main character function

Possible duplicate:
What is an undefined link / unresolved external symbol error and how to fix it?

I am learning C ++ and I have a compilation problem in my project. I read a lot of posts with this error in the header, but I can not find where the problem is.

I have a method call in my main function that is responsible for the error. Whenever I comment on a line, the project compiles perfectly.

The code is as follows:

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

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

Now the rest of the classes and headers:

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

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

Compiler Output:

 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 
+7
source share
2 answers

You meant

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

instead

 void Check(int iStatus, char *szFunction) void SendNow() 
+10
source

Basically, this error means that you have a function that you promise to implement in your header, but when it got to the part where it really needed the function, it did not find it.

If you comment out a function call, the promise that you will execute that function still exists. However, no one uses this function, so it does not matter that you do not fulfill your promise.

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

You define the function as:

 void SendNow() 

This is a global function, not a class function, so you did not implement the class function that you promised to implement.

You can fix this by turning it into:

 void SendMail::SendNow() 

Note that you have the same problem in Check() , although this has not yet caused an error.

+11
source

All Articles