Error: undefined reference to...

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!
Last edited on
extern Class1 newClass[];
You tell that there is global array named newClass somewhere, but don't actualy define it anywhere. So there is an error when you trying to access it.
Last edited on
I thought I defined it in main()
 
Class1 newClass[4];
Last edited on
MiiNiPaa wrote:
You tell that there is global array named newClass somewhere
You should pass it to function (what you actually should do and do not use globals) if you want to have it in some other scope.
Okay, I see what you mean about it being global. It needs to be defined outside of main.

And interesting on passing it to a function, I hadn't thought of that.
Just so I'm clear on how to do it...
So, for example, instead of:

1
2
3
4
5
void Class2::change_num()
{
    otherx = newClass[1].x;
    othery = newClass[1].y;
}


I'd do something like this:

1
2
3
4
5
void Class2::change_num()
{
    otherx = get_var(1);
    othery = get_var(2);
}


Then make this in main.cpp:

1
2
3
4
5
6
7
8
void get_var(int which)
{
    switch (which)
    {
          case 1:  return newClass[1].x;
          case 2:  return newClass[1].y;
    }
}


And why not use global variables? It seems like a lot of extra steps and code to create a function to pass each variable instead of just being able to tell it which variable you want.
Nope
http://www.cplusplus.com/doc/tutorial/functions/
http://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/
You should use something like
1
2
3
4
5
6
7
8
9
void Class2::change_num(const Class1& c)
{
    otherx = c.x;
    othery = c.y;
}
//and use like:
Class1 newClass[4];
Class2 newClass2;
newClass2.change_num(newClass[1]);


About globals:
In short, globals hampers optimization, introduces excessive dependencies and making program state harder to predict ad debug.
http://www.parashift.com/c++-faq/global-vars.html
http://c2.com/cgi/wiki?GlobalVariablesAreBad
http://stackoverflow.com/questions/484635/are-global-variables-bad
http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil
Last edited on
No, it's not really a lot of extra steps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

class Class1
{
    public:
        int x;
        int y;
};

class Class2
{
public:

    int otherx;
    int othery;

    void change_num(const Class1&);
};

void Class2::change_num(const Class1& c1)
{
    otherx = c1.x ;
    othery = c1.y ;
}

int main()
{
    Class1 newClass[4] ;
    for ( int i=0; i<4; ++i )
    {
        newClass[i].x = i ;
        newClass[i].y = i ;
    }

    Class2 c2 ;
    for ( int i=0; i<4; ++i )
    {
        c2.change_num(newClass[i]) ;
        std::cout << '(' << c2.otherx << ", " << c2.othery << ")\n" ;
    }
}
Ahah! Cire, I had seen that somewhere before but had never really paid attention to it. That makes a lot of sense. Thanks for that example. I'll implement it and see if I can get it off the ground. :)

And MiiNiPaa, that makes sense on global variables after reading over those. Seems like a good practice to avoid from the start.

Now, are global functions acceptable?
MilesAway1980 wrote:
Now, are global functions acceptable?
Of course they do. Most of the standard library is made of those.
Though Java begs to differ
Last edited on
Topic archived. No new replies allowed.