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
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.
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).
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.
error LNK2001: unresolved external symbol "public: static int WrappedVector::_N" (?_N#WrappedVector##2HA)
header.h
struct WrappedVector
{
static int _N;
double *_x;
};
main.cpp
const int WrappedVector::_N = 3;
i don't understand whats wrong
Just change the definition
int WrappedVector::_N = 3; // Note no const
see LIVE DEMO1
or the declaration
struct WrappedVector {
static const int _N;
// ^^^^^
double *_x;
};
see LIVE DEMO2
consistently.
If you need the latter form (static const int) you can also initialize it directly in the declaration:
struct WrappedVector {
static const int _N = 3;
// ^^^
double *_x;
};
see LIVE DEMO3
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
In my VS2010 DLL project i have an error, when compiling this project: LNK 2001 unresolved external on pFuncs member of my class:
class Foo
{
// ...
static NPPluginFuncs * pFuncs;
// ...
};
Here a struct, defined in npfunctions.h:
typedef struct _NPPluginFuncs {
uint16_t size;
uint16_t version;
NPP_NewProcPtr newp;
NPP_DestroyProcPtr destroy;
NPP_SetWindowProcPtr setwindow;
NPP_NewStreamProcPtr newstream;
NPP_DestroyStreamProcPtr destroystream;
NPP_StreamAsFileProcPtr asfile;
NPP_WriteReadyProcPtr writeready;
NPP_WriteProcPtr write;
NPP_PrintProcPtr print;
NPP_HandleEventProcPtr event;
NPP_URLNotifyProcPtr urlnotify;
void* javaClass;
NPP_GetValueProcPtr getvalue;
NPP_SetValueProcPtr setvalue;
NPP_GotFocusPtr gotfocus;
NPP_LostFocusPtr lostfocus;
NPP_URLRedirectNotifyPtr urlredirectnotify;
NPP_ClearSiteDataPtr clearsitedata;
NPP_GetSitesWithDataPtr getsiteswithdata;
NPP_DidCompositePtr didComposite;
} NPPluginFuncs;
Static members of a struct (or class) are declared in the typedef struct { }; and needs to be defined explicitly once:
#include "npfunctions.h"
Foo::pFuncs = NULL; // optional initialization
int main()
{
Foo::pFuncs = new NPPluginFuncs;
}
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.
Error 1 error LNK2019: unresolved external symbol "bool __cdecl
prefix(unsigned int,unsigned int)" (?prefix##YA_NII#Z) referenced in
function _main c:\Users\Work\documents\visual studio
2012\Projects\Book\Project5\Project5\Source.obj Project5
Error 2 error LNK1120: 1 unresolved
externals c:\users\work\documents\visual studio
2012\Projects\Book\Project5\Debug\Project5.exe 1 1 Project5
I just..I dont even know what I have to ask you guys. How do I fix this?
This is the code:
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
void citire(vector<unsigned int> myVector,int &nrElem);
bool prefix(unsigned int nr1,unsigned int nr2);
int main(){
int nrElem={0};
vector<unsigned int> myVector;
//citire(myVector,nrElem);
cout << prefix(123,1234);
system("pause");
return 0;
}
void citire(vector<unsigned int> myVector,int &nrElem){
cout << "NumarElemente=" ;
cin >> nrElem ;
for(int i=0;i<nrElem;i++){
unsigned int nrCitit;
cout << "Elem #" << i+1 <<"=";
cin >> nrCitit;
myVector.push_back(nrCitit);
};
for(int i=0;i<nrElem;i++){
cout << myVector.at(i);
};
}
bool prefix(unsigned int &nr1,unsigned int &nr2){
unsigned int nr1copy=nr1;
unsigned int nr2copy=nr2;
int digitsNr1 = 0; while (nr1copy != 0) { nr1copy /= 10; digitsNr1++; }
int digitsNr2 = 0; while (nr2copy != 0) { nr2copy /= 10; digitsNr1++; }
if ( nr2/_Pow_int(10,digitsNr2-digitsNr1)==nr1) {return true;}
else return false;
}
bool prefix(unsigned int nr1,unsigned int nr2);
is not same as
bool prefix(unsigned int& nr1,unsigned int &nr2);
In forward forward declaration, you are taking the parameters by value but in the definition it is by reference. Keep the argument types same in the declaration and the definition.
unresolved external symbol "bool __cdecl prefix(unsigned int,unsigned int)"
Usually when you see these kind of linker errors, the first thing you need to check is if the function's declaration and definition signatures match or not. In this case, it is clearly not.
Declaration:
bool prefix(unsigned int nr1,unsigned int nr2);
definition:
bool prefix(unsigned int &nr1,unsigned int &nr2){ ... }
See the difference? Both should be the same. Looking at your code, it looks like you should keep the version in the declaration.
You have a prototype for the prefix() function:
bool prefix(unsigned int nr1,unsigned int nr2);
which has a signature that differs from the implementation given below:
bool prefix(unsigned int &nr1,unsigned int &nr2) {
^^^ ^^^
....
}
Note that in the prototype, the nr1 and nr2 parameters are passed by value; instead, in the implementation signature, they are passed by reference (note the &).
Both prototype and implementation signatures should match. Fix the wrong one.
(Note: since you can't pass literals like the 123 in main() as non-const reference, I think the wrong one is the implementation signature, i.e. drop the & in the implementation signature).