Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Vector push_back call is an undefined reference  (Read 4535 times)

0 Members and 1 Guest are viewing this topic.

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
[SOLVED] Vector push_back call is an undefined reference
« on: December 12, 2012, 02:29:44 am »
Hi, I'm attempting to create an animation system which is essentially just a dumbed down version of what is in the Thor library.

I have not coded much yet at all but have run into an error that I cannot resolve.

First, I have a header of a class I am calling "Animator". It looks like this:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

class Animator
{
    private:

    // The following is a structure containing information about a frame. Right now it is just an arbitrary float
    // which will be the frame duration and a subrectangle which will be the frame on the sprite sheet.
    struct Frame
    {
        Frame(float duration, const sf::IntRect& subrect);

        mutable float duration;
        sf::IntRect subrect;
    };

    // Here is a vector of frames which will compose an animation.
    std::vector<Frame> mFrames;

    public:
    Animator();
    ~Animator();

    // And a function prototype to add a frame to the vector.
    void addFrame(float duration, const sf::IntRect& subrect);
};

Here is the implementation:

#include "animate.h"

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

Animator::Animator()
{
}

Animator::~Animator()
{
}

// addFrame function. All I'm doing here is pushing back the information gained through the parameters
// in the vector
void Animator::addFrame(float duration, const sf::IntRect& subrect)
{
    mFrames.push_back(Frame(duration, subrect));
}

Obviously there isn't much here. I've just ripped off Bromeon's AddFrame stuff. Even so, when I try to compile this Code::Blocks gives me an error:

Code: [Select]
obj\Debug\animate.o:C:\Users\user\Documents\CodeBlocks\Projects\SFML1\animate.cpp|17|undefined reference to `Animator::Frame::Frame(float, sf::Rect<int> const&)'|
I've never really used containers before, so it's probably some dumb syntax mistake I've made. I can't figure it out though; it looks exactly like what is in the Thor library as far as I can tell.
« Last Edit: December 13, 2012, 11:33:36 pm by Rabees »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Vector push_back call is an undefined reference
« Reply #1 on: December 12, 2012, 02:52:22 am »
Well then take again a closer look at Thor's code... ;)

You don't want to define the struct private within the class, but you want to have it 'external', see here.

And if you put it into another namespace as Thor does then make sure to call it correctly, see here.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #2 on: December 12, 2012, 03:11:45 am »
Strange, in the version I was looking at (1.1) it was declared as a private member of the class.

        // ---------------------------------------------------------------------------------------------------------------------------
        // Private types
        private:
                // Frame with sub-rectangle and duration
                struct Frame
                {
                                                                                        Frame(float duration, ResourcePtr<const sf::Texture> texture, const sf::IntRect& subrect);

                        mutable float                                   duration;
                        sf::IntRect                                             subrect;
                        ResourcePtr<const sf::Texture>  texture;
                };

Anyway, is there a particular place this struct should be? I tried sticking it just outside the class definition, and I am having the same error.

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

struct Frame
{
    Frame(float duration, const sf::IntRect& subrect);

    mutable float duration;
    sf::IntRect subrect;
};

class Animator
{...

edit: I'll mention I am also not very experienced with using structs :P
« Last Edit: December 12, 2012, 03:15:10 am by Rabees »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #3 on: December 12, 2012, 03:17:56 am »
undefined reference to `Animator::Frame::Frame(float, sf::Rect<int> const&)

Generally that's a linker error indicating that the function's body is not defined, or that the source file it is defined in isn't included in the compilation/linking process.


Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #4 on: December 12, 2012, 03:37:43 am »
Well, I have two files for my animator class, animate.h and animate.cpp, and I'm pretty sure I have defined the function's body here in animate.cpp:

void Animator::addFrame(float duration, const sf::IntRect& subrect)
{
    mFrames.push_back(Frame(duration, subrect));
}

and at the top of animate.cpp I have

#include "animate.h"

so it should be included, no? The addFrame function's prototype is also certainly declared in animate.h

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #5 on: December 12, 2012, 04:17:59 am »
Animator::addFrame is not the function referred to as undefined.

That would be the constructor for Frame.
« Last Edit: December 12, 2012, 04:19:30 am by cire »

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #6 on: December 12, 2012, 04:19:32 am »
There is no other function aside from the constructor and destructor for Animator. It appears to be saying the Frame struct is undefined.

Edit: Oh, is it perhaps this line inside of the struct?

Frame(float duration, const sf::IntRect& subrect);

I was in fact confused as to what it was supposed to be in Thor's code, I assumed it was something to do with structs that I did not know about. Do structs require a constructor?
« Last Edit: December 12, 2012, 04:27:09 am by Rabees »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #7 on: December 12, 2012, 04:25:52 am »
That is a constructor declaration for Frame.

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #8 on: December 12, 2012, 04:34:02 am »
It appears there are more to structs than I thought. I have found more lines in Thor's code doing things with the struct, I'll see if I can understand this.

edit: I see this in Thor's implementation of the class:

FrameAnimation::Frame::Frame(float duration, ResourcePtr<const sf::Texture> texture, const sf::IntRect& subrect)
: duration(duration)
, texture(texture)
, subrect(subrect)
{
}

Is this the constructor for Frame?


EDIT: I fixed it :D
I didn't know the struct needed constructed. I added that line into my implementation and moved the struct declaration back into the private sector of the class.
« Last Edit: December 13, 2012, 11:35:44 pm by Rabees »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vector push_back call is an undefined reference
« Reply #9 on: December 12, 2012, 07:34:18 am »
To be clear: structs and classes are the same, the only difference being the default visibility (public for structs, private for classes). So a struct can have constructors, member functions, etc. It doesn't need to have a constructor, but it can have one and if you declare it like you do you must give it a body, like any other function.
Laurent Gomila - SFML developer

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Vector push_back call is an undefined reference
« Reply #10 on: December 13, 2012, 11:32:42 pm »
That is very useful information, thanks!