Basic C++ Linking Help

Apprentice
Posts: 9
Joined: 2009.07
Post: #1
The basic structure of my game right now is that I have a main program that handles input, draws, etc. and it contains/updates views and menus from the classes I have.

So I have an array of Menu and View objects that are updated generically with virtual functions like iterate() and draw().

Now, I am getting into trouble because one of the View objects needs data about a separate View object. The top-down design is no longer going to hold up.

I tried making a function in the main file, then using it in a class, but I get a duplicate symbol error in XCode. How do you take care of this?

UPDATE::
I am getting sick and tired of this. Every time I link to a new file, I need to make the extern statements, try to figure out the error, the end up quitting and redefining the necessary functions in the file with a different name. There has to be a better way. For example, I made a a testRectIntersection(Rect r1, Rect r2) function, and when I try to import the file containing it from two different files, I get an error. How do people do it!!?!?!??!!?


I am seriously frustrated. These errors make texturing impossible and simple functions super-difficult.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #2
Quote:I tried making a function in the main file, then using it in a class, but I get a duplicate symbol error in XCode. How do you take care of this?

Well, for starters, if you're declaring something in main.cpp and then using it in a class, you're not understanding the purpose of classes.

Secondly, the general strategy is:

Put class implementations/function definitions in .cpp files
Put class declarations and function prototypes in .h files
Put include guards around the .h files
Include the .h files in the .cpp files, including the minimum number of files needed. If absolutely necessary, you can include a .h file in another .h file if one needs to know about the other, but only do that when necessary.

A more advanced topic is using forward declarations to avoid the need to to include .h files inside another .h file, but ignore that until you get more comfortable with it.

It looks like this:

Myclass.h file:
----------------
#ifndef MY_CLASS_H
#define MY_CLASS_H

class Myclass
{
int x;
int y;

int addXandY();
};

Myclass.cpp file
--------------
#include myclass.h

int Myclass::addXandY()
{
return x+y;
}
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2009.07
Post: #3
Thank you for giving me a reply, despite the low quality of the question. I read the question asking guide a few minutes ago and feel horrible just for posting this.

I eventually found a different work-around, but the problem still exists to a certain extent.

For example, my "main" file (that sets up GLUT event handling, etc) would want to use functions from a texturing file, and one of my classes would also want to variables to get the loaded textures. My main file does have a .h and a .cpp, and when I included the texture file in main.h (and used extern), it worked. However, when I included the same file in my view, still using extern, I got that dreaded duplicate symbol error.

Once again, thank you for even trying on that horrible question. Even this one is not so great. (I'll work on it Smile)
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #4
On the question asking front, I'd suggest that you spend less time describing your code and just show it to us (always copy and paste to avoid introducing new errors or even inadvertently fixing old ones; and use code tags!) Smile

With regards to your duplicate symbols, did you declare the variable in your header file? It should be declared in the cpp file (exactly once) and then referenced from other cpp files using extern (you can do this in a header file as long as the extern declaration can't be seen by the same cpp file the variable is actually declared in. Like so:
Code:
//class1.h

#ifndef CLASS1_H
#define CLASS1_H

#ifndef CLASS1_CPP
extern int globalVariable;    //this declaration can't be seen from within class1.cpp

class class1
{
int x;
int y;

int addXandY();
};

Code:
//class1.cpp

#define CLASS1_CPP
#include class1.h

int globalVariable;

int class1::addXandY()
{
return x+y;
}

Code:
//class2.cpp

#include "class2.h"
#include "class1.h"

//class2 goes here...
Quote this message in a reply
Post Reply