473,725 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friend declaration declares a non-template function

I have the following code:

#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
template <class T>
class Stack{
public:
enum{DefaultSta ck = 10, EmptyStack = -1};
Stack();
Stack(int);
~Stack();
void push(const T&);
T pop();
T topNoPop() const;
bool empty() const;
bool full() const;
friend ostream& operator<<(ostr eam& os, const Stack<T>& s);
private:
T* elements;
int top;
int size;
void allocate(){
elements = new T[size];
top = EmptyStack;
}
void msg(char m[]) const{
cout << "*** " << m << " ***" << endl;
}
};

template<class T>
ostream& operator<<(ostr eam& os, const Stack<T>& s)

{
s.msg("Stack contents:");
int t = s.top;
while (t s.EmptyStack)
cout << s.elements[t--] << endl;
return os;
}

template<class T>
Stack<T>::Stack (){
size = DefaultStack;
allocate();
}

template<class T>
Stack<T>::Stack (int s){
if (s < 0)
s *= -1;
else if (s == 0)
s = DefaultStack;
size = s;
allocate();
}

template<class T>
Stack<T>::~Stac k(){
delete[] elements;
}

template<class T>
void Stack<T>::push( const T& e){
assert(!full()) ;
if (!full())
elements[++top] = e;
else
msg("Stack full!");
}

template<class T>
T Stack<T>::pop() {
assert(!empty() );
if (!empty())
return elements[top--];
else{
msg("Stack empty!");
T dummy_value;
return dummy_value; //return arbitrary value
}
}

template<class T>
T Stack<T>::topNo Pop() const{
assert(top EmptyStack);
if (!empty())
return elements[top];
else{
msg("Stack Empty!");
T dummy_value;
return dummy_value;
}
}

template<class T>
bool Stack<T>::empty () const{
return top <= EmptyStack;
}

template<class T>
bool Stack<T>::full( ) const{
return top + 1 >= size;
}
int main(){

Stack<charcStac k(20);

cout << cStack << endl; //empty stack
cout << "pushing A, B" << endl;
cStack.push('A' );
cStack.push('B' );
cout << cStack << endl; //BA
return 0;
}
When I compile with g++, there is error like:

stack.cpp:18: warning: friend declaration `std::ostream&
operator<<(std: :ostream&, const Stack<T>&)' declares a non-template
function
stack.cpp:18: warning: (if this is not what you intended, make sure the
function template has already been declared and add <after the
function name here) -Wno-non-template-friend disables this warning
/tmp/ccrvlcDf.o(.tex t+0x13f): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<char&, Stack<charconst &)'
/tmp/ccrvlcDf.o(.tex t+0x1b8): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<char&, Stack<charconst &)'
collect2: ld returned 1 exit status
I think it's totally legal to make a friend function inside a template
class declaration, why there is an error like this?

Thank you.

Jan 26 '07 #1
2 11863
fd*******@gmail .com wrote:

(snipped the parts that are not relevant)
template <class T>
class Stack{
friend ostream& operator<<(ostr eam& os, const Stack<T>& s);
};

template<class T>
ostream& operator<<(ostr eam& os, const Stack<T>& s)

{
s.msg("Stack contents:");
int t = s.top;
while (t s.EmptyStack)
cout << s.elements[t--] << endl;
return os;
}
When I compile with g++, there is error like:

stack.cpp:18: warning: friend declaration `std::ostream&
operator<<(std: :ostream&, const Stack<T>&)' declares a non-template
function
stack.cpp:18: warning: (if this is not what you intended, make sure the
function template has already been declared and add <after the
function name here) -Wno-non-template-friend disables this warning
/tmp/ccrvlcDf.o(.tex t+0x13f): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<char&, Stack<charconst &)'
/tmp/ccrvlcDf.o(.tex t+0x1b8): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<char&, Stack<charconst &)'
collect2: ld returned 1 exit status
I think it's totally legal to make a friend function inside a template
class declaration, why there is an error like this?
Exactly for the reason that the compiler told you. Your "friend declaration
`std::ostream& operator<<(std: :ostream&, const Stack<T>&)' declares a
non-template function". To make your template operator a friend, you have
to "add <after the function name here".

Jan 26 '07 #2
fd*******@gmail .com wrote:

(snipped the parts that are not relevant)
template <class T>
class Stack{
friend ostream& operator<<(ostr eam& os, const Stack<T>& s);
};

template<class T>
ostream& operator<<(ostr eam& os, const Stack<T>& s)

{
s.msg("Stack contents:");
int t = s.top;
while (t s.EmptyStack)
cout << s.elements[t--] << endl;
return os;
}
When I compile with g++, there is error like:

stack.cpp:18: warning: friend declaration `std::ostream&
operator<<(std: :ostream&, const Stack<T>&)' declares a non-template
function
stack.cpp:18: warning: (if this is not what you intended, make sure the
function template has already been declared and add <after the
function name here) -Wno-non-template-friend disables this warning
/tmp/ccrvlcDf.o(.tex t+0x13f): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<char&, Stack<charconst &)'
/tmp/ccrvlcDf.o(.tex t+0x1b8): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<char&, Stack<charconst &)'
collect2: ld returned 1 exit status
I think it's totally legal to make a friend function inside a template
class declaration, why there is an error like this?
Exactly for the reason that the compiler told you. Your "friend declaration
`std::ostream& operator<<(std: :ostream&, const Stack<T>&)' declares a
non-template function". To make your template operator a friend, you have
to "make sure the function template has already been declared and add <>
after the function name here".
I know that compilers often produce hard to understand messages, but this
one couldn't be any clearer.

Jan 26 '07 #3

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

Similar topics

6
8235
by: Tjerk Wolterink | last post by:
When i open the following xml file in internetexplorer: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE xc:content > <xc:xcontent xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent" xmlns="http://www.w3.org/1999/xhtml" module="gastenboek"> <xc:bericht> <xc:id>1</xc:id> <xc:naam type="string"><!]></xc:naam>
4
3825
by: marco_segurini | last post by:
Hi, the following test program shows a solution to a problem I have had. Now, this test program is compiled and linked by VS2003 and g++ while Comeau-on-line-compiler fails with this messages: "ComeauTest.c", line 21: error: constant "COuter::ID" is inaccessible int i = COuter::ID; ^
2
3986
by: humble04 | last post by:
Hi, I am compiling a collection of C++ code. Most of them are using the new format #include <iostream>. I think all of them because I failed at finding out which header file uses the old format #include <iostream.h>. However, I met the following error messages. "/usr/vacpp/include/iostream.h", line 74.7: 1540-0400 (S) "class ostream" has a conflicting declaration "../include/myfile.h", line 7.1: 1540-0424 (I) "ostream" is declared on
5
5012
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char, std::char_traits<char> >& << Thing&' operator friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&...
7
1946
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
3
4589
by: arnuld | last post by:
1) int i = 3; 2.) int* pi; 3.) int* pi2 = &i 4.) char* pc; 5.) char c; 6) char c2 = 'a' 7.) char* pc2 = &c2 8.) char* ps = "Stroustroup"; 9.) extern double d;
5
1822
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean? "laborated-type-specifiers and friend declarations may introduce a
1
2192
by: Alan Johnson | last post by:
Consider the following piece of code: // test.cpp class B ; namespace N { class A { friend class B ;
15
14910
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here after a long time since people here also helped me out with my lexer. thanking in anticipation.
6
3165
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend void func()
0
8748
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9393
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9248
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4506
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3212
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
2
2622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.