-2

I have 4 files: header.h, header.cpp, header2.h and header2.cpp, like the ones below:

header.h

# pragma once
# include "header2.h"

template<class T>
class firstClass
{
    T var;
public:
    firstClass(T v){   var = v;  }

    void output(void){     cout << var << endl;   }
};

header.cpp

// # include "header.h"

header2.h

# pragma once
# include "header.h"

class secondClass:public firstClass<char>
{
public:
    secondClass(char a):firstClass(a){}
};

header2.cpp

# include "header2.h"

The problem happens if I un-comment the line in header.h, I got an error in code::blocks:

error in file(header2.h): expected template-name before '<' token

Is anyone know why the problem, and how to fix it. Thank you very much)

Ninja
  • 59
  • 5
  • 2
    Not the actual problem but be aware of: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – πάντα ῥεῖ Aug 01 '22 at 23:15
  • 3
    firstClass does not depend on secondClass. header.h should not include header2.h. As it is in your post you have an infinite loop from mutual includes - well you would if it wasn't for your pragma once.. – Avi Berger Aug 01 '22 at 23:16
  • @ avi berger "firstClass does not depend on secondClass", No it depends, this is only a sample of the main problem. "you have an infinite loop from mutual includes" I do not think so thanks to #pragma once preprocessor directive – Ninja Aug 01 '22 at 23:21
  • 1
    True that the pragma once's break prevent the infinite loop, I overlooked that. But the result is that you are trying to define secondClass before firstClass. SInce secondClass inherits from first class, you can't do that. If firstClass depends on secondClass, well it depends how. Someways will work (possibly with a forwad declaration), others are not allowed. – Avi Berger Aug 01 '22 at 23:28
  • 1
    If firstClass's dependence on secondClass is inside a method implementation, you can move the header .h include down to after the class definition, but before the method implementation. If its a member function parameter, you will also need a forward declaration at the beginning. – Avi Berger Aug 01 '22 at 23:34
  • Thank avi berger, can you write a forward declaration for me, all those i write fails – Ninja Aug 01 '22 at 23:41
  • This may help a little, secondClass will inherit from firstClass, then it will have its own methods, then when it is finished, firstClass will use secondClass. I know this looks like a spaghetti dish, but i am trying to find a genius here who can help me do it ) – Ninja Aug 01 '22 at 23:44
  • You've lost me in your explanation, but from your post, a forward declaration would simply be: `class secondClass;` I've tried it [here](https://godbolt.org/z/cr5obaznv). – Avi Berger Aug 02 '22 at 00:13

1 Answers1

2

header.h tries to include header2.h. That won't work, as header2.h refers to the template defined later in header.h. Suggest not depending on secondClass (and thus header2.h) in header.h.

lorro
  • 10,687
  • 23
  • 36