Problem overriding >> and << in a template class

Discussion in 'C++' started by, Mar 3, 2005.

  1. Guest

    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 {
    vector<Item> results;

    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;
    }else if(results < m){
    m = results;

    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;
    }else if(results > m){
    m = results;

    return m;

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

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

    cout << " >";

    return os;

    template <typename item>
    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;
    }else {
    //size = parseInt(s.substr(0,s[s.size()-1]));
    cout << count << " " << atof(s.c_str()) << endl;
    return is;

    int main(){

    vector<double> a;


    //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
    , Mar 3, 2005
    1. Advertising

  2. 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::eek:stream& operator<< (std::eek:stream&, foo<T> const&);

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

    template<typename T>
    std::eek:stream& operator<< (std::eek:stream& 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:> <>
    <> - Software Development & Consulting
    Dietmar Kuehl, Mar 3, 2005
    1. Advertising

  3. Guest

    thanks a lot for the help, i tried it and it worked. i really
    appreciate it and God bless you.
    , Mar 10, 2005
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Pete Becker
    May 4, 2005
  2. Replies:
    Gianni Mariani
    Jun 8, 2007
  3. Replies:
  4. A L
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. Peter Davis
    Peter Davis
    Jul 25, 2012

Share This Page