473,379 Members | 1,257 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,379 software developers and data experts.

Problem overriding >> and << in a template class

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. i would
forget overriding it but i have to do it because its a coursework. here
is a simple version of the class
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
template <typename Item>
class Sample {
private:
vector<Item> results;

public:
Sample(vector<Item> samps);

const vector<Item> get_data();

void set_data(const vector<Item> &v);

long double minimum();

long double maximum();

friend ostream& operator<<(ostream& os, const Sample& samp);

friend istream& operator>>(istream& is, Sample& samp);

};

template <typename item>
Sample<item>::Sample(vector<item> samps) : results(samps) {}

template <typename item>
const vector<item> Sample<item>::get_data(){
return results;
}

template <typename item>
void Sample<item>::set_data(const vector<item> &v) {
results = v;
}

template <typename item>
long double Sample<item>::minimum(){
long double m;
for(int i = 0; i < results.size(); i++){
if(i == 0){
m = results[i];
}else if(results[i] < m){
m = results[i];
}
}

return m;
}

template <typename item>
long double Sample<item>::maximum(){
long double m;
for(int i = 0; i < results.size(); i++){
if(i == 0){
m = results[i];
}else if(results[i] > m){
m = results[i];
}
}

return m;
}

template <typename item>
ostream&
operator<<(ostream& os, const Sample<item>& samp){
cout << "<" << samp.results.size() << ":";

for(int i = 0; i < samp.results.size(); i++){
cout << " " << samp.results[i];
}

cout << " >";
return os;
}

template <typename item>
istream&
operator>>(istream& is, Sample<item>& samp){
string s;
int count = 0;
int size;
while (is >> s){
if(count == 0 && s[0] == '<' && s[s.size()-1] == ':'){
//size = parseInt(s.substr(0,s[s.size()-2]));
size = atoi((s.substr(1,s.size()-2)).c_str());

cout << "begin size = " << size << endl;
}else if(s[s.size()-1] == '>'){
//size = parseInt(s.substr(0,s[s.size()-1]));
cout << "end " << endl;
count = 0;
break;
}else {
//size = parseInt(s.substr(0,s[s.size()-1]));
cout << count << " " << atof(s.c_str()) << endl;
samp.results.push_back(atof(s.c_str()));
}
count++;
}
return is;
}

int main(){

vector<double> a;

a.push_back(7);
a.push_back(11);
a.push_back(2);
a.push_back(13);
a.push_back(3);
a.push_back(5);

//string s;
Sample<double> samp(a);
while (cin >> samp){

cout << samp.minimum() << endl << samp.maximum() << endl;

}

cout << samp;

return 0;
}
Any help will be greatly appreciated

Jul 23 '05 #1
2 2405
fr*******@hotmail.com wrote:
hello people i. can anybody help me,


Interestingly, this looks very much like an article I recently rejected
from comp.lang.c++.moderated because it uses excessive code, i.e. it
includes loads of stuff entirely irrelevant to the question. Of course,
the rejection also mentioned the cause the of problem: The functions
you declare friends within the body of the class are non-template
functions and, except for the same name, entirely unrelated to the
function templates you use later!

For the friend functions to work correctly you need to declare them
*prior* to the class definition which in turn means that you also need
to forward declare the class definition itself. In addition, you need
to use a template argument list in the friend declaration. However,
since the template arguments can actually be deduced from the function
arguments, this template argument list can be empty. It just has to be
present to indicate that the function template is made a friend. The
whole code would look something like this:

#include <iostream>

template <typename> class foo;
template <typename T>
std::ostream& operator<< (std::ostream&, foo<T> const&);

template <typename T>
struct foo
{
foo(): val(17) {}
// ...
friend std::ostream& operator<< <>(std::ostream&, foo<T> const&);
private:
int val;
};

template<typename T>
std::ostream& operator<< (std::ostream& os, foo<T> const& f)
{
return os << f.val;
}

int main()
{
std::cout << foo<int>() << "\n";
}

BTW, this is not "overriding" the output operator but "overloading".
There is signification difference between these two terms although
they look very similar. Overriding is the term used to indicate that
a virtual function of a base class is, well, overridden by a derived
class, i.e. it is the mechanism used to implement dynamic polymorphism.
The overridden function has essentially the same signature as the
original on (although the return type may be covariant; theoretically
the arguments could be contravariant but this is not supported by C++).
Overloading on the other means that the same function name is used for
an otherwise unrelated function: the signatures of overloaded functions
vary be definition and there is no dynamic polymorphism involved. Of
course, the unrelated functions should to similar things when they
share a common name but this is technically not required.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2
thanks a lot for the help, i tried it and it worked. i really
appreciate it and God bless you.
franky

Jul 23 '05 #3

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

Similar topics

15
by: Philipp Lenssen | last post by:
My friend has the following problem (background: we want to transform XML to XHTML via XSLT): "We copy XHTML fragments into an output by using the following template: <xsl:template match="*"...
3
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
3
by: Jim in Arizona | last post by:
Most of the asp.net learning I've done has been from books that were written during the 1.0 framework. I didn't have a copy of visual studio when I started reading them then I got a hold of VS 2005...
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
36
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 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.