Template <-> Enum problem

Hello,

I have below (depersonalized)code which compiles with an error : "constant "OFF" is not a type name", on line 23.
I can't figure out what is wrong. Does anybody know ?


#include <iostream>
using namespace std;

enum ON_OFF
{
ON,
OFF,
};

class ABC {
public:
ABC (ON_OFF argOnOff) {
}
};

template <typename clsClass> class clsTemplate {
public:
clsTemplate () {
cout << "2" << "\n";
}

clsClass myClass(OFF); //this is line 23

};

class clsMyServer : public clsTemplate<ABC>
{
public:
clsMyServer(): clsTemplate<ABC>( ) {
cout << "1" << "\n";
}
};


int main(void) {

clsMyServer lcServer ();

/* ABC lcABC(OFF); this works */
}
Please uses code tags.

The problem is clsClass myClass(OFF); //this is line 23 , you cannot initialize members like that, you have to initialize them in the constructor like that:

1
2
3
4
5
6
7
8
9
template <typename clsClass> class clsTemplate {
public:
clsTemplate () : myClass(OFF) {
cout << "2" << "\n";
}

clsClass myClass;

};
Last edited on
You declared a templetae class the tamplate parameter of which is a type parameter.

typename clsClass

When you create an object of thi class you must to specify a type as a template argument. For example you could write

clsTemplate<ON_OFF> myClass;

Record

clsClass myClass(OFF);

means that on object of class clsClass is being created by means of calling constructor clsClass( OFF ). However there is no such a class and there is no such a constructor of that class with one parameter.


Last edited on
Aquaz: thanks, that solved my problem !

vladFromMoscow: thanks for your reply, the ON/OFF is not supposed to come from outside, in this case/example it should always be OFF.
Topic archived. No new replies allowed.