take a look at this code:

template <class T>
class C {
public:

	class A {

	protected:
		int x;
	};

	class B: public A {
	protected:
		void write() {
			cout << x << endl;
		}
	};

};

when I try to compile with g++ i get:
test.cpp: In member function ‘void C<T>::B::write()’:
test.cpp:18: error: ‘x’ was not declared in this scope

If I remove the template argument of class C all workes fine ( of course )
Does anyone have an ideea of what is going on?

thx in advance

Recommended Answers

All 3 Replies

Well where are you exactly using template functionalities in this code segment ??? I don't see its usage anywhere in the code given by you.

Use this

void write() {
   cout << this->x << endl;
 }

Well where are you exactly using template functionalities in this code segment ??? I don't see its usage anywhere in the code given by you.

It's just a sample I made to hilight the compile error. I might use the template parameter on other methods I didn't post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.