473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1590
On Mon, 15 Dec 2003 21:32:21 +1100, "Jason Heyes"
<ge******@optus net.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******@optus net.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.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
Jul 22 '05 #3

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

Similar topics

9
2481
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 i =0; i<=1; i++ ) if( !isdigit( string )){} return false; for(i=1; i<=2; i++)
4
1535
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 all permisions to do its task !
2
1522
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 fname; char lname; char fname; char street;
5
2101
by: sriram | last post by:
I have the following: class BSA { ... ... ... ... public: enum VRGAttrId {
3
1734
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 ..... <parent>
4
3846
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 wrongly being called twice. The weird part is that no "print" functions are being repeated, but any "lower" functions, such as fwrite or...
11
1300
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
2704
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 something to do with the inclusions of header files. Here are the files i include in main.cpp file #include "SDL.h" // SDL: window & input...
3
1371
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, typename T> class hashmap { map<key , Ttable;
0
7496
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7941
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7784
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5354
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1916
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 we have to send another system

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.