problems with overload operator >>

I wrote this program. Note that Date is another class that i have implemented.
Compiler finds error in function read of Libretto about "ss >> riga" and in the overload of the oparator >> in Entry class.
Help me pls, i don't know what I have to do !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #ifndef ENTRY_H_INCLUDED
#define ENTRY_H_INCLUDED
#include "Date.h"
#include <vector>
#include <istream>

template <typename T>
class Entry
{
public:
    Entry(){};
    Entry(Date d, string description, T amount )
    {
        _date=d;
        _description=description;
        _amount=amount;

    }
//getters
Date date () const;
string description ()const;
T amount () const;

string str() const;
 friend istream& operator >>(istream& is, Entry <T> &dest){
        is >> dest._date;
        is.ignore();
        is >> dest._description;
        is.ignore();
        is >> dest._amount;

        return is;
    }


private:
    Date _date;
    string _description;
    T _amount;
};




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef LIBRETTO_H_INCLUDED
#define LIBRETTO_H_INCLUDED
#include "Entry.h"
#include <fstream>
#include <cstring>
template <typename T>
class Libretto
{
public:
    Libretto(){};
    void readFile(string filename);

private:
    vector <Entry <T> > _v;
    ifstream fin;
    int _day,_month,_year;
    string _desc;
    T _amount;

};

template <typename T>
void Libretto<T>::readFile(string filename)
{
    string line;
    stringstream ss;
    Entry <float> riga;
    fin.open(filename.c_str());
    if(!fin.is_open())
    {
        cerr<<"errore!"<<endl;
    }
    while(getline(fin, line))
    {
        ss << line;
        ss >> riga;
        _v.push_back(riga);

    }
    fin.close();
    size_t i;
   for(i=0; i<_v.size(); i++)
   {
       cout<<_v[i].str()<<endl;
   }
}



#endif // LIBRETTO_H_INCLUDED 
Hi, Faggio.

It's not easy to give opinions about such a small piece of code that can't even compile, but I'll try to pinpoint a couple of potential issues.

friend istream& operator >>(istream& is, Entry <T> &dest)
If the function is friend, it can't be part of the class. So I think you should move the definition outside the class.

This could help also in the error you get about ss >> riga; .

Other weird things are the fact that you declare for example
string _desc
but you didn't seem to include <string> (but you include <cstring>... to what avail, if I may ask?).

BTW, why do you declare "i" outside the for loop?
1
2
size_t i;
for(i=0; i<_v.size(); i++)

Do you have a further use for it?

I'm bound to confess I find it confusing to have functions and variable with the same names, apart from an underscore... Do you find it easy to read? Well, that's just a matter of tastes, I presume.

I think you ought be better not to declare using namespace std; , as you are likely to have done somewhere.

Hope that could be of some help.
I'm very greatfull for your answer and I understand your difficulties to read this piece of code that is a part of a larger project.
Infact I include <string> in another class(Data) that is included in Entry and ,so , also in Libretto.
I prefer to write the declaration of i outside the for loop.
I try to place the definition of friend function outside the class but it doesn't work.
So i replace the definition inside and i write <float>inside of <T> in friend function: now the only one error that compiler gives me is:
error: no match for 'operator >>' in 'is >> dest._date'



> Compiler finds error
¿what error?

> If the function is friend, it can't be part of the class. So I think you
> should move the definition outside the class.
there is no problem in writing the definition there, and in some cases (like templates) may be preferred.

> now the only one error that compiler gives me is:
> error: no match for 'operator >>' in 'is >> dest._date'
¿have you overloaded the operator>> for Date?


> Infact I include <string> in another class(Data) that is included in Entry
try to avoid that.
Suppose that later `Date' doesn't need string anymore, then you'll get compiler errors in Entry.
If you need to use something, then include the appropriate header.
there is no problem in writing the definition there, and in some cases (like templates) may be preferred.

Didn't know that!
Thanks a lot, @ne555, I was giving a wrong answer.
Thank you a lot @ne555!! I had to overload the operator in Date!!
thank you again!!
Topic archived. No new replies allowed.