Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Variable is undeclared, but why?

Reply
Thread Tools

Variable is undeclared, but why?

 
 
mailforpr@googlemail.com
Guest
Posts: n/a
 
      11-27-2006
Here's an examble:

template<class C> struct S
{
C c;
};

template<class C> struct D
ublic S<C>
{

void f(C temp)
{
c=temp;
}
};

int main()
{
D<int> i;
i.f(2);

return 0;
}


Error message (compiled with g++ 3.4):
In member function `void D<C>::f(C)':
error: `c' undeclared (first use this function)

D is a S, so D should have public access to c. But it hasn't. Does
someone know why that is so?

 
Reply With Quote
 
 
 
 
mailforpr@googlemail.com
Guest
Posts: n/a
 
      11-27-2006
Okay, it does have access to c. I modified the code a bit:

void f(C temp)
{
S<C>::c=temp;
}

Is there any other way to access the member of a base class/struct?
Writing S<C>:: before each member is annoying.

 
Reply With Quote
 
 
 
 
flopbucket
Guest
Posts: n/a
 
      11-27-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Okay, it does have access to c. I modified the code a bit:
>
> void f(C temp)
> {
> S<C>::c=temp;
> }
>
> Is there any other way to access the member of a base class/struct?
> Writing S<C>:: before each member is annoying.


Well, you can use this->

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-27-2006
flopbucket wrote:
> (E-Mail Removed) wrote:
>> Okay, it does have access to c. I modified the code a bit:
>>
>> void f(C temp)
>> {
>> S<C>::c=temp;
>> }
>>
>> Is there any other way to access the member of a base class/struct?
>> Writing S<C>:: before each member is annoying.

>
> Well, you can use this->


You could also use 'using':

template<class C> class B {
protected:
C c;
};

template<class C> class D : public B<C> {
using B<C>::c; /// AHA!
public:
void foo(C cc) {
c = cc;
}
};

int main() {
D<int> di;
di.foo(42);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      11-27-2006

(E-Mail Removed) wrote:

> D is a S, so D should have public access to c. But it hasn't. Does
> someone know why that is so?


Because you haven't read the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.19

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Connect to 802.11b but not 802.11g... but used to be able to...? jbraly@gmail.com Wireless Networking 1 03-13-2006 11:05 PM
conects but wont send info but receves =?Utf-8?B?dGFuaw==?= Wireless Networking 1 01-04-2006 04:07 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments