473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another C++ compile issue

Hi,

I have two classes:

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

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

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.relea se(); // 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

Aug 16 '06 #1
8 1312
Mark wrote:
Hi,

I have two classes:

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

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

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.relea se(); // 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.

--
Ian Collins.
Aug 16 '06 #2
Mark wrote:
template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // 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

Aug 16 '06 #3
Ian Collins wrote:
Mark wrote:
>Hi,

I have two classes:

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

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

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.relea se(); // 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.
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
Aug 16 '06 #4
On Wed, 16 Aug 2006 11:33:03 +0200, Bernd Strieder
<st******@infor matik.uni-kl.dewrote:
>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

Aug 16 '06 #5
Pierre Barbier de Reuille wrote:
Ian Collins wrote:
>>Mark wrote:
>>>Hi,

I have two classes:

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

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

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.relea se(); // 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.


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.

--
Ian Collins.
Aug 16 '06 #6
Ian Collins wrote:
Pierre Barbier de Reuille wrote:
>Ian Collins wrote:
>>Mark wrote:

Hi,

I have two classes:

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

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

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.relea se(); // 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.

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.
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
Aug 16 '06 #7
Pierre Barbier de Reuille wrote:
Ian Collins wrote:
>>Pierre Barbier de Reuille wrote:
>>>Ian Collins wrote:
>>>>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).

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.
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.

--
Ian Collins.
Aug 16 '06 #8
Mark <no****@nospam. spamwrote:
template <class Resourceclass GuardBase
{
protected :
Resource& fResource;
bool fAcquired;

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

public :

void acquire(void)
{
}

void release(void)
{
}

};

template <class Resourceclass LockGuard : public GuardBase<Resou rce>
{
public :
LockGuard(Resou rce& res) : GuardBase<Resou rce>(res)
{
fAcquired = false; // Error
fResource.acqui re(); // Error
fAcquired = true;
}

~LockGuard()
{
if (fAcquired) // Error
fResource.relea se(); // 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-lit...html#faq-35.18

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
613
by: Michael L. Hostbaek | last post by:
Hi, I am trying to compile a piece of software, on a FreeBSD system. gcc version 2.95.4 When compiling, I get lots of these warnings: --- logger.c: In function `cf_logger_init':
188
7264
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
6
2257
by: SteveS | last post by:
Hello All. I have an asp.net application with 3 different assemblies. They are like this: 1) Assembly: PublicSite (This contains the website UI) Root namespace: PublicSite 2) Assembly: PublicSite.MyProfile.Business (This contains the business rules) Root namespace: PublicSite
4
1172
by: Winista | last post by:
I have a property in my class like.. Int32 iVal; public String Val { get{return iVal;} set{this.iVal = value;} }
4
1548
by: EoindeBarra | last post by:
I am not sure where I am going wrong in the code below. It seems to be working fine, then one of the pointers (directory1) gets garbage added to it, and it breakes the rest of the program. Also, there seems to be memory issues when I debug the program and I know they are from the code below, just not sure why. I would really appreciate any guidance on the following problem. I have a set method
14
4270
by: rohitkumar | last post by:
this is my code to calculate the sum of digits in a number for eg. 432567 sum =4+3+2+5+6+7=27 i 'm using turboc++ compiler from borland and i 'm runing my program on DOS. #include<iostream.h> #include<conio.h> long poweroften(int); //to calculate power of 10
1
3083
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows Laptop, running WinXP, Python 2.5 and the latest version of Cheetah. I have two questions: 1. How and where do you compile Cheetah templates in Windows? The command in the docs is cheetah compile a, however, I believe that this
2
2052
by: Jeff | last post by:
Hi I'm trying to achieve a scenario where I have c# files that are compiled dynamically, the assemblies are then loaded in a different AppDomain, I call a simple method from the object, and then unload the AppDomain to release the lock on the assemly files (so to I can compile the code again if it has been modified). However, I've encountered a problem, whereby the assembly is also loaded in the default AppDomain! I have a few classes...
3
3550
by: =?Utf-8?B?QWxleGFuZGVyIFd5a2Vs?= | last post by:
My application is taking way too long to build. It use to never take so long. I change only line and I think it recompiles all the classes in the assembly because it hangs forever. Here is the compiler line from the output window during complication: Target CoreCompile: C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /platform:x86 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /main:AutoDeveloper.Program...
6
1960
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a non- comment Python statement, there is no error. Here's an example (using 2.5.1 on OS X) .... def foo(): .... print 'bar' """ <code object <moduleat 0x79608, file "", line 2>
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.