0

I'm trying to inherit a non-template class from a template class that specified witch type I need

My code is like this:

the header file for base class(updated):

//base.hpp
template<typename T>
class Base {
public:
    Base(T a,int b) :
            aa(b) {
        this->bb = a;
        // ... some code
    }
    // .. some functions ( all are NOT virtual )
protected:
    const int aa;
    T bb;
    // .. some non-constant variables
}

The header and code for derived class:

//derived.hpp
#include <SFML/System.hpp>
#iclude "base.hpp"

class Derived: public Base<sf::Vector2i> {
public:
    Derived(float, int, Vector2i);
    // .. other methods
protected:
    // .. other variables
}

//derived.cpp
#include "derived.hpp"

Derived::Derived(float initf, int myint, Vector2i vec) :
        Base(vec, myint) {
    // ... some codes
}

"sf" referes to the namespac for working with SFML library. I'm using SFML 2.0 that compiled from the source using cmake

(for more information you could see: http://www.sfml-dev.org/)

When I try to compile these codes with a command similar to this:

$ g++ ./main.cpp ./derived.cpp ./base.cpp -lsfml-system

I get some linker error that tell:

In function `Derived::Derived(float, int, sf::Vector2<int>)':
undefined reference to `Base<sf::Vector2<int> >::Base(sf::Vector2<int>, int)'

Also I'm using "g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3" as my C++ compiler with C++11 enabled.

RiaD
  • 46,822
  • 11
  • 79
  • 123
ARF
  • 72
  • 7
  • Thousands of duplicates... get rid of your base.cpp file. – Kerrek SB Jul 05 '13 at 22:05
  • @kerrek-sb : no! its not my problem. I still having issue when i made my methods inline and/or included them in the class definition – ARF Jul 05 '13 at 22:09
  • 2
    I have a car in the garage, it won't start. I asked the mechanic and told him the symptoms: I turn the key, nothing happens. *That is most probably the battery*. So I bought a battery and installed it. I turned the key and the engine won't start. Not having an engine under the hood might be related. That does not mean that I did not need a battery (I did not have one!), it only means that there might be other issues with my car. – David Rodríguez - dribeas Jul 05 '13 at 22:12
  • @DavidRodríguez-dribeas 9 times out of 10 it's caused by the dead hooker in the trunk. – Captain Obvlious Jul 05 '13 at 22:14

1 Answers1

1

If you want the compiler to implicitly instantiate your templates (99.9% of the cases you want), the compiler must see the definitions of the members. Move the definition of the Base constructor to the header and that should solve your problem.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • @david-rodriguez-dribeas : the linker still tells errors – ARF Jul 05 '13 at 22:03
  • @david-rodriguez-dribeas : same errors! – ARF Jul 05 '13 at 22:12
  • @ARF: hard to believe, but that might be due to discrepancies between what you are trying to compile and what you are showing in the question. Try to reduce the program to the minimum that fails, then post the exact program and the errors. – David Rodríguez - dribeas Jul 05 '13 at 22:14
  • @david-rodriguez-dribeas : now i'm so sleepy! :zZ I'll do this tomorrow! by the way, TnX alot! ;) – ARF Jul 05 '13 at 22:20
  • @david-rodriguez-dribeas : not exactly as you asked but I updated the code to a similar version to my code ;) – ARF Jul 06 '13 at 01:50