473,320 Members | 2,080 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,320 software developers and data experts.

Problem with defining template friend function of a template class.

The following program works with g++3.3. I'm assuming g++-3.4 is more
standard conforming that g++-3.3. Would you please tell me what is
right way to declare a template friend function of a template class?

Thanks,
Peng

#include <iostream>

template <typename T>
class A{
private:
A(){}
T _a;
public:
friend A *makeA<T>();//error in g++-3.4, works with g++3.3
};

template <typename T>
A<T> *makeA(){
return new A<T>;
}

int main ( void ) {
makeA<int>();
}

Nov 9 '05 #1
2 1696
Pe*******@gmail.com wrote:
#include <iostream>

template <typename T>
class A{
private:
A(){}
T _a;
public:
friend A *makeA<T>();//error in g++-3.4, works with g++3.3
};

template <typename T>
A<T> *makeA(){
return new A<T>;
}

int main ( void ) {
makeA<int>();
}

The C++ standard states that if a template function is a friend of a
class then the function must be declared before the class definition.

Hence for this code to compile, you will need to add following two
lines before the class definition :

template <class T> class A; // declare the class since it is used in
function's prototype
template<class T> A<T>* makeA(); // declare the function

This would solve the problem.

Nov 9 '05 #2
Neelesh wrote:
Pe*******@gmail.com wrote:
#include <iostream>

template <typename T>
class A{
private:
A(){}
T _a;
public:
friend A *makeA<T>();//error in g++-3.4, works with g++3.3
};

template <typename T>
A<T> *makeA(){
return new A<T>;
}

int main ( void ) {
makeA<int>();
}


The C++ standard states that if a template function is a friend of a
class then the function must be declared before the class definition.

Hence for this code to compile, you will need to add following two
lines before the class definition :

template <class T> class A; // declare the class since it is used in
function's prototype
template<class T> A<T>* makeA(); // declare the function

This would solve the problem.


I don't know about gcc 3.4 but it's a good idea on some compilers to use
this very similar version

template <class T> class A;
template <class T> A<T>* makeA();

template <typename T>
class A
{
friend A* makeA<>();
};

Note that <> has replaced <T> in the friend declaration.

john
Nov 9 '05 #3

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

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
2
by: Jason Heyes | last post by:
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_) { }
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.