Strange template problem.

J

Jason Heyes

The following program does not compile. Apparantly "t" is inaccessible.

#include <iostream>
using namespace std;

template <class T> class Foo
{
T t;
public:
Foo(T t_) : t(t_) { }
void print() const { cout << t << endl; }
friend Foo<T> operator+(int n, const Foo<T> &f);
};

template <class T>
Foo<T> operator+(int n, const Foo<T> &f)
{
return Foo<T>(n + f.t);
}

int main()
{
Foo<int> f1(3), f2(5);
(1 + f2).print();
return 0;
}

This one does compile.

#include <iostream>
using namespace std;

template <class T> class Foo
{
public:
T t;
Foo(T t_) : t(t_) { }
void print() const { cout << t << endl; }
};

template <class T>
Foo<T> operator+(int n, const Foo<T> &f)
{
return Foo<T>(n + f.t);
}

int main()
{
Foo<int> f1(3), f2(5);
(1 + f2).print();
return 0;
}

And so does this one.

#include <iostream>
using namespace std;

template <class T> class Foo
{
T t;
public:
Foo(T t_) : t(t_) { }
void print() const { cout << t << endl; }
friend Foo<T> operator+(int n, const Foo<T> &f);
};

Foo<int> operator+(int n, const Foo<int> &f)
{
return Foo<int>(n + f.t);
}

int main()
{
Foo<int> f1(3), f2(5);
(1 + f2).print();
return 0;
}

Strange huh? Can someone please explain why the first program doesn't
compile? I can rewrite it into an equivalent version that does compile but I
am curious as to why that version does not. I am using VC++ 6.0. Thanks.
 
T

tom_usenet

The following program does not compile. Apparantly "t" is inaccessible.

#include <iostream>
using namespace std;

template <class T> class Foo
{
T t;
public:
Foo(T t_) : t(t_) { }
void print() const { cout << t << endl; }
friend Foo<T> operator+(int n, const Foo<T> &f);

The above declares a non-template operator. I suspect this isn't what
you meant. See

http://gcc.gnu.org/faq.html#friend
Strange huh? Can someone please explain why the first program doesn't
compile? I can rewrite it into an equivalent version that does compile but I
am curious as to why that version does not. I am using VC++ 6.0. Thanks.

VC++6 has very poor template support, so the faq above might not help.
Try in an MSVC specific group for workarounds for that compiler. For a
quick suggestion, try defining the friend function inside the class
declaration.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
D

Dietmar Kuehl

Jason Heyes said:
The following program does not compile. Apparantly "t" is inaccessible.

Sure: it is inaccessible!
#include <iostream>
using namespace std;

template <class T> class Foo
{
T t;

't' is made private here...
public:
Foo(T t_) : t(t_) { }
void print() const { cout << t << endl; }
friend Foo<T> operator+(int n, const Foo<T> &f);

.... and this declares the non-template function 'operator+()' with certain
arguments to be a friend...
};

template <class T>
Foo<T> operator+(int n, const Foo<T> &f)
{

.... while the implementation is actually a template...
return Foo<T>(n + f.t);

.... and hence 't' is inaccessible.

You probably wanted to make the template 'operator+()' a friend. Since I'm
personally using friends only very rarely I'm a little bit rusty on what
is needed to make it legal but I think this should do:

template <class T> class Foo;
template <class T> Foo<T> operator+(int, Foo<T> const&);
template <class T> class Foo {
T t;
friend Foo<T> operator+ <T>(int, Foo<T> const&);
// ...
};

Quite painful but at least doable. However, it is a good argument to avoid
friends :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top