template, files problem

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

header.h
1
2
3
4
5
6
7
8
9
10
11
12
# 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
1
2
3
4
5
6
7
8
# pragma once
# include "header.h"

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


header2.cpp
# include "header2.h"

The problem is that if I uncomment the line in header.cpp, 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)
Last edited on
I think the class declaration in header.h is incomplete. The final }; is missing.

Keep in mind that #include "foo.h" really just inserts the content of foo.h into the current file in a "copy & paste" fashion, so syntax errors in the included file may very well backfire at a later point...
Last edited on
Thanks @Kigar ,I edited the code, But the problem is something else))
Last edited on
Obviously, you moved the }; to the next line to properly close the class deceleration.

...but now the body of function output() is missing its closing }.


I suggest you re-format your code with proper indentation, so those things become apparent:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class T>
class firstClass
{
    T var;
public:
    firstClass(T v)
    {
        var = v;
    }

    void output(void)
    {
        cout << var << endl;
    } // <-- this was missing in your code !!!
};

Last edited on
Thank you one more time @kigar, I think now you can see what is the real problem I am looking to solve(unless i forget another piece of the code ))) )
Why do you include "header2.h" in "header.h" ???

It makes sense to include "header.h" into "header2.h", because class secondClass (declared in "header2.h") wants to extend class firstClass (declared in "header.h"), but not the other way around! In fact, you insert the declaration of class secondClass into "header.h" before class firstClass is even declared...

Again, keep in mind that the #include directive just inserts the file contents in a "copy & paste" fashion!

Also keep in mind that the C/C++ compiler processes the file from top to bottom in a single pass. So, anything you want to "use" at a certain point of the file, must have been declared above (before).
Last edited on
Here is why, the code above is just a piece trying to site the problem.

In short secondClass need to inherit from firstClass, then it need to implements is own functions, then the firstClass will use the full secondclass.

But doing so, I am falling under something called circular dependency which I can solve if I deal only with classes, but here you can clearly see that I need to use a template

Thank you for your time @kigar )
Avoid circular dependency by all means!

If class Bar (declared in "bar.h") wants to inherit from class Foo (declared in "foo.h"), then class Foo clearly must be declared before (above) the declaration of class Bar. Consequently, you'd #include "foo.h" into "bar.h" at the top of "bar.h". But there is no reason and no need to include "bar.h" into "foo.h" !!!


foo.h:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

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

bar.h:
1
2
3
4
5
6
7
8
#pragma once
#include "foo.h"

class Bar : public Foo<char>
{
public:
    Bar(char a) : Foo(a) { }
};

main.c:
1
2
3
4
5
6
#include "bar.h" // <-- indirectly includes "foo.h" too

int main()
{
    Bar bar('q');
}
Last edited on
Topic archived. No new replies allowed.