Undefined reference? o.0

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Undefined reference? o.0

Post by Ginto8 »

I have a problem. Since I segfaulted pong ( :lol: :oops: :shock2: :shock: :roll: ), 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();
};

:roll: This occurs at each and every point that I try to do something with them. Any suggestions as to what I'm doing wrong? :worship:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
sparda
Chaos Rift Junior
Chaos Rift Junior
Posts: 291
Joined: Tue Sep 23, 2008 3:54 pm

Re: Undefined reference? o.0

Post 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.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Undefined reference? o.0

Post 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 ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Undefined reference? o.0

Post 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
Last edited by MarauderIIC on Sun Feb 15, 2009 1:54 pm, edited 1 time in total.
Reason: misread, my bad.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Undefined reference? o.0

Post by M_D_K »

already told him that.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply