473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Variable is undeclared, but why?

Here's an examble:

template<class Cstruct S
{
C c;
};

template<class Cstruct D
:public S<C>
{

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

int main()
{
D<inti;
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?

Nov 27 '06 #1
4 1271
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.

Nov 27 '06 #2

ma*******@googlemail.com 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->

Nov 27 '06 #3
flopbucket wrote:
ma*******@googlemail.com 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 Cclass B {
protected:
C c;
};

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

int main() {
D<intdi;
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
Nov 27 '06 #4

ma*******@googlemail.com 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

Nov 27 '06 #5

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

Similar topics

10
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having...
4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
2
by: ~~~ .NET Ed ~~~ | last post by:
It is not the first time I see this happen. I am using VS.NET 2003 with .NET Framework 1.1. In this particular situation I have a custom user control in a windows form. There is a member variable...
0
by: Michael Howes | last post by:
I have a strange "warning" that shows up during design time in my current project. I have a UserControl, which contains a public enum called ViewTypes This UserControl also has a public property...
2
by: hui | last post by:
Here is a problem I am having with web form designer. I have a database control in the form, and setup the connection string as a dynamic property. It compiles and runs fine. I close the aspx...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
64
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
11
by: =?UTF-8?Q?Filip_Gruszczy=C5=84ski?= | last post by:
Hello everyone! It is my first message on this list, therefore I would like to say hello to everyone. I am fourth year student of CS on the Univeristy of Warsaw and recently I have become very...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.