I have a class that has a static member, which I want to use in the class constructor, but the code doesn't compile, and I'm left with these errors:
fatal error LNK1120: 1 unresolved externals
error LNK2001: unresolved external symbol "protected: static class Collection A::collection"
Any help will be appreciated.
Thanks.
a.h:
class A
{
protected:
static Collection<A*> collection;
};
a.cpp:
A::A() {
A::collection.push_back(this);
}
You need to add
Collection<A*> A::collection;
to your a.cpp file.
In your .cpp you need to add:
Collection<A*> A::collection;
The .h only declared that there would be a copy somewhere. You need to provide that copy in the .cpp.
alternatively, if you don't want to put that line in a cpp file, you can use a static method which returns a reference to a static instance... i.e.
class A
{
public:
static Collection<A*>& collection()
{
static Collection<A*> singleInstance;
return singleInstance;
}
};
Related
I have a C++ project that won't compile, and the following 2 errors are produced:
Error LNK1120 1 unresolved externals
Error LNK2019 unresolved external symbol "public: virtual __cdecl
StartWindow::~StartWindow(void)" (??1StartWindow##UEAA#XZ) referenced
in function "public: void __cdecl StartWindow::`vbase
destructor'(void)" (??_DStartWindow##QEAAXXZ)
StartWindow is a class I have defined, but currently it is never instantiated or included anywhere in the project. Deleting the class allows the project to compile, but if this class is within the project it won't.
I will include the code for the class in case I am missing something:
.CPP File
#include "StartWindow.h"
StartWindow::StartWindow()
{
setImmediateDrawMode(false);
}
void StartWindow::onDraw()
{
clearScreen(WHITE);
EasyGraphics::onDraw();
}
Header File:
#pragma once
#include "EasyGraphics.h"
class StartWindow : public EasyGraphics
{
public:
StartWindow();
~StartWindow();
private:
virtual void onDraw();
};
Thanks.
You're missing the implementation for the destructor for StartWindow. In your implementation file (.cpp file), append:
StartWindow::~StartWindow(){
//if your destructor is non-trivial, include definition here
}
I am new to c++. I started now playing with classes and I am having a noob problem with statics.
class Test
{
public:
Test(){};
~Test(){};
static void test();
static Helper* helper;
};
void Test::test()
{
Object obj = Test::helper->getObject();
//...
}
When I try to compile it gives the error:
main.obj : error LNK2001: unresolved external symbol "public: static class Helper* Test::helper" (?helper#Test##2PAVHelper##A)
What is wrong with my code?
The first answer is correct. The reason behind this is that you need to allocate memory for static objects outside the class definition. If you define the the class in a header file, and include it in several cpp files, the compiler doesn't know where and how you want to create the object that 'helper' points to.
you need to define Test::helper. Write something like this outside the class:
Helper* Test::helper = new Helper;
Here is my simple setup: (i've hidden lots of unneeded information)
//AutoFocusTest.h
#include "camAVTEx.h"
class CAutoFocusTestApp : public CWinApp
{
protected:
camera_t* mCamera;
public:
virtual BOOL InitInstance();
};
//camAVTEx.h
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
//member variables
//member function declarations
}
//camAVTEx.cpp
#include "camAVTEx.h"
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
//stuff
}
camera_avtcam_ex_t::~camera_avtcam_ex_t()
{
//stuff
}
//the rest are defined here in my program
//AutoFocusTest.cpp
#include AutoFocusTest.h
BOOL CAutoFocusTestApp::InitInstance()
{
mCamera = new camera_avtcam_ex_t();
}
This setup produces the error:
3>AutoFocusTest.obj : error LNK2001: unresolved external symbol
"public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)"
(??0camera_avtcam_ex_t##QEAA#XZ)
From everything I've read on this relatively common problem, I have not linked something causing my camera_avtcam_ex_t function definitions to not be found. However, I can't figure out what I could have missed. I have added all of the include directories and library directories, as well as added the library files to the additional dependencies section.
Can anyone spot anything that I might be missing?
Assuming you have defined the constructor for your camera_avtcam_ex_t, it's declared as private, you can't instantiate it.
This is best explained by the code itself. I want to use a QList<MyStruct> as a static member, but get a linker error 2001 (LNK2001). The code is in the same order in file MyClass.h
struct MyStruct{
double x;
...
};
typedef QList<MyStruct> MyStructList;
class MyClass
{
....
private:
static MyStructList _myValues; // does not work => LNK2001
MyStructList _myValues; // Test 1 OK
static QList<int> _myValues; // Test 2 OK
Any hints? Eran's answer (see comment below) is the right hint. SOLVED
LNK2001:
error: LNK2001: unresolved external symbol "private: static class QList<struct>
Yet another case of forgotten static member definition... MyStructList MyClass::_myValues should have been placed in one of the project's .cpp files.
I was trying to incorporate the Singleton design pattern into my code, but I started getting a weird error:
main.obj : error LNK2005: "private: static class gameState * gameState::state" (?state#gameState##0PAV1#A) already defined in gameState.obj
If you're not familiar with the singleton pattern, it is basically used to enforce only 1 instance of a certain object in the entire program.
Here is the relevant code:
gameState.h:
class gameState
{
public:
static gameState* Instance() {return state;}
.
.
.
private:
gameState();
static gameState* state;
};
gameState* gameState::state = new gameState();
and right now I'm just using the instance of that object in the main.cpp file:
gameState *currState = gameState::Instance();
.
.
.
for_each(currState->getHumanPieces().begin(),currState->getHumanPieces().end(), drawPieces);
It would seem I am trying to redefine gameState::state, but can't figure out why... help anyone?
that solved that, but one error still remains, which I didn't actually post before as I thought it was just part of the other one:
error LNK2019: unresolved external symbol "private: __thiscall gameState::gameState(void)" (??0gameState##AAE#XZ) referenced in function "void __cdecl `dynamic initializer for 'private: static class gameState * gameState::state''(void)" (??__E?state#gameState##0PAV1#A##YAXXZ)
any good tip on how to fix that one as well?
Thanks both of you, its fixed :D
You need to put the definition of the static gameState* into exactly one source file, i.e. this line:
gameState* gameState::state = new gameState();
If you put it in a header which is included by multiple source files, each has a definition of gameState::state which leads to errors at link-time.
For the follow-up problem, use Vadakkumpadaths advice: you need to provide a definition for gameStates constructor, not only a declaration.
Add definition for your constructor to fix second linker error.
private:
gameState()
{
}
Use a header guard macro for the redefinition problem, and explicitly define your private constructor.