zeboidlund
zeboidlund

Reputation: 10147

Visual C++ template syntax errors

I have a header file...

#include <SFML\Graphics.hpp>
#include <SFML\Graphics\Drawable.hpp>
#include <SFML\System.hpp>
#include <iostream>

#ifndef _SPRITE_H_
#define _SPRITE_H_

namespace Engine 
{
    template <class T>
    class Sprite
    {
        sf::Vector2<T> * vector;
        sf::Sprite * sprite;

    public:
        Sprite(sf::Vector2<T> vect, sf::Sprite spr) 
        { 
            this->sprite = spr;
            this->vector = vect;            
        }
        ~Sprite();
        bool Draw(T x, T y, T rotate = 0);
        sf::Image GetImage()
        {
            return this->sprite->GetImage();
        }
    };
};

#endif _SPRITE_H_

And a source file...

#include <SFML/Graphics.hpp>
#include <SFML/Config.hpp>

#include "sprite.h"

template <typename T>
Sprite(sf::Vector2<T> vector, sf::Sprite sprite) 
{
    this->sprite = sprite;
    this->vector = vector;
}

template <typename T>
bool Draw(T x, T y, T rotate) 
{
    return false;
}

In VS 2010, when I compile VC++ I get the following errors:

Error   2   error C2143: syntax error : missing ';' before '<'  c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7   1   Engine2
Error   3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7   1   Engine2
Error   4   error C2988: unrecognizable template declaration/definition c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7   1   Engine2
Error   5   error C2059: syntax error : '<' c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7   1   Engine2
Error   6   error C2059: syntax error : ')' c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 7   1   Engine2
Error   7   error C2143: syntax error : missing ';' before '{'  c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 15  1   Engine2
Error   8   error C2447: '{' : missing function header (old-style formal list?) c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 15  1   Engine2

I am a total noob to C++ (coming from C#), and have been having issues compiling this simple file as a means to at least learn the syntax before I continue on. As you can see, all I'm trying to do is reference a template from a header file to a source file, so that template reference effects all of my methods.

What am I doing wrong? I've tried to understand these compiler messages, but I'm having trouble decrypting them.

Update

Followed suggestion: took care of almost everything, except for this:

Error   1   error C2995: 'Engine::Sprite<T>::Sprite(sf::Vector2<T>,sf::Sprite)' : function template has already been defined    c:\users\owner\documents\visual studio 2010\projects\engine2\engine2\sprite.cpp 12  1   Engine2

Upvotes: 1

Views: 10598

Answers (1)

Seth Carnegie
Seth Carnegie

Reputation: 75150

Since Sprite is inside Engine, it should be

template <typename T>
Engine::Sprite<T>::Sprite(sf::Vector2<T> vector, sf::Sprite sprite)
{ ... }

And

template <typename T>
bool Engine::Sprite<T>::Draw(T x, T y, T rotate)
{
    return false;
}

Or, as @jli suggests, put it inside the namespace Engine too:

namespace Engine {
    template <typename T>
    Sprite<T>::Sprite(sf::Vector2<T> vector, sf::Sprite sprite)
    { ... }

    template <typename T>
    bool Sprite<T>::Draw(T x, T y, T rotate)
    {
        return false;
    }
}

Upvotes: 2

Related Questions