Inhertiance

I am learning how to use derived classes using inheritance but am so far unable to declare the derived class without error. I have boiled down my code to demonstrate the error I am receiving in the program I am working on

test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef TEST_H
#define TEST_H

#include <string>
#include <iostream>

using namespace std;

template < typename T >
class Aaa
{
public:
  T var1;
  void out(){cout << var1;};
  Aaa(T a): var1(a){};

};

template < typename T >
class Bbb : public Aaa  
{                       //Error: expected class-name before '{' token

public:
  T var2;
  void out(){cout << var2;}
  Bbb(T b): var2(b) {};

};

#endif 


tester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "test.h"

int main()
{
  Aaa<string> Test1("hello");
  Bbb<string> Test2("goodbye");
  Test1.out();
  Test2.out();

  return 0;
}
The error I get in Visual Studio 2019:
'Aaa': use of class template requires template argument list

Easy to fix in your header file (you need to have a base class default ctor):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef TEST_H
#define TEST_H

#include <string>
#include <iostream>

template <typename T>
class Aaa
{
public:
   T var1 { };
   void out() { std::cout << var1 << '\n'; };
   Aaa(T a) : var1(a) { }

   Aaa() { } // <---- default ctor needed!
};

template <typename T>
class Bbb : public Aaa<T>
{
public:
   T var2 { };
   void out() { std::cout << var2 << '\n'; }
   Bbb(T b) : var2(b) { }
};

#endif 

main source:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include "test.h"

int main()
{
   Aaa<std::string> Test1("hello");
   Bbb<std::string> Test2("goodbye");

   Test1.out();
   Test2.out();
}

hello
goodbye

Learning inheritance using templated classes IMO over-complicates things.
It looks like my problem was this:

class Bbb : public Aaa<T>

I did not have the <T> on my code, including that fixed my problem.

Having a default constructor was the problem my compiler would have showed next, so thank you for solving my future problem and helping me recognize my current problem!
I will take a slightly different view from @Furry Guy. You may have actually been trying to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef TEST_H
#define TEST_H

#include <string>
#include <iostream>

template <typename T>
class Aaa
{
public:
   T var1 { };
   void out() { std::cout << var1 << '\n'; };
   Aaa(T a) : var1(a) { }
};

template <typename T>
class Bbb : public Aaa<T>
{
public:
   Bbb(T b) : Aaa<T>(b) { }
};

#endif  


Either way, you need to derive Bbb<T> from Aaa<T>, not Aaa. In your original code (and @Furry Guy's solution) you have duplicate member data (var1 and var2) and you need a default constructor. In my solution, you only have 1 copy of the member data, and you don't need a default constructor.
I retained the member data in the two classes since they might not be duplicates. The derived class might be storing different data from the base class.
Topic archived. No new replies allowed.