I have the following code:
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
template <class T>
class Stack{
public:
enum{DefaultStack = 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<<(ostream& 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<<(ostream& 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>::~Stack(){
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>::topNoPop() 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<charcStack(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(.text+0x13f): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<char&, Stack<charconst&)'
/tmp/ccrvlcDf.o(.text+0x1b8): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<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. 2 11788 fd*******@gmail.com wrote:
(snipped the parts that are not relevant)
template <class T>
class Stack{
friend ostream& operator<<(ostream& os, const Stack<T>& s);
};
template<class T>
ostream& operator<<(ostream& 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(.text+0x13f): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<char&, Stack<charconst&)'
/tmp/ccrvlcDf.o(.text+0x1b8): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<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". fd*******@gmail.com wrote:
(snipped the parts that are not relevant)
template <class T>
class Stack{
friend ostream& operator<<(ostream& os, const Stack<T>& s);
};
template<class T>
ostream& operator<<(ostream& 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(.text+0x13f): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<char&, Stack<charconst&)'
/tmp/ccrvlcDf.o(.text+0x1b8): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:...
|
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 ...
|
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,...
|
by: Mark P |
last post by:
Is this legal and sensible?
class Outer
{
friend struct Inner
{
...
};
...
|
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;
|
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?...
|
by: Alan Johnson |
last post by:
Consider the following piece of code:
// test.cpp
class B ;
namespace N
{
class A
{
friend class B ;
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |