Hi all, new to the forums.
Been learning C++ lately and have been converting some of my older QBasic games into C++ for practice and am having a blast learning and updating them with OOP.
However, lately, I've been working on breaking up one of my larger programs into multiple files. I was able to work most of the kinks out, but no matter what I do, I just can't seem to get past an "undefined reference to" error.
I put together some sample code that essentially does the same thing I want to do and gets the same error. Note that this doesn't actually DO anything.
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include "class1.h"
#include "class2.h"
#include "global.h"
int main()
{
Class1 newClass[4];
for (int i = 0; i < 4; i++)
{
newClass[i].x = 1;
newClass[i].y = 1;
}
return 0;
}
|
class1.h
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#ifndef CLASS1_H_INCLUDED
#define CLASS1_H_INCLUDED
class Class1
{
public:
int x;
int y;
Class1();
};
#endif
|
class1.cpp
1 2 3 4 5 6 7
|
#include "class1.h"
Class1::Class1()
{
x = 0;
y = 0;
}
|
class2.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef CLASS2_H_INCLUDED
#define CLASS2_H_INCLUDED
class Class2
{
public:
int otherx;
int othery;
void change_num();
};
#endif
|
class2.cpp
1 2 3 4 5 6 7 8 9
|
#include "class1.h"
#include "class2.h"
#include "global.h"
void Class2::change_num()
{
otherx = newClass[1].x;
othery = newClass[1].y;
}
|
global.h
1 2 3 4 5 6 7 8
|
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
#include "class1.h"
extern Class1 newClass[];
#endif
|
It seems no matter what order of header file rearrangement, etc. I get the following error in
class2.cpp:
1 2 3 4
|
obj\Debug\class2.o||In function `ZN6Class2C2Ev':|
C:\C Programs\Tutorials\test\class2.cpp|7|undefined reference to `newClass'|
C:\C Programs\Tutorials\test\class2.cpp|8|undefined reference to `newClass'|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
|
Why doesn't it have a reference to newClass? Wouldn't including "global.h" reference it?
Thanks for any help, I appreciate it!