Question on C ++ Inheritance

In the test.cpp file, I have the following:

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 the following:

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 need a default constructor for my base class, as it does not make sense in my code. I just want my derived class to execute the called constructor of the base class and do some extra construction for the extra material in the derived class. I really don't know why it is trying to call the default constructor of the base class when I try to explicitly call the alternative constructor. Did I miss something?

thank

+3
source share
5 answers

A:

B(int a) : A<int>(a) { } 

, , - A , - ++. Visual ++ 2010 .

g++ 4.3 . , - g++ g++, , ( g++ ).

+9

B(int a):A<int>(a){}.

+3

:

class B : public A<int>
{
public:
  B(int a):A<int>(a){};
  virtual ~B();

private:
};

.

0

A, A, A<int>

-1

, A . , template<> Class A<int>...

-1

Source: https://habr.com/ru/post/1769910/


All Articles