Unresolved External Symbol while creating an object - c++

I am trying to run a main.cpp which access 3 different classes. For some reason, I am getting a unresolved external symbol error. From what I've seen online, its clearly a linking error somewhere, but I cannot find it. I've listed the error below, but it's got a lot of info in it and im having trouble finding out exactly what it means.
The error: main.obj:-1: error: LNK2001: unresolved external symbol "public: __thiscall AtpReader::AtpReader(class std::basic_string,class std::allocator >)" (??0AtpReader##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
My code is:
main.cpp:
#include <iostream>
#include "atlasobject.h"
#include "atp.h"
#include "atpreader.h"
using namespace std;
int main()
{
AtpReader reader("E:/doc.txt");
return 0;
}
AtpReader.h:
#ifndef ATPREADER_H
#define ATPREADER_H
#include "atp.h"
class AtpReader
{
public:
AtpReader();
AtpReader(string filename);
void atpReadHeader();
void atpRead();
string decryptLine(string line);
ATP readerATP;
private:
string file;
};
#endif // ATPREADER_H
atp.h:
#ifndef ATP_H
#define ATP_H
#include "atlasobject.h"
#include "vector"
struct Image{
string Dim;
string Vox;
string Ori;
char* data;
};
class ATP
{
public:
ATP();
vector<AtlasObject> listOfMaps;
private:
Image referenceImage;
};
#endif // ATP_H
and AtlasObject.h:
#ifndef ATLASOBJECT_H
#define ATLASOBJECT_H
#include <string>
using namespace std;
class AtlasObject{
public:
//virtual void create();
AtlasObject();
void set_uid(string id);
void set_label(string l);
void set_offset(string o);
void set_mapInfo(string info);
void set_data(char* inData);
void set_isEncrypted(int encrypted);
string get_uid();
string get_label();
string get_offset();
string get_mapInfo();
char* get_data();
int get_isEncrypted();
protected:
string uid;
string label;
string offset;
string mapInfo;
char *data;
int isEncrypted;
};
#endif // ATLASOBJECT_H
my AtpReader.cpp is:
#include "atpreader.h"
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <sstream>
AtpReader::AtpReader()
{
printf("AtpReader()\n");
}
AtpReader::AtpReader(string filename)
{
printf("AtpReader(%s)\n",filename.c_str());
}

I see you did not include AtpReader.h in AtpReader.cpp, but probably you just missed it when you made copy/paste, to insert it here, because if you didn't really include it, the error would have been different. Also, I see you're including in your main.cpp both "atlasobject.h"
and "atp.h" and you don't really need that.
Later edit: Your problem is in the atp.h...you constructor is declared but never defined. Do this: ATP(){};

Try using g++ in linux terminal
make the object files of each of the source codes and then link the object files and run the executable
g++ -c atp.cpp AtpReader.cpp AtlasObject.cpp
g++ -o exe atp.o AtpReader.o AtlasObject.o
./exe

AtpReader.cpp is not getting built or the its object file is not getting linked to final executable. Check if AtpReader.obj/.o is created in build directory.

Because of the linker error you are getting and assuming that this is some of your actual code. Since I can't see any function inlining, global constants or variables being used out of scope I think the problem is located in the AtpReader.cpp, are you missing an #include AtpReader.h there?
With just a function prototype, the compiler can continue without error, but the linker cannot resolve a call to an address because there is no function code or variable space reserved. You will not see this error until you create a call to the function that the linker must resolve.

Related

Visual Studio doesn't accept class implementation [C++]

I'm trying to move from Dev C++ to Visual Studio while studying C++ (since I'll have to work with the latter) but for some reason, a rather simple class implementation that perfectly works in Dev C++ creates a long list of errors in Visual Studio.
The files are simple:
header file, for the declaration of constructors, variables etc
cpp file, to implement said constructors, functions etc
consoleapplication file (on visual studio), to produce the "main()" function.
stock2.h
#ifndef STOCK2_H_
#define STOCK2_H_
class Stock
{
public:
Stock();
Stock(const char* co, int n = 0, double pr = 0.0);
~Stock();
void show()const;
private:
std::string company;
int shares;
double share_val;
double total_val;
};
#endif
stock2.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include "stock2.h"
Stock::Stock() //default constructor
{
//code
}
Stock::Stock(const char* co, int n, double pr)
{
//code
}
Stock::~Stock()
{
std::cout << "Stock object has been destroyed" << std::endl;
}
//Methods
void Stock::show() const
{
//code
}
ConsoleApplication.cpp
#include "stdafx.h"
#include "stock2.cpp"
int main()
{
using std::cout;
const int STKS = 4;
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.1),
Stock("Boffo Objects", 200, 2.0),
Stock(),
Stock("Monolithic Obelisks", 130, 3.25)
};
cout << "Stock Holdings: \n";
for (int st = 0; st<STKS; st++)
stocks[st].show();
return 0;
}
I've tried to find the solution on other questions posted here but I really can't figure out what's wrong here.
I also read that one is not supposed to #include a cpp file since the header should be the link between the main() and the cpp file itself, but if I decide to use #include stock2.H instead of .CPP in consoleapplication, then the compiler can't find the methods implementations anymore.
EDIT: In the rush i forgot to post the errors!
They're all in this form:
Error LNK2005
"public: void __thiscall Stock::update(double)" (?update#Stock##QAEXN#Z) already defined in
ConsoleApplication1.obj ConsoleApplication1 //path\ConsoleApplication1\ConsoleApplication1\stock2.obj
EDIT2: Since many of you are asking me about the "Solution Explorer", I better just add a screenshot to show you how it's made right now
You included stock2.cpp in your ConsoleApplication.cpp. This means all the code inside stock2.cpp is now compiled twice, and the linker shows the error message
Error LNK2005 "public: void __thiscall Stock::<...> already defined
for the now duplicated functions. Simply replace
#include "stock2.cpp"
with
#include "stock2.h"
If you get another error when doing so, please post the error message for this.

C++ unresovled external symbol link error related with name mangling?

all
i created a static lib project with VS 2010 sp1, and simply defined a class with a member function (code snippet):
.h:
namespace puphttp{
class CRequester
{
public:
void RequesterUpdate();
};
}
.cpp:
#include "StdAfx.h"
#include "Requester.h"
#include <iostream>
using namespace std;
namespace puphttp{
void CRequester::RequesterUpdate()
{
cout<<"updating";
}
}
at last i linked the lib file and tried to invoke the following code:
puphttp::CRequester c;
c.RequestUpdate();
the following link error occur as i compile:
error LNK2001: unresolved external symbol "public: void __cdecl puphttp::CRequester::RequestUpdate(void)" (?RequestUpdate#CRequest#puphttp##QEAAXXZ)
then i used dumpbin to check the actual function name in my lib, it's:
(?RequestUpdate#CRequest#puphttp##QAEXXZ)
so the difference is QEAAXXZ vs QAEXXZ, i didn't have time to get into name mangling rule yet, so any quick answer? really much appreciated.

LNK2019 error when trying to test a dll

I work with an application that appears (just started a few weeks ago, so I am still learning the older apps) to be built in C and my company wants to use that program's ability to make a call to an outside DLL to extend some new functionality. To do this, I started working on my POC, which is the first two files below. The only specification we were given was that the dll has to export the following function:
extern int __stdcall TestMethod_LoadCustomer(const char * name, char * id);
I tried to implement that as follows:
TestDLL.h
#define TestDLL_API __declspec(dllexport)
namespace TestDLL
{
class TestDLL
{
public:
static TestDLL_API int TestMethod_LoadCustomer(const char* name, char* id);
};
}
TestDLL.cpp
// TestDLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "TestDLL.h"
#include <string.h>
extern "C" int __declspec(dllexport) __stdcall TestMethod_LoadCustomer(const char* name, char* id)
{
if (strlen(name) <= 8) {
strcpy(id, name); // name contains customer id
} else {
id[0] = 0; // Customer not found
}
return 0;
}
These two files compile fine. The problem comes in when I try to test this dll via a separate little console app shown here:
RunTEST.cpp
// RunTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "TestDLL.h"
using namespace std;
int _tmain()
{
char* id= "";
TestDLL::TestDLL::TestMethod_LoadCustomer("77777", id);
cout << id;
cin >> id;
return 0;
}
All I am looking for is to be able to pass in a character string into the call TestMethod_LoadCustomer() and have it be added to the id field.
When I actually try to build this solution, I get the following error:
"error LNK2019: unresolved external symbol "public: static int __cdecl TestDLL::TestDLL::TestMethod_LoadCustomer(char const *, char *)" (?TestMethod_LoadCustomer#TestDLL#1#SAHPBDAD#Z) referenced in function _wmain"
I am assuming it has something to do with the way I am trying to reference it in my client app, but I am not sure. I have looked at other LNK2019 errors on StackOverflow, but none of those solutions seemed to work here, of I have incorrectly implemented them. Can any one assist in helping me get rid of this error message?
On the TestDLL.cpp file is missing two things:
1) The namespace TestDLL.
2) the TestDLL:: before the method name.
// TestDLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "TestDLL.h"
#include <string.h>
namespace TestDLL {
extern "C" int __declspec(dllexport) __stdcall TestDLL::TestMethod_LoadCustomer(const char* name, char* id)
{
if (strlen(name) <= 8) {
strcpy(id, name); // name contains customer id
} else {
id[0] = 0; // Customer not found
}
return 0;
}
}
While defining the function in TestDLL.cpp you didn't mention that the function is member of the class TestDLL.

unresolved external symbol visual studio 2010 error

i've implemented with visual studio 2010 professional a solution containing 2 projects; the first called OptDll consist of a dynamic library with a method i want to export, while the second is a exe project, called prova, in order to try the dll.
I've included the correct references and all work fine until i decided to insert in OptDll project a new class (GlobalOutput) in order to create the output parameters i want. When i build the project OptDll no error occurs but when i build the whole solution i get these errors in the prova project:
Error 59 error LNK2019: unresolved external symbol "public: __thiscall GlobalOutput::~GlobalOutput(void)" (??1GlobalOutput##QAE#XZ) referenced in function _main C:\Users\ANTONIO\Desktop\optDll\prova\prova.obj
Error 60 error LNK2019: unresolved external symbol "public: __thiscall GlobalOutput::GlobalOutput(void)" (??0GlobalOutput##QAE#XZ) referenced in function _main C:\Users\ANTONIO\Desktop\optDll\prova\prova.obj
Error 61 error LNK1120: 2 unresolved externals C:\Users\ANTONIO\Desktop\optDll\Debug\prova.exe 1
I read it could be a problem with the class constructor/destructor but i don't fix it.
Below the code of interest.
OptFunDll.h
#ifdef OPTFUNDLL_EXPORTS
#define OPTFUNDLL_API __declspec(dllexport)
#else
#define OPTFUNDLL_API __declspec(dllimport)
#endif
//#include "tool_library.h"
#include "GlobalOutput.h"
namespace optFun
{
// This class is exported from the optFunDll.dll
class myoptFun
{
public:
// funzione che implementa il modulo di ottimizzazione
class OPTFUNDLL_API GlobalOutput;
static OPTFUNDLL_API void scheduling(const char*,const char*,const char*,const char*,GlobalOutput&);
};
}
OptFunDll.cpp
#include "stdafx.h"
#include "optFunDll.h"
#include <stdexcept>
using namespace std;
::Random Particle::_rnd;
::Random ParticleSwarm::_rnd;
namespace optFun
{
void myoptFun::scheduling(const char *pRicette,const char *pWarehouse,const char *pFarmacia,const char *pShaker,GlobalOutput& total){
//here some code...
//class definition
total.CreateDataPrescription(final_list);
total.time=time_output;
GlobalOutput.h
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxadv.h>
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include "GlobalInfo.h"
#include "tool_library.h"
struct DataPrescription{
EDrug NameDrug;
double Dosage;
EContainerType DestType;
ELiquid IdDest;
double CapacityDest;
double Priority;
bool ScaricoShaker;
DataPrescription(){
NameDrug=EDrug_NoDrug;
Dosage=0.0;
DestType= EContainerType_Tot;
IdDest=ELiquid_NoLiquid;
CapacityDest=0.0;
Priority=0.0;
ScaricoShaker=true;
}
DataPrescription(EDrug name,double dos,EContainerType dest,ELiquid ID,double cap_dest,double p,bool _ScaricoShaker){
NameDrug=name;
Dosage=dos;
DestType=dest;
IdDest=ID;
CapacityDest=cap_dest;
Priority=p;
ScaricoShaker=_ScaricoShaker;
}
};
class GlobalOutput{
public:
CArray<DataPrescription> OptList;
time_info time;
GlobalOutput();
~GlobalOutput();
void CreateDataPrescription(vector<ricetta>&);
};
#endif
GlobalOutput.cpp
#include "stdafx.h"
#include "GlobalOutput.h"
GlobalOutput::GlobalOutput(){
time.total_makespan=0;
}
GlobalOutput::~GlobalOutput(){
}
void GlobalOutput::CreateDataPrescription(vector<ricetta>& list){
//DataPrescription tmp;
for(unsigned int i=0;i<list.size();i++){
DataPrescription tmp(list[i].getID(),list[i].getdosage(),list[i].get_destination(),list[i].get_DestType(),list[i].get_CapacityDest(),list[i].getPriority(),list[i].processing_info.scarico_shaker);
this->OptList.Add(tmp);
}
}
and Finally the main of prova project:
#include "stdafx.h"
#include "optFunDll.h"
#include <iostream>
//#include "C:\Users\ANTONIO\Desktop\optDll\optDll\tool_library.h"
using namespace std;
int main()
{
const char *pRicette;
pRicette=new char(NULL);
const char *pWarehouse;
pWarehouse=new char(NULL);
const char *pFarmacia;
pFarmacia=new char(NULL);
const char *pShaker;
pShaker=new char(NULL);
optFun::myoptFun::GlobalOutput total;
optFun::myoptFun::scheduling(pRicette,pWarehouse,pFarmacia,pShaker,total);
return 0;
}
Thanks for your help.
Please let me know if you want more info.
if I comment the code lines under class definition in OptDll.cpp i get these errors:
Error 5 error C2079: 'total' uses undefined class 'optFun::myoptFun::GlobalOutput' c:\users\antonio\desktop\optdll\prova\prova.cpp 23
Error 6 error C2664: 'optFun::myoptFun::scheduling' : cannot convert parameter 5 from 'int' to 'optFun::myoptFun::GlobalOutput &' c:\users\antonio\desktop\optdll\prova\prova.cpp 24
instead if i uncomment the definition of total i obtain also:
9 IntelliSense: incomplete type is not allowed c:\users\antonio\desktop\optdll\optdll\optfundll.cpp 131
Thanks for you availability, I'm new on c++ programming.
You are not actually exporting your class GlobalOutput.
You need to have:
class OPTFUNDLL_API GlobalOutput

C++ LNK2019 errors everytime after I add object of class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I'm working on own double linked list and everytime after adding object of class DoubleList in Evidence.cpp I get a LNK2019 error: Unresolved external symbol. I will be glad for every advice. Here are my codes:
StudentRecord.h
#pragma once
#include <string>
#include <iostream>
#include <algorithm>
#include "Student.h"
#include "DoubleList.h"
using namespace std;
using namespace SemestralWork;
class StudentRecord{
public:
DoubleList<Student> *List; //declared object (in StudentRecord.cpp is problem with that)
StudentRecord(void);
~StudentRecord(void);
Student &SearchStudent(const string &ID);
void addStudent(const Student &student, Student::Position position = Student::Position::LAST);
Student RemoveStudent(const string &ID);
void WriteAllStudents(void);
void Cancel(void);
};
StudentRecord.cpp (just part)
#include "StdAfx.h"
#include "StudentRecord.h"
using namespace SemestralWork;
StudentRecord::StudentRecord(void)
{
List = new DoubleList<Student>(); // <---- here is first LNK2019 error
}
Student &StudentRecord::SearchStudent(const string &ID){
Student * SearchedStudent;
Student * EmptyStudent = NULL;
//********** down here are next 4 LNK2019 errors. ************
for(DoubleList<Student>::iterator it = List->begin(); it != List->end(); ++it){
if(ID == List->AccessActual().getID()){
SearchedStudent = &List->AccessActual();
return *SearchedStudent;
}
} // 5 unresolved externals
return *EmptyStudent;
}
//...
DoubleList (just constructor)
template<typename T>
DoubleList<T>::DoubleList(void){
NumberOfElements = 0;
First= NULL;
Last= NULL;
Actual= NULL;
}
Student.h
#pragma once
#include <string>
using namespace std;
class Student
{
private:
string firstName, lastName, ID;
public:
Student(string, string, string);
~Student(void);
string getFirstName();
string getLastName();
string getID();
enum Position{ FIRST, LAST, ACTUAL, PREVIOUS, NEXT};
};
EDIT: Error message here:
Error 5 error LNK2019: unresolved external symbol "public: class Student & __thiscall SemestralWork::DoubleList::AccessActual(void)" (?AccessActual#?$DoubleList#VStudent###SemestralWork##QAEAAVStudent##XZ) referenced in function "public: class Student & __thiscall StudentRecord::SearchStudent(class std::basic_string,class std::allocator > const &)" (?SearchStudent#StudentRecord##QAEAAVStudent##ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
(one of five LNK errors)
So, you have a message. I'll break the message in several lines. The message should be read like this:
LNK2019: unresolved external symbol
"some function or variable that compiler saw nicely declared but the linker can't find the definition of"
(name of the missing symbol as linker sees it)
referenced in function
"some function where the missing function/variable is being used"
(name of the function as linker sees it)
In your case linker needs function DoubleList::AccessActual(void) in namespace SemestralWork. The function is declared somewhere, most likely in DoubleList.h. You probably need to add DoubleList.cpp file to the project. Maybe you forgot to define the function? In that case you have to write it.
Also, you really, really need to remove using namespace lines from header files. Really! Classes that exist in namespaces MUST be declared like this:
namespace SomeNamespace {
class SomeClass
{
void SomeFunction();
...
}
}
And SHOULD be defined like this in source file:
void SomeNamespace::SomeClass::SomeFunction()
{
...
}
In header files all stuff from any other namespaces, including std namespace, SHOULD be used with full name (std::string). In source files you MAY use using namespace std AFTER all #include directives. Some people disagree on this last one, but it's a matter of style.