Printing vector problem.
Apr 17, 2011 at 3:34pm UTC
this is a part of my program. it seems tha in my printing function vprint has a problem
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
class Scedule
{
vector<Appointment> m_apps;
public :
void addAppointment(Appointment const & app)
{
m_apps.push_back(app);
}
//friend void vprint;
friend class Appointment;
friend class MyDate;
};
void vprint(vector <Appointment> m_apps,int i)
{
{
for (int i = 0; i < m_apps.size(); i++)
cout << m_apps[i] << endl; //problem
}
}
int main(int argc, char *argv[])
{
Appointment app;
Scedule mySced;
MyDate dt;
app.set_description("Ravtevou" );
dt.set_date("15/4/02" );
dt.set_hour("14:00" );
mySced.addAppointment(app);
vprint(app);
print(app,dt);
system("pause" );
return 0;
}
here is the message
C:\Documents and Settings\irini\ÅðéöÜíåéá åñãáóßáò\main.cpp In function `void vprint(std::vector<Appointment, std::allocator<Appointment> >, int)':
90 C:\Documents and Settings\irini\ÅðéöÜíåéá åñãáóßáò\main.cpp no match for 'operator<<' in 'std::cout << (&m_apps)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Appointment, _Alloc = std::allocator<Appointment>](((unsigned int)i))'
Apr 17, 2011 at 3:46pm UTC
Unless your class "Appointment" overloaded the "<<" operator then I expect you meant to print a certain data member of that class instead of the class itself.
Apr 17, 2011 at 3:52pm UTC
so i must specify the data that i want to print?i post my whole code. Sorry but i'm greek and i dont understand perfect english..
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Appointment;
class MyDate
{
private :
string date;
string hour;
public :
MyDate(){};//constructor xoris parametrous//
void set_date(string _date)
{
date=_date;
}
void set_hour(string _hour)
{
hour=_hour;
}
friend void print(Appointment &,MyDate &) ;
friend class Scedule;
};
class Appointment
{
private :
string description;
string date;
string hour;
public :
Appointment(){};//constructor xoris parametrous//
void set_description(string _description)
{
description=_description;
}
friend class MyDate;
friend class Scedule;
friend void print(Appointment &, MyDate &);
};
void print(Appointment &a,MyDate &d)
{
cout<<"description of appointment " <<a.description<<endl;
cout<<"date " <<d.date<<" hour " <<d.hour<<endl;
}
class Scedule
{
vector<Appointment> m_apps;
public :
void addAppointment(Appointment const & app)
{
m_apps.push_back(app);
}
friend class Appointment;
friend class MyDate;
};
void vprint(vector <Appointment> m_apps,int i)
{
{
for (int i = 0; i < m_apps.size(); i++)
cout << m_apps[i] << endl;
}
}
int main(int argc, char *argv[])
{
Appointment app;
Scedule mySced;
MyDate dt;
app.set_description("Ravtevou" );
dt.set_date("15/4/02" );
dt.set_hour("14:00" );
mySced.addAppointment(app);
vprint(app);
print(app,dt);
system("pause" );
return 0;
}
Apr 17, 2011 at 4:01pm UTC
Is "print(...)" on Line 59 intended to be a member function of the "Appointment" class? Because you need to Set it up as such if that's the case.
Apr 17, 2011 at 4:04pm UTC
this one
1 2 3 4 5 6 7 8
void print(Appointment &a,MyDate &d)
{
cout<<"description of appointment " <<a.description<<endl;
cout<<"date " <<d.date<<" hour " <<d.hour<<endl;
}
is for other use. does not create problem in compiling. the problem is to the function
1 2 3 4 5 6 7 8 9 10
void vprint(vector <Appointment> m_apps,int i)
{
{
for (int i = 0; i < m_apps.size(); i++)
cout << m_apps[i] << endl; //here is the compiling error
}
Apr 17, 2011 at 4:07pm UTC
The problem is that you are trying to display private data memebers of your "Appointment" class trough another class. I thought that your "print(...)" function was intended to do this but if it isn't then you need to write up a simular function to retrieve the data.
Apr 17, 2011 at 4:10pm UTC
thank you. I'll try to fix it!
Topic archived. No new replies allowed.