This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I already looked up some answers in Stackoverflow for this type of problem, none of which are helping me out. This question describes how to resolve this error, and that I should provide a definition and not just a declaration. I've done that, but I'm still getting the following error:
Error 13 error LNK2019: unresolved external symbol "private: __thiscall NetworkManager::NetworkManager(void)" (??0NetworkManager##AAE#XZ) referenced in function "public: static class NetworkManager * __cdecl NetworkManager::Instance(void)" (?Instance#NetworkManager##SAPAV1#XZ) C:\Users\HIDDEN\Documents\AGK Projects\C++ Libraries\apps\template_windows_vs2013\NetworkManager.obj Template
Here's the code:
NetworkManager.h
#ifndef _H_NETWORKMANAGER_
#define _H_NETWORKMANAGER_
#include<iostream>
#include<vector>
class NetworkManager
{
private:
NetworkManager();
static NetworkManager * netManager;
public:
int networkID;
static NetworkManager * Instance();
int HostNetwork(std::string netName, std::string hostName, int port);
int JoinNetwork(std::string netName, std::string clientName);
bool IsNetworkActive(int netID);
};
#endif
NetworkManager.cpp
#include<iostream>
#include "NetworkManager.h"
#include "template.h"
NetworkManager * NetworkManager::netManager = NULL;
NetworkManager * NetworkManager::Instance()
{
if (!netManager)
netManager = new NetworkManager;
return netManager;
}
int NetworkManager::HostNetwork(std::string netName, std::string hostName, int port)
{
int networdID__;
const char * netName__ = netName.c_str();
const char * hostName__ = hostName.c_str();
networdID__ = agk::HostNetwork(netName__, hostName__, port);
return networdID__;
}
int NetworkManager::JoinNetwork(std::string netName, std::string clientName)
{
int networdID__;
const char * netName__ = netName.c_str();
const char * clientName__ = clientName.c_str();
networdID__ = agk::JoinNetwork(netName__, clientName__);
return networdID__;
}
bool NetworkManager::IsNetworkActive(int netID)
{
switch (agk::IsNetworkActive(netID))
{
case 0: return false; break;
case 1: return true; break;
}
}
You declared NetworkManager::NetworkManager() in the header file but there is no implementation of it in the source file.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I am using static variable. After referring to Unresolved external symbol on static class members, I modified the program with Abc::ct
#include <iostream>
class Abc
{
private:
static unsigned int ct;
public:
void f1()
{
for (int i = 0; i < 5; ++i)
f2();
}
void f2() {
Abc::ct = 0;
if (Abc::ct == 0)
std::cout << "Zero iteration\n";
std::cout << Abc::ct << "\t";
++Abc::ct;
}
};
int main()
{
Abc obj;
obj.f1();
}
but getting error as error LNK2001: unresolved external symbol "private: static unsigned int Abc::ct" in MSVC or undefined reference to Abc::ct in g++. How can I define static variable in class Abc?
You declared your static variable, but you did not define and initialize it. Above main(), but outside of your class, add the following line:
unsigned int Abc::ct = 0;
or, if you are using C++17, you can change your:
static unsigned int ct;
to:
static inline unsigned int ct = 0;
You have to define it:
unsigned int Abc::ct = 0;
Demo
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
This problem has been solved before, but I've been looking all over, and none of those explains how to fix this, the situation i'm in. Most of them is about external libraries.
I'm trying to test my code. I've made a test class and that class is trying to access another class by including that class's header file. But when I'm trying to call its function it just gives me an unresolved external symbol error.
This is my current attempt. Here I'm trying to access the other classes's header file to call it's functions.
CardTest.cpp
#include <iostream>
#include "../Header Files/Hand.h"
#include "../Header Files/HandValueCalculator.h"
using namespace std;
HandValueCalculator handValueCalculator;
Hand hand;
void Test() {
bool value = handValueCalculator.DoesHandHaveAce(&hand.cards);
cout << value << endl;
}
HandValueCalculator.h
#ifndef HANDVALUECALCULATOR_H_INCLUDED
#define HANDVALUECALCULATOR_H_INCLUDED
#include <vector>
#include "../Header Files/Card.h"
class HandValueCalculator {
public:
HandValueCalculator();
bool DoesHandHaveAce(std::vector<Card>* cards);
int GetValueWithoutAce(std::vector<Card>* cards);
int GetValueWithAce(std::vector<Card>* cards);
};
#endif // HANDVALUECALCULATOR_H_INCLUDED
HandValueCalculator.cpp
#include "../Header Files/HandValueCalculator.h"
HandValueCalculator::HandValueCalculator() {
}
bool HandValueCalculator::DoesHandHaveAce(std::vector<Card>* cards) {
int i;
for (i = 0; i < cards.size(); i++) {
if (cards.at(i).GetValue() == 11) {
return true;
}
}
return false;
}
int HandValueCalculator::GetValueWithoutAce(std::vector<Card>* cards) {
for (i = 0; i < cards.size(); i++) {
int cardValue = cards.at(i).GetValue()
totalValue = totalValue + cardValue;
}
return 0;
}
int HandValueCalculator::GetValueWithAce(std::vector<Card>* cards) {
return 0;
}
This is the error I'm getting, and I don't think the compiler recognizes that the functions have a body, and because it can't find a body for the declared functions it returns an error like this.
C:\Users\fagel\Documents\Blackjack\Blackjack\CardTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall HandValueCalculator::a(void)" (?a#HandValueCalculator##QAEXXZ) referenced in function "void __cdecl Test(void)" (?Test##YAXXZ)
Your HandValueCalculator does not have a void a(); implementation available to the linker. If the a function is defined, make sure you link with the object file containing the definition.
However, you're most likely the victim of the most vexing parse and think you've declared a to be a variable (somewhere not shown), but you've instead declared a function (without definition).
I wrote a simple class which manages the current execution session. Therefore, I decided to use a singleton pattern but the compilation crashes at linked step. This is the error:
error LNK2005: "class Session * session" (?session##3PAVSession##A) already defined in ClientTsFrm.obj
(...)client\FrmSaveChat.obj TeamTranslate
error LNK2005: "class Session * session" (?session##3PAVSession##A) already defined in ClientTsFrm.obj
(...)client\Login.obj TeamTranslate
error LNK2019: unresolved external symbol "protected: __thiscall Session::Session(void)" (??0Session##IAE#XZ)
referenced in function "public: static class Session * __cdecl Session::Instance(void)" (?Instance#Session##SAPAV1#XZ)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_nick" (?_nick#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_service" (?_service#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_serverAddress" (?_serverAddress#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_googleAPI" (?_googleAPI#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_language" (?_language#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static int Session::_numbLanguageSelected" (?_numbLanguageSelected#Session##0HA)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_translationEngine" (?_translationEngine#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK1120: 8 unresolved externals
(...)client\bin\TeamTranslate.exe TeamTranslate
IntelliSense: identifier "wxZipStreamLink" is undefined
(..)\include\wx\zipstrm.h 417 5
session.h (singleton)
#ifndef __SESSION_H__
#define __SESSION_H__
#include <cstring>
#include <stdio.h>
class Session {
public:
static Session* Instance();
void setNick(char* nick);
const char* getNick();
void setService(char* serv);
const char* getService();
void setLanguage(char* lang);
const char* getLanguage();
const char* getServerAddress();
void setServerAddress(char *sv);
void setGoogleAPIKey(char* code);
const char* getGoogleAPIKey();
void setNumbLanguageSelected(int v);
int getNumbLanguageSelected();
const char* Session::getTranslationEngine();
void Session::setTranslationEngine(char *sv);
char* Session::getGoogleURLTranslation();
void update();
bool read();
protected:
Session();
private:
static Session* _instance;
static const char* _nick; // client nickname
static const char* _service; // service used to translation (Google, Bing,....)
static const char* _serverAddress;
static const char* _googleAPI;
static const char* _language;
static int _numbLanguageSelected;
static const char* _translationEngine;
};
#endif
Session.cpp
#include "Session.h"
Session* Session::_instance = 0;
Session* Session::Instance() {
if (_instance == 0) {
_instance = new Session;
_instance->read();
}
return _instance;
}
void Session::setGoogleAPIKey(char* code){
_googleAPI = strdup(code);
}
const char* Session::getGoogleAPIKey(){
return _googleAPI;
}
void Session::setLanguage(char* lang){
_language = strdup(lang);
}
const char* Session::getLanguage(){
return _language;
}
void Session::setNick(char* nick){
_nick = strdup(nick);
}
const char* Session::getNick(){
return _nick;
}
void Session::setService(char* serv){
_service = strdup(serv);
}
const char* Session::getService(){
return _service;
}
const char* Session::getServerAddress(){
return _serverAddress;
}
void Session::setServerAddress(char *sv){
_serverAddress = strdup(sv);
}
void Session::setNumbLanguageSelected(int v){
this->_numbLanguageSelected = v;
}
int Session::getNumbLanguageSelected(){
return _numbLanguageSelected;
}
const char* Session::getTranslationEngine(){
return _translationEngine;
}
void Session::setTranslationEngine(char* sv){
_translationEngine = strdup(sv);
}
void Session::update(){
FILE* config = fopen("conf\\config.txt", "w");
fprintf(config, "%s\n", _serverAddress);
fprintf(config, "%s\n", _nick);
fprintf(config, "%d\n", _numbLanguageSelected);
fprintf(config, "%s\n", _language);
fprintf(config, "%s", _translationEngine);
fflush(config);
fclose(config);
}
bool Session::read(){
FILE * config;
if (config = fopen("conf\\config.txt", "r"))
{
fscanf(config, "%s", _serverAddress);
fscanf(config, "%s", _nick);
fscanf(config, "%d", _numbLanguageSelected);
fscanf(config, "%s", _language);
fscanf(config, "%s", _translationEngine);
fflush(config);
fclose(config);
return true;
} else
return false;
}
char* Session::getGoogleURLTranslation(){
return "https://www.googleapis.com/language/translate/v2?key=";
}
Example about how I call the singleton:
#include "../data/Session.h"
static Session* session = Session::Instance();
Can you help me to fix the errors?
thanks in advance.
One of your headers (which you didn't show, but it is included in both "ClientTsFrm.cpp" and "FrmSaveChat.cpp") defines a variable called "session" of type Session*.
The other errors are caused by your forgetting to define most static members of Session.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
Main class
int main() {
initCarList();
}
void initCarList() {
List<Car> carList;
Car c1 = Car("Toyota", "Bettle", 5);
carList.add(c1);
Car c2 = Car("Mercedes", "Bettle", 7);
carList.add(c2);
Car c3 = Car("FireTruck", "Large Van", 20);
carList.add(c3);
Car c4 = Car("Puma", "Saloon Car", 10);
carList.add(c4);
}
List class
#include "List.h"
#include <iostream>
using namespace std;
template <typename ItemType>
class List {
private:
ItemType itemList[10];
int size;
public:
List();
void add(ItemType);
void del(int index);
bool isEmpty();
ItemType get(int);
int length();
};
template<typename ItemType>
List<ItemType>::List() {
size = 0;
}
template<typename ItemType>
void List<ItemType>::add(ItemType item) {
if(size < MAX_SIZE) {
itemList[size] = item;
size++;
} else {
cout << typename << " list is full.\n";
}
}
I got errors like these
Error 3 error LNK2019: unresolved external symbol "public: void
__thiscall List::add(class Car)" (?add#?$List#VCar####QAEXVCar###Z) referenced in function "void
__cdecl initCarList(void)" (?initCarList##YAXXZ) C:\Users\USER\Desktop\New
folder\DSA_Assignment\main.obj DSA_Assignment
Did I do anything wrongly in my code? NEED HELP THANKS!
There is a syntax error (cout << typename ) in your code. I don't know how you got the linker error. May be its not being compiled at all.
otherwise its okay http://ideone.com/PGWGZu
Clearly you did as it doesn't work! Flippancy aside, let's take a look at the error message bit by bit:
Error 3 error LNK2019: unresolved external symbol
So this is a linkage error. The linker is trying to put together the units that were individually compiled together, but in this case it can't find an external symbol - usually a function or variable name.
"public: void __thiscall List::add(class Worker)" (?add#?$List#VWorker####QAEXVWorker###Z)
This is the full signature of the function that you're missing. It's name manged unfortunately but with your context knowledge of the code that you're writing, you should be able to tell that it's:
void List::add(Worker)
The next bit ...
referenced in function "void __cdecl initWorkerList(void)" (?initWorkerList##YAXXZ) C:\Users\USER\Desktop\New folder\DSA_Assignment\main.obj DSA_Assignment
... is telling you where the problem is happening, i.e where in the code it's trying to link, there is a reference to the missing function. Again, after demangling it's in:
void initWorkerList()
As you can see, with a bit of graft, you can determine exactly what you've done wrong here. Hope this helps.
I have the following project files:
//connections.cpp
#include "stdafx.h"
#include "LibraryHeaders.h"
#include "FileManager.h"
#define WSAVersion 0x202
#define GSMsgID 0x100
extern HWND Main_hWnd;
bool InitConnections ()
{
FileManager::ConnectFile *connectfile = FileManager::ReadConnectFile(connectfile);
SockBase GSConnection(WSAVersion, TCP, connectfile->GS_IP, connectfile->GS_Port, Main_hWnd, GSMsgID);
if (GSConnection.Connect() != true) {return false;}
return true;
}
//FileManager.cpp
#include "stdafx.h"
#include "FileManager.h"
#include "LibraryHeaders.h"
using namespace FileManager;
ConnectFile* ReadConnectFile(ConnectFile *ConnectStruct)
{
FileLibrary connectfile("DMOConnection.cfg");
if (connectfile.OpenFile(HEAP, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, PAGE_READWRITE) != true) {return false;}
ConnectStruct->GS_IP = connectfile.parser->str_GetToken((char*)connectfile.FileBuff);
if (ConnectStruct->GS_IP == (const char*) -1) {return false;}
ConnectStruct->GS_Port = connectfile.parser->int_GetToken((char*)connectfile.FileBuff);
if (ConnectStruct->GS_Port == -1) {return false;}
return ConnectStruct;
}
//FileManager.h
namespace FileManager
{
struct ConnectFile
{
const char* GS_IP;
unsigned int GS_Port;
};
ConnectFile* ReadConnectFile(ConnectFile*);
}
And when trying to build the project i got this error:
Connections.obj : error LNK2019: unresolved external symbol "struct FileManager::ConnectFile * __cdecl FileManager::ReadConnectFile(struct FileManager::ConnectFile *)" (?ReadConnectFile#FileManager##YAPAUConnectFile#1#PAU21##Z) referenced in function "bool __cdecl InitConnections(void)" (?InitConnections##YA_NXZ)
I dont understand why, the linker should look up and see that ive defined FileManager::ReadConnectFile on FileManager.cpp but it doesnt, any tip how to fix this?
You're defining a free function:
ConnectFile* ReadConnectFile(ConnectFile *ConnectStruct)
not a member:
ConnectFile* FileManager::ReadConnectFile(ConnectFile *ConnectStruct)
Totally different.
Also:
using namespace FileManager;
and
error LNK2019: unresolved external symbol "struct FileManager::ConnectFile [...]
suggests you have a namespace FileManager and a struct FileManager... any reason for using the same name?
i fixed it by declaring the function out of the namespace:
namespace FileManager
{
struct ConnectFile
{
const char* GS_IP;
unsigned int GS_Port;
};
}
using namespace FileManager;
ConnectFile* ReadConnectFile(ConnectFile *ConnectStruct);
The IDE is VC11 Beta, thanks for the answers.
The code
using namespace FileManager;
ConnectFile* ReadConnectFile(ConnectFile *ConnectStruct)
{ ...some definition...}
defines the ReadConnectFile function not in the namespace FileManager, but in global namespace.