Global instance error.
Sep 13, 2014 at 1:57am UTC
I've been trying to create a global instance of a class but when i use it i get an "Unresolved external symbol" error and i can't figure out why. This is how it's set up.
MyClass.h
1 2 3 4 5 6 7 8 9 10
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass
{
public :
void func();
};
#endif
MyClass.cpp
1 2 3 4 5 6
#include "MyClass.h"
void MyClass::func()
{
int x = 1;
}
Global.h
1 2 3 4 5 6 7 8 9 10 11
#ifndef GLOBAL_H
#define GLOBAL_H
#include "MyClass.h"
namespace Global
{
extern MyClass g_myClass;
}
#endif
Global.cpp
1 2 3 4 5 6
#include "Global.h"
namespace Global
{
MyClass g_MyClass;
}
main.cpp
1 2 3 4 5 6 7
#include "Global.h"
int main()
{
Global::g_myClass.func();
return 0;
}
Everything is fine until i try to call func() then i get this error,
LNK2001: unresolved external symbol "class MyClass Global::g_myClass"
Anyone know what i'm doing wrong? Thank you
Sep 13, 2014 at 2:16am UTC
Is Global.cpp being compiled? It needs to be part of the project.
Topic archived. No new replies allowed.