473,698 Members | 1,967 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 11851
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
8233
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
3823
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
3980
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
5008
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
4585
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
1820
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
2190
by: Alan Johnson | last post by:
Consider the following piece of code: // test.cpp class B ; namespace N { class A { friend class B ;
15
14906
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
3163
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
8672
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
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
9018
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...
1
8890
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
5859
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
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
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
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.