Another C++ compile issue

M

Mark

Hi,

I have two classes:

template <class Resource> class GuardBase
{
protected :
Resource& fResource;
bool fAcquired;

GuardBase(Resource& res) : fResource(res), fAcquired(false) {}

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resource> class LockGuard : public GuardBase<Resource>
{
public :
LockGuard(Resource& res) : GuardBase<Resource>(res)
{
fAcquired = false; // Error
fResource.acquire(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.release(); // Error
}
};

I get compiler errors for fAcquired (and fResource) "not declared in
this scope" in the constructor and destructor of LockGuard. This is
with G++ 4.1.0 on SUSE Linux. This previously compiled on several
other compilers.

Can someone tell me what is wrong here and how to fix it please?

TIA, Mark
 
I

Ian Collins

Mark said:
Hi,

I have two classes:

template <class Resource> class GuardBase
{
protected :
Resource& fResource;
bool fAcquired;

GuardBase(Resource& res) : fResource(res), fAcquired(false) {}

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resource> class LockGuard : public GuardBase<Resource>
{
public :
LockGuard(Resource& res) : GuardBase<Resource>(res)
{
fAcquired = false; // Error
fResource.acquire(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.release(); // Error
}
};

I get compiler errors for fAcquired (and fResource) "not declared in
this scope" in the constructor and destructor of LockGuard. This is
with G++ 4.1.0 on SUSE Linux. This previously compiled on several
other compilers.

Can someone tell me what is wrong here and how to fix it please?
Looks OK, try this->fAcquired.
 
B

Bernd Strieder

Mark said:
template <class Resource> class LockGuard : public GuardBase<Resource>
{
public :
LockGuard(Resource& res) : GuardBase<Resource>(res)
{
fAcquired = false; // Error
fResource.acquire(); // Error
fAcquired = true;
}
};

I get compiler errors for fAcquired (and fResource) "not declared in
this scope" in the constructor and destructor of LockGuard. This is
with G++ 4.1.0 on SUSE Linux. This previously compiled on several
other compilers.

Can someone tell me what is wrong here and how to fix it please?

This is due to changes which made g++ since 3.4 a lot more conformant to
the C++ standard. You have to make sure that names are treated as
dependent names, in this case by inserting this-> as has been posted.
There are other cases requiring different handling. The corrected code
should be accepted by old compilers, at least gcc-3.3, too.

See

http://gcc.gnu.org/gcc-3.4/changes.html

Bernd Strieder
 
P

Pierre Barbier de Reuille

Ian said:
Looks OK, try this->fAcquired.

If does not look OK ...
You *have* to specify the "this->" if you want to use members inherited
from template classes, as if a base class is dependant on a template
parameter, its scope is not examine during unqualified name lookup (cf.
C++ norm 14.6.2 §3).

The reason is, if a specialisation of the template does not include
these members, what should the compiler do ? Look for these at global
scope ?

But the worst is the opposite: what if the child class uses some global
variables that happens to be public or protected members in some
specialization ? Not enforcing the "this->" could lead to very strange
behaviour !

Pierre
 
M

Mark

This is due to changes which made g++ since 3.4 a lot more conformant to
the C++ standard. You have to make sure that names are treated as
dependent names, in this case by inserting this-> as has been posted.
There are other cases requiring different handling. The corrected code
should be accepted by old compilers, at least gcc-3.3, too.

See

http://gcc.gnu.org/gcc-3.4/changes.html

Thanks to all who replied. This solves this problem ... now on to the
next ;-)

Mark
 
I

Ian Collins

Pierre said:
If does not look OK ...
You *have* to specify the "this->" if you want to use members inherited
from template classes, as if a base class is dependant on a template
parameter, its scope is not examine during unqualified name lookup (cf.
C++ norm 14.6.2 §3).
My understanding of that was the base class scope is used for name
lookup when the derived template class is instantiated.
 
P

Pierre Barbier de Reuille

Ian said:
My understanding of that was the base class scope is used for name
lookup when the derived template class is instantiated.

It used to be like that for g++ (and most probably for many others) but
it was a mistake, as the norm explicitly says otherwise.

Pierre
 
I

Ian Collins

Pierre said:
It used to be like that for g++ (and most probably for many others) but
it was a mistake, as the norm explicitly says otherwise.
Um, it does too. Looks like the example contradicts the text, which
says the base class scope is not examined until the class template is
instantiated, which implies that it is included.
 
M

Marcus Kwok

Mark said:
template <class Resource> class GuardBase
{
protected :
Resource& fResource;
bool fAcquired;

GuardBase(Resource& res) : fResource(res), fAcquired(false) {}

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resource> class LockGuard : public GuardBase<Resource>
{
public :
LockGuard(Resource& res) : GuardBase<Resource>(res)
{
fAcquired = false; // Error
fResource.acquire(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.release(); // Error
}
};

I get compiler errors for fAcquired (and fResource) "not declared in
this scope" in the constructor and destructor of LockGuard. This is
with G++ 4.1.0 on SUSE Linux. This previously compiled on several
other compilers.

Can someone tell me what is wrong here and how to fix it please?

Others have posted the solution using this->fAcquired, but there are
other workarounds too. See:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top