valid C++

M

Marc Schellens

gcc 3.4 complains when a
derived class uses an indentifier which is
defined in the base class and the base class
is the template parameter.

eg. something like:

class B
{
protected:
int member;
};

template< class base>
class D: public base
{
public:
void aFun();
};

template< class base>
void D< base>::aFun()
{
member = 1;
}

template class D< B>;

It says then:
member undecalred (first use of this function).


Isn't that valid C++?

thanks,
marc
 
A

Alf P. Steinbach

* Marc Schellens:
gcc 3.4 complains when a
derived class uses an indentifier which is
defined in the base class and the base class
is the template parameter.

eg. something like:

class B
{
protected:
int member;
};

template< class base>
class D: public base
{
public:
void aFun();
};

template< class base>
void D< base>::aFun()
{
member = 1;
}

template class D< B>;

It says then:
member undecalred (first use of this function).

Isn't that valid C++?

No, but older compilers may accept it (also, VC 7.1 accepts it).

The problem is that when seeing 'member = 1' for the first time the
compiler doesn't have the slightest cue about where 'member' comes from,
or whether it comes from anywhere.

To give it that clue you can use a 'using' statement in class D,
or you can qualify 'member' in some way, e.g. 'this.member'.
 
S

Sharad Kala

Alf P. Steinbach said:
* Marc Schellens:
To give it that clue you can use a 'using' statement in class D,
or you can qualify 'member' in some way, e.g. 'this.member'.

Of course you must have meant this->member.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top