i am really confusing about how to pass an enum type to a function in c++.
i am googling and test all the soloutions presented but non of them solve my problem.
in the SocketInfo.h ,
i have an enum named SocketType that declared globally as :
typedef enum SocketTypeEnum
{
SOCKET_TYPE_IPSEC
} SocketType;
in the SocketInfo.h i have a class named SocketInfo :
class SocketInfo
{
public:
SocketInfo(const char* ip,unsigned short fd,SocketType stype);
}
in the SocketInfo.cpp :
SocketInfo::SocketInfo(const char* ip, unsigned short fd,SocketType stype)
{
//some work done here
}
i build this class without any error
now for test this class i create a win32 console application.in the _tmain i write this code
#include "SocketInfo.h"
void Test_Socket()
{
SocketInfo* si = new SocketInfo(NULL,5060,SOCKET_TYPE_IPSEC);
}
int _tmain(int argc, _TCHAR* argv[])
{
Test_Socket();
getch();
return 0;
}
after running above code i got these errors:
Error 4 error LNK2019: unresolved external symbol "public: __thiscall SocketInfo::SocketInfo(char const *,unsigned short,enum SocketTypeEnum)" (??0SocketInfo##QAE#PBDGW4SocketTypeEnum###Z) referenced in function "void __cdecl Test_Socket(void)" (?Test_Socket##YAXXZ)
Error 5 error LNK1120: 1 unresolved externals
how i can solve these errors.
all codes compile on visual studio 2010 Ultimate.
The problem isn't in how you pass the enum as parameter, which is ok, but that you don't export symbols from your project or import them in your test project.
You need to add the lib file generated by the project defining SocketInfo to the additional dependencies of the test project.
You also need to export the class:
_declspec(dllexport) class SocketInfo
{
public:
SocketInfo(const char* ip,unsigned short fd,SocketType stype);
};
and import it in the test project with _declspec(dllimport). This duality is usually achieved through macros - look it up.
Related
I normally work in c# and am out of my wits for this one . I used Walkthrough: Creating and Using a Dynamic Link Library (C++) to create a Dynamic Link Library.
I have defined two methods as shown below
DeveloperConsoleManager.h
#pragma once
#include "atlstr.h"
#ifdef DEVCONSOLEMANAGER_EXPORTS
#define DEVCONSOLEMANAGER_API __declspec(dllexport)
#else
#define DEVCONSOLEMANAGER_API __declspec(dllimport)
#endif
namespace DeveloperConsoleManager
{
class DeveloperConsoleLogic
{
public:
// Returns a + b
static DEVCONSOLEMANAGER_API double Add(double a, double b);
static DEVCONSOLEMANAGER_API bool CheckforValidFile(CString fileName);
};
}
DeveloperConsoleManager.cpp
// DeveloperConsoleManager.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "DeveloperConsoleManager.h"
namespace DeveloperConsoleManager
{
double DeveloperConsoleLogic::Add(double a, double b)
{
return a + b;
}
bool DeveloperConsoleLogic :: CheckforValidFile(CString fileName)
{
return false;
}
}
I use these methods in a .cpp file in a different project (type: Application (.exe)). When I Build the solution, there are following linker errors
Warning 1 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::Add' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 10
Warning 2 warning C4273: 'DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile' : inconsistent dll linkage e:\md_69\developerconsolemanager\developerconsolemanager.cpp 16
Error 3 error LNK2028: unresolved token (0A0004F1) "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 4 error LNK2019: unresolved external symbol "public: static bool __cdecl DeveloperConsoleManager::DeveloperConsoleLogic::CheckforValidFile(class ATL::CStringT > >)" (?CheckforValidFile#DeveloperConsoleLogic#DeveloperConsoleManager##$$FSA_NV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function "public: void __thiscall CSaSsiConsoleUi::UploadSsiCheck(void)" (?UploadSsiCheck#CSaSsiConsoleUi##$$FQAEXXZ) E:\MD_69\DeveloperConsoleUI\SaSsiConsoleUI.obj
Error 5 error LNK1120: 2 unresolved externals E:\MD_69\Debug\DeveloperConsoleUi.exe
There is no linker error for the "Add" method.
I have already included "DeveloperConsoleManager.lib" in Linker -> Input -> Additional Dependencies. Please help me find out what exactly am I doing wrong.
I would be glad to add any additional information needed.
Thanks to #Igor Tandetnik and the awesome thing that is internet, I figured it out. I am adding it as an answer so that some one else might benefit.
The problem was with CString. The project in which the function was defined was a dynamic link library (dll) and the call was being made from an MFC application. Now, the issue was that, MFC uses for CString while the non-MFC dll uses .
CString in is defined as:
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
while in is defined as:
typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CString;
This, as you can clearly see is different. The workaround I used was using CAtlString instead of CString . However, please feel free to suggest any better way if you come across.
I have the code presented later using Xerces-c, which can be built as a static or dynamic library. Failing to include of course results in a compiler error, however when I add #include <xercesc/util/PlatformUtils.hpp> visual studio 2012 gives me a linker errors saying:
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Initialize(char const * const,char const * const,class xercesc_3_1::PanicHandler * const,class xercesc_3_1::MemoryManager * const)" (__imp_?Initialize#XMLPlatformUtils#xercesc_3_1##SAXQBD0QAVPanicHandler#2#QAVMemoryManager#2##Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Terminate(void)" (__imp_?Terminate#XMLPlatformUtils#xercesc_3_1##SAXXZ) referenced in function __catch$_main$0
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char const * const xercesc_3_1::XMLUni::fgXercescDefaultLocale" (__imp_?fgXercescDefaultLocale#XMLUni#xercesc_3_1##2QBDB)
Based on the dllimport part of the error it seems that it's failing to find a dll. This is confirmed by that when I build Xerces-c as a dynamic library and link to it the error goes away. However if I build Xerces-c as a static library and link to it the same error remains. So my question is why am I getting an error asking for a dll when I'm including and linking to a static library?
using namespace xercesc;
int main(int argc, char* argv[])
{
std::ifstream inputFile(argv[1]);
char c = inputFile.get();
while (inputFile.good()) {
std::cout << c;
c = inputFile.get();
}
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
//XercesDOMParser parser;
//parser.useScanner(XMLUni::fgDGXMLScanner);
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
You need to compile your application with XERCES_STATIC_LIBRARY preprocessor macro to disable DLL import/export for Xerces library.
Also check that you link against static version of .lib files.
I am trying to create a rather simple project in native c++ that calls a a managed dll.
this how my native c++ code looks:
// MyCppStud.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MyStudWrapper\MyStudentWrapperWrapper.h"
int _tmain(int argc, _TCHAR* argv[])
{
char * path = "C:/Users/rami.schreiber/documents/visual studio 2013/Projects/TestProj/test.xml";
MyStudentWrapperWrapper* student = new MyStudentWrapperWrapper();
// student->GetStudent(path);
return 0;
}
and here are the .h and .cpp files for the managed dll (compiled with /clr)
//MyStudentWrapperWrapper.h
#pragma once
//#ifdef THISDLL_EXPORTS
#define THISDLL_API __declspec(dllexport)
/*#else
#define THISDLL_API __declspec(dllimport)
#endif*/
class MyStudentWrapper;
class THISDLL_API MyStudentWrapperWrapper
{
private:
MyStudentWrapper* _Impl;
public:
MyStudentWrapperWrapper();
MyStudentWrapperWrapper(MyStudentWrapper* student);
~MyStudentWrapperWrapper();
MyStudentWrapperWrapper* GetStudent(const char* path);
void SaveStudent(MyStudentWrapperWrapper* student, const char* path);
};
// MyStudentWrapperWrapper.cpp
#pragma once
#include "stdafx.h"
#include "MyStudWrapper.h"
#include "MyStudentWrapperWrapper.h"
MyStudentWrapperWrapper::MyStudentWrapperWrapper()
{
_Impl = new MyStudentWrapper;
}
when i build the solution i get a link error
1>MyCppStud.obj : error LNK2019: unresolved external symbol "public: __thiscall MyStudentWrapperWrapper::MyStudentWrapperWrapper(void)" (??0MyStudentWrapperWrapper##QAE#XZ) referenced in function _main
1>c:\users\...\documents\visual studio 2013\Projects\MyStudentProj\Debug\MyCppStud.exe : fatal error LNK1120: 1 unresolved externals
From what I understand I am not referencing the the .lib file correctly and therefor the linker does not recognize the c'tor for my wrapper class.
can someone please explain how to correctly reference the dll to my c++ project.
thank you very much!
Greetings.
I have searched for a solution, but I think this problem is personal code specific, hence my posting here.
I'll get straight to the point.
In my main I have two objects.
Computer *computer = new Computer();
Player *player = new Player();
In the computer class, in the header I have the following:
private:
Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;
Then in Computer.cpp:
Computer::Computer(char token, bool hasTurn)
{
m_token = token;
m_hasTurn = hasTurn;
strategy = new Strategy();
}
int Computer::getMove(const char (&board)[9])
{
twoInRow = strategy->checkTwoInRow(board);
counter = strategy->counter(board);
makeTwo = strategy->makeTwo(board);
if(twoInRow != 0)
{
return twoInRow - 1;
} else if(counter != 0) {
return counter - 1;
} else if(makeTwo != 0) {
return makeTwo - 1;
} else {
return 0;
}
}
At this point I think the problem arises.
The methods called from the class Strategy all require knowledge of the board thus:
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
The problems im getting, unabling them to compile:
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::makeTwo(char const (&)[9])" (?makeTwo#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 2 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::counter(char const (&)[9])" (?counter#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 3 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::checkTwoInRow(char const (&)[9])" (?checkTwoInRow#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
As a c++ noob, I have absolutely no clue why or how this problem is caused. I think it has to do something with either the instantiation of Strategy in the computer class, or with the parameter given from computer to the strategy in the method calls.
Can anyone explain WHY this error is occuring, I don't quite understand the error at all.
And also how to solve/prevent this?
EDIT*
I just got a request to share the Strategy class:
Strategy.h:
#pragma once
class Strategy
{
public:
Strategy(void);
~Strategy(void);
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
};
The class defined these methods, I won't post them because they are quite long.
This is a linking error and it has nothing to do with instantiations or parameters.
You haven't provided the linker with the definitions for those functions. If you defined them in Strategy.cpp you need to compile that and add it as an argument to the linker. How you do that depends entirely on what tools you're using to build your program.
If you're using Visual Studio (or any other IDE) it should take care of it automatically once you've added Strategy.cpp to your project.
Or did you perhaps define them like this:
int checkTwoInRow(const char (&board)[9])
{
// Do something with board the wrong way
}
instead of like this:
int Strategy::checkTwoInRow(const char (&board)[9])
{
// Do something with board the right way
}
The first one doesn't define a Strategy member function, it defines a global function.
The error is simply stating that you have declared, but not defined, the member functions Strategy::makeTwo, Strategy::counter, and Strategy::checkTwoInRow. Are you sure that you implemented them (in a source file that's actually being compiled) and that you didn't accidentally define them as free functions?
I have two classes, one inherited from the other. When I compile, I get the following errors:
Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base#Parsables#Utility##QAE#XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity#Parsables#Utility##QAE#XZ)
Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base#Parsables#Utility##UAE#XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity#Parsables#Utility##UAE#XZ)
D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals
I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..
"include/Utility/Parsables/Base.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP
namespace Utility
{
namespace Parsables
{
class Base
{
public:
Base( void );
virtual ~Base( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP
"src/Utility/Parsables/Base.cpp"
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
Base::Base( void )
{
}
Base::~Base( void )
{
}
}
}
"include/Utility/Parsables/Entity.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
class Entity : public Base
{
public:
Entity( void );
virtual ~Entity( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
"src/Utility/Parsables/Entity.cpp"
#include "Utility/Parsables/Entity.hpp"
namespace Utility
{
namespace Parsables
{
Entity::Entity( void )
{
}
Entity::~Entity( void )
{
}
}
}
The relevant bit is this:
unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"
You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.
Are you sure Base.cpp is being included in the build?
Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.
Either your base.cpp is not being compiled/linked or you have a misspelling in it