1

The question is how to call the base constructor from an inherited template class. I want to create a FixedQueue and overload some function in std::queue. Therefore std:queue is the base class. The keyword using, since c++11, can be used to call the base and it works if this is a specialised class, but I cannot get it working with a template class.

Furthermore I tried it to use the old c++ standard in which I simply invoke the defined constructors in std::queue. However it doesn't work.

h file

#ifndef _HEADER_FIXED_QUEUE_
#define _HEADER_FIXED_QUEUE_

#include <queue>
#include <iostream>

template<class T> 
class FixedQueue : public std::queue<T>
{
  //using queue<T>::queue<T>;

  public:
    FixedQueue();
    FixedQueue(const T &initial_var);
    void foo() { std::cout << "inside\n"; }

};

#endif

cpp file

#include "FixedQueue.h"

template<typename T>
FixedQueue<T>::FixedQueue()
:
  std::queue<T>()
{ 
  std::cout << "Default Constructor FixedQueue\n";
}

template<typename T>
FixedQueue<T>::FixedQueue(const T &initial_var)
:
  std::queue<T>(initial_var)
{ 
  std::cout << "Specialized Constructor FixedQueue\n";
}

main file.

#include <iostream>
#include "FixedQueue.h"

int main()
{
  FixedQueue<int> d_frameSlices;


  std::cout << "I want to do something with my queue\n";
}

The question is thus. How do I chain the constructors to the defined constructors in the base class std::queue. The template thing is killing me.

This is the error message I obtain from clang, which is the usual undefined reference.

Undefined symbols for architecture x86_64:
  "FixedQueue<int>::FixedQueue()", referenced from:
      _main in main-lqoFSA.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If someone knows how to do this with "using" or the old fashion way I am happy with both. Thanks in advance

Montaldo
  • 863
  • 8
  • 16

1 Answers1

4

you should not put the template in cpp file put it all in header file

michaeltang
  • 2,850
  • 15
  • 18
  • This is not the issue. I am declaring the class in the header file and implementing the functions in the cpp. These need the template definition in order to know the desired Type and if the function is a template function it deff needs that declaration – Montaldo Jan 16 '14 at 12:28
  • @Montaldo: You're not listening. This _is_ an issue. Do not implement the functions in the CPP. This is not a _class_, but a _class template_. – Lightness Races in Orbit Jan 16 '14 at 12:28
  • Your right, but isn't there a possibility to implement them in a cpp file. Normally with everything else regarding templates I was able to do this. Could you elaborate on this for me please? – Montaldo Jan 16 '14 at 12:33
  • @Montaldo: No, not really. Function definitions for templates must be visible in _every_ translation unit. Is this not stated in your C++ book? For more information see the link Alf posted in comments a few minutes ago. – Lightness Races in Orbit Jan 16 '14 at 12:34
  • Ah no I am wrong. Your right, The reason as to why this worked is I used to include my cpp file in the header. Kinda hack. Thanks for the effort and explanation. – Montaldo Jan 16 '14 at 12:36
  • @Montaldo: That's a pattern some people use though, in the rare occasion that I do it, I call it a .ipp file rather than .cpp which sort of de-hacks it a bit. :) – Lightness Races in Orbit Jan 16 '14 at 12:38