By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Strange template problem.

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 15 Dec 2003 21:32:21 +1100, "Jason Heyes"
<ge******@optusnet.com.au> wrote:
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
Jul 22 '05 #2

P: n/a
"Jason Heyes" <ge******@optusnet.com.au> wrote:
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 :-)
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.