In the file test.cpp, I have this:
template <typename T>
class A
{
public:
A(int a){};
virtual ~A();
private:
};
class B : public A<int>
{
public:
B(int a):A(a){};
virtual ~B();
private:
};
int main()
{
return 0;
}
When I compile it, I get this:
jason@jason-linux:~/Documents/ECLibrary$ g++ -g -Wall -Wextra -pedantic-errors test.cpp -o tdriver
test.cpp: In constructor ‘B::B(int)’:
test.cpp:14: error: class ‘B’ does not have any field named ‘A’
test.cpp:14: error: no matching function for call to ‘A<int>::A()’
test.cpp:5: note: candidates are: A<T>::A(int) [with T = int]
test.cpp:3: note: A<int>::A(const A<int>&)
I do not want a default constructor for my base class, since it doesn’t make sense in my code. I just want my derived class to perform the called constructor of the base class and do some extra construction for the extra stuff in the derived class. I’m really not sure why it is trying to call the default constructor of the base class when I’m trying to explicitly call an alternate constructor. Am I missing something here?
Thanks
You can add the template argument list to
A:Note that the code that you have–using
Awithout the template argument list–is valid C++. Comeau and Visual C++ 2010 both accept the code as-is.g++ 4.3 does not accept the code without the template argument list. Perhaps someone can test a later version of g++ or check the g++ bug database to see whether it is a known issue (I don’t regularly use g++ and am not familiar with their bug database).