Page 1 of 1
Undefined reference? o.0
Posted: Sun Feb 15, 2009 8:15 am
by Ginto8
I have a problem. Since I segfaulted pong (
), I decided to take a step back and make tic tac toe. But now I've run into another problem. When I am compiling it seems to do fine, until it starts linking the executable. When it gets to that point, it keeps saying that there are undefined references to Sprite::images and Sprite::filenames, which are here:
Click here to see the hidden message (It might contain spoilers)
Code: Select all
class Sprite
{
static vector<string> filenames;
static vector<sf::Image> images;
sf::Sprite sprite;
static int arrayLocation(string filename);
static void loadImage(string filename);
public:
Sprite();
Sprite(string filename, float x = 0, float y = 0);
Sprite(string filename, sf::Vector2f coords);
void load(string filename, float x = 0, float y = 0);
void load(string filename, sf::Vector2f coords);
void coords(float x, float y);
void coords(sf::Vector2f coords);
sf::Vector2f coords();
void show();
};
This occurs at each and every point that I try to do something with them. Any suggestions as to what I'm doing wrong?
Re: Undefined reference? o.0
Posted: Sun Feb 15, 2009 8:58 am
by sparda
Dude, you have to be very careful when you're introducing new files into the program.
This used to happen to me all the time when I was first learning C++. The linker will bitch if you fuck up the references. There is a certain way to write your program when it contains a shit-load of files, that can help you avoid a confusing mess of unreferenced calls; and it is very strict. You have your one-and-only "application file," then you have a multitude of "translation units" that link with the main application file. The translation units are, in the most simple sense (it can get more complicated), an interface and implementation file. Ultimately all your translations units should link correctly with you application file in a compilation unit. You can think of the whole situation as a tree graph, where your root node is the file with your main() function.
A simple google will help you with the specifics of this.
The usual format will go something like this:
Code: Select all
main.cpp________________________________________________________
| |____________ | |
| | | |
header(1).hpp implem(1).cpp . . . . . header(n) implem(n)
(There can also be an optional main.h that includes all the other files)
Like I said, this is the simplest case. In which there are not subtrees of header and implementation files, as well as no interchanging includes, in which you have to watch your ass. Hopes that solves your problem.
Re: Undefined reference? o.0
Posted: Sun Feb 15, 2009 10:23 am
by programmerinprogress
I usually put all my class declarations into separate header files (or several sometimes if they are related, or inherited), then I make separate implementation files, and I never have any problems.
I take measures to prevent multiple redefinition, normally with #pragma once
So i'll have my main.cpp, and then any preprocessor commands at the top, each implementation file has an #include for the header file it matches with, and everything is fine and dandy
Re: Undefined reference? o.0
Posted: Sun Feb 15, 2009 1:46 pm
by MarauderIIC
Okay, to REALLY answer your question: The problem is because it's static. And static members aren't initialized when the class is since they don't belong to an instance of the class. In your sprite.cpp file, add:
Code: Select all
vector<string> Sprite::filenames;
vector<sf::Image> Sprite::images;
Thanks
internet
Re: Undefined reference? o.0
Posted: Sun Feb 15, 2009 2:40 pm
by M_D_K
already told him that.