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

Help wanted on operator overloading

template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
/*Compiler says here, ';' shoud occur before ref,and ref is not
declared,but I really don't know what's the problem here*/
out << *ref;
return out;
}

Dec 11 '06 #1
6 1203

"Magcialking дµÀ£º
"
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
/*Compiler says here, ';' shoud occur before ref,and ref is not
declared,but I really don't know what's the problem here*/
out << *ref;
return out;
}
Have you include the header file <list>.
The following code works fine in my compiler.

#include <list>
#include <iostream>
using namespace std;

template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
std::cout << *ref << "\n";
return out;
}
int main()
{
list<inta;
a.push_back(1);
cout << a;
return 0;
}

Dec 11 '06 #2
I'm sure I left nothing.
And the code you give doesn't work in my compliler(Mingw)

"mimi дµÀ£º
"
"Magcialking дµÀ£º
"
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
/*Compiler says here, ';' shoud occur before ref,and ref is not
declared,but I really don't know what's the problem here*/
out << *ref;
return out;
}
Have you include the header file <list>.
The following code works fine in my compiler.

#include <list>
#include <iostream>
using namespace std;

template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
std::cout << *ref << "\n";
return out;
}
int main()
{
list<inta;
a.push_back(1);
cout << a;
return 0;
}
Dec 11 '06 #3
I'm sure I left nothing.
And the code you give doesn't work in my compliler(Mingw)

"mimi дµÀ£º
"
"Magcialking дµÀ£º
"
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
/*Compiler says here, ';' shoud occur before ref,and ref is not
declared,but I really don't know what's the problem here*/
out << *ref;
return out;
}
Have you include the header file <list>.
The following code works fine in my compiler.

#include <list>
#include <iostream>
using namespace std;

template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
std::cout << *ref << "\n";
return out;
}
int main()
{
list<inta;
a.push_back(1);
cout << a;
return 0;
}
Dec 11 '06 #4
Magcialking :
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
/*Compiler says here, ';' shoud occur before ref,and ref is not
declared,but I really don't know what's the problem here*/
out << *ref;
return out;
}
I have compile your code with mingw, and it produce the same
error! The root of the error is that the compiler look
list<T>::const_iterator as a dependent name. So you need to
do

for( typename list<T>::const_iterator ref ...)

and everything is OK!
Dec 11 '06 #5

"Bo Yang дµÀ£º
"
Magcialking :
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref !=
lst.end();ref++)
/*Compiler says here, ';' shoud occur before ref,and ref is not
declared,but I really don't know what's the problem here*/
out << *ref;
return out;
}
I am sorry. Maybe I should uninstall my VC 6.0 immediately.
I have compile your code with mingw, and it produce the same
error! The root of the error is that the compiler look
list<T>::const_iterator as a dependent name. So you need to
do

for( typename list<T>::const_iterator ref ...)

and everything is OK!
Dec 11 '06 #6
Thanx a lot, both of you!

Dec 12 '06 #7

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
8
by: John Hardin | last post by:
All: The syntax for overloading the "+" and other simple binary math operators is pretty straightforward, but I can't seem to wrap my brain around the idea that overloading the "+" operator...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
2
by: Steve R. Hastings | last post by:
While studying iterators and generator expressions, I started wishing I had some tools for processing the values. I wanted to be able to chain together a set of functions, sort of like the...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.