473,387 Members | 1,365 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,387 software developers and data experts.

Strange template problem.

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
2 1573
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: muser | last post by:
I have written a function that checks the first four characters in an address are valid; i.e. 1d2 sour st would prove to be invalid. bool checkdigitsinaddress( const char* string ) { for( int...
4
by: Tjerk Wolterink | last post by:
I've xml code like this: roles.xml: <?xml version="1.0" encoding="ISO-8859-1"?> <roles xmlns="http://www.wolterinkwebdesign.com/xml/roles"> <!-- ! The admin role. ! And admin should have...
2
by: Gandu | last post by:
Could some C++ guru shed some light as to what might be going on? I have the following two classes which are simply data containers: const unsigned int MAXSIZE = 40; class DataItem{ char...
5
by: sriram | last post by:
I have the following: class BSA { ... ... ... ... public: enum VRGAttrId {
3
by: rob.guitar.rob | last post by:
Hello, My last few posts have been revolving aroung the same problem, and I still cant solve it and I would be really appreciate if anyone could spot a problem. a section of my XML goes like...
4
by: Nate Murray | last post by:
Hey all, I'm having a strange PHP (4.3.10) problem that seems to have something to do with javascript. This is a bit complex, so hold on to your hats. My issue is that a single function is...
11
by: Gary li | last post by:
Hi.all look coding as below please: class A { public: template<class Tcreate(){return new T;} }; int main() { A a;
1
by: stromhau | last post by:
Ok, i have a file with main and an additional .cpp file i include in the main file but i get a lot of strange warnings when including. Both files compile just great separately. It seems that it have...
3
by: Zheng Da | last post by:
Hello, I want a use the class of map in a class with template, and the type of elements in map is specified by the parameter of the class's template. For example, template <typename key,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.