473,474 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is g++ wrong?

g++ can't compile this code:

#include <iostream>
using namespace std;

template <typename T>
class Outer
{
public:
class Base
{
protected:
int m_n;
};

class Derived : public Base
{
public:
Derived(int n)
{
m_n = n; // g++ says m_n undeclared
}
void print()
{
cout << m_n << endl;
}
};
};

int main(int argc, char *argv[])
{
Outer<int>::Derived(100).print();
return 0;
}

Sep 12 '06 #1
2 1504

steve yee wrote:
g++ can't compile this code:

#include <iostream>
using namespace std;

template <typename T>
class Outer
{
public:
class Base
{
protected:
int m_n;
};

class Derived : public Base
{
public:
Derived(int n)
{
m_n = n; // g++ says m_n undeclared
}
void print()
{
cout << m_n << endl;
}
};
};

int main(int argc, char *argv[])
{
Outer<int>::Derived(100).print();
return 0;
}
You can't ask a compiler to guess what its suppose to do with that
template parameter. At the very least, a templated class will require a
ctor. There is also the fundamental question of what that "Outer" class
is suppose to bring to this design.

Maybe i'm wrong but you are attempting to solve a technical issue when
what i see is lack of a more fundamental startegy.

#include <iostream>

namespace Outer
{
template< typename T >
class Base
{
T m_t;
public:
Base() : m_t(0) { }
Base(T t) : m_t(t) { }
~Base() { }
T get() const { return m_t; }
};

template< typename T >
class Derived : public Base< T >
{
public:
Derived() { }
Derived(T t) : Base< T >(t) { }
~Derived() { }
void display() const { std::cout << Base< T >::get() <<
std::endl; }
};

} // namespace

int main()
{
Outer::Derived< int d0;
Outer::Derived< int d1(100);

d0.display();
d1.display();

return 0;
}

Sep 12 '06 #2
Hi Steve,

g++ probably is right. Qualifying as m_n does satisfy it. Comeau also
agrees with g++. One could argue that In the context of Outer, Base
isn't dependent on a template (as none of the other members are) and
so m_n should be found unqualified, but that doesn't appear to be how
it's generally implemented.

Does anyone know the relevant passages in the standard?

Jens
Sep 12 '06 #3

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

Similar topics

21
by: Jay Levitt | last post by:
I'm just starting to play around with CSS and MovableType. My home page (http://www.jay.fm) now validates on both the CSS and the XHTML. However, the Google cached version shows the wrong font in...
7
by: Jerry Krinock | last post by:
I've declared a class that has some std::vector data members like this: class MyClass { public: ... std::vector<Apples> apples ; ... private: ...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
5
by: Krisnamourt Correia via SQLMonster.com | last post by:
I have one query that executes many times in a week. I created one Maintenances plan that Rebuild all index in my Database that has been executed at 23:40 Saturday until stop finished at Sunday. ...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
3
by: Soren Jorgensen | last post by:
Hi, Following code should give the number of weeks in years 1998-2010 for a Danish calendar (on a Danish box) GregorianCalendar cal = new GregorianCalendar(); for(int i = 1998; i < 2010; i++)...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
3
by: belton180 | last post by:
CODE]../* Program function: Simulate the stack using a stack limit of 10. Display a menu for the the following. Create a stack Insert an item in the stack Pop an item from the stack ...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.