is g++ wrong?

S

steve yee

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;
}
 
S

Salt_Peter

steve said:
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;
}
 
J

Jens Theisen

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
 

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
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top