no match for 'operator<<' in 'std::cout << (&it)->_

Pages: 12
what is wrong with my code . it is the part where I use the classes

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
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include "database.h"


using namespace std;



int main()
{ 
    string a;
int exit;                     
string letter;           
int i = 1;             
int n_l=0, n_r=0, n_e=0; 
string line;
    string firstname;
    string lastname;
    string email_;
    string age_;
    string gender_; 
    string optionn;
    player dname;
    
    
    
    
    //***the main question***//
    thestart:
    cout<< "1-Add a player" << endl;    // program's question
    cout<< "2-list players" << endl;    
    cout<< "3-play game" << endl;
    cout<< "Q-Quit" << endl;
    cin >> optionn;
               
option:
if (optionn== "1")  // if the variable equation equals 1
{                     

goto addplayer ;    // going to the option 1 add aplayer 
}

else if (optionn== "2")    // going to the option 2 list player 
{
goto listplayer ;
}

else if (optionn== "3")    // going to the option 3 playgame 
{
goto playgame;
}


else if (optionn== "q")    // exit 
{
goto end;
}


else 
{
cout <<"Error:Invalid option."<<endl;
goto thestart;    //going to the beginning and repeating all the previous steps 
}
  


//*** option one***//
{
addplayer: // lable of goto command


//***** player information ***//    
//*** connect to the class that deal with player's information ***//
cout<< "please enterplayer information"<<endl;
         
    
  player data;
  data.print(firstname,email_,age_,gender_);
  database savem;
  savem.save(dname);
  
  
   goto thestart;

}
 return 0;
}

  


this my player.h
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
  #ifndef player_H
#define player_H
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <string>

using namespace std;


class player {
      
	private:     
	string name,email,gender;
    	string age;
    
  	public:
	//*** class members ***//
        
	void print(string,string,string,string);
	void player_(string);
	


  
};
#endif

 
 void player::print(string firstname, string email_, string age_, string gender_){
     
      
name = firstname;
email = email_;
age = age_;
gender= gender_;
	///**** saving all player's information ***///
	
	     cout<< "enter name"<<endl;
         cin>>name;
         cout<< "enter email"<<endl;
         cin>>email;
         cout<< "enter age"<<endl;
         cin>>age_;
         cout<< "enter gender"<<endl;
         cin>>gender; 
         
         cout<<" name: "<< name<<endl; 
         cout<<" email: "<< email<<endl;
	     cout<<" age: "<< age<<endl;
	     cout<<" gender: "<< gender<<endl;
 
      
           
    }
        
                  
     
        




this my database.h
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
  #include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include "player.h"
using namespace std;

class database
              {
                   private:
                           player firstname;
                           
                           
                   public:   
                          void save (player);
               };
               
               
void database::save(player dname)
{
     firstname = dname;
          vector<player>playersave;
           playersave.push_back(firstname);
        vector<player>::iterator it; 
           for( it = playersave.begin(); it != playersave.end(); ++it )
		{
           cout<<*it;
           
            }
           
}




this my third week with c++
Your player class does not provide operator<< and you are trying to invoke it anyway on line 29 of database.h
Hi
Thanks for replay, but what should I do to make it work correctly
a) provide operator<<
or
b) Use some other function to output yout variable.
closed account (j3Rz8vqX)
Good job so far.

Possible solution: (Example):
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
#include <iostream>
#include <vector>
using namespace std;
class Player{
public:
    int x, y;
    Player()
    {
        x=y=0;
    }
    friend ostream& operator<< (ostream &theStream, Player &P)
    {
        theStream<<"x: "<<P.x<<endl;
        theStream<<"y: "<<P.y<<endl;
        return theStream;
    }
};
int main()
{
    Player P;
    vector<Player> player;
    player.push_back(P);

    vector<Player>::iterator it;

    for( it = player.begin(); it != player.end(); ++it )
    {
        cout<<*it;
    }
    return 0;
}

Acquire what you need.
1) You cannot define friend function at the place of declaration.
2) output operator should be non-member
3) it should take object to output by const reference
4) Herb Sutter and Scott Meyers advise you to refrain of friend functions and do not make output operator one

5) Good job so far Bunch of gotos are prime example of good job, sure.

I would suggest to remade your print function so it would really just print everything and not "assign and print" like now
thanks;
but I do not get it, as I told before this is my third week.
I need to use the vector in the database.h
Ok. Change your print function to this:
1
2
3
4
5
6
7
void player::print()
{
    cout<<" name: "<< name<<endl; 
    cout<<" email: "<< email<<endl;
    cout<<" age: "<< age<<endl;
    cout<<" gender: "<< gender<<endl;
}

use it instead of << operator like
1
2
for( it = player.begin(); it != player.end(); ++it )
    it -> print();

create new function for input and name it say, get_data() (and use it on line 84 of main.cpp instead of print())

Read some articles on flow control and get rid of gotos
my code so far

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 


/
#include <iostream>
#include <ostream>

#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include "database.h"


using namespace std;



int main()
{ 
    string a;
int exit;                     
string letter;           
int i = 1;             
int n_l=0, n_r=0, n_e=0; 
string line;
    string firstname;
    string lastname;
    string email_;
    string age_;
    string gender_; 
    string optionn;
    player dname;
    
    
    
    
    //***the main question***//
    thestart:
    cout<< "1-Add a player" << endl;    // program's question
    cout<< "2-list players" << endl;    
    cout<< "3-play game" << endl;
    cout<< "Q-Quit" << endl;
    cin >> optionn;
               
option:
if (optionn== "1")  // if the variable equation equals 1
{                     

goto addplayer ;    // going to the option 1 add aplayer 
}

else if (optionn== "2")    // going to the option 2 list player 
{
goto listplayer ;
}

else if (optionn== "3")    // going to the option 3 playgame 
{
goto playgame;
}


else if (optionn== "q")    // exit 
{
goto end;
}


else 
{
cout <<"Error:Invalid option."<<endl;
goto thestart;    //going to the beginning and repeating all the previous steps 
}
  


//*** option one***//
{
addplayer: // lable of goto command
player get_datam;
get_datam.get_data();
//***** player information ***//    
//*** connect to the class that deal with player's information ***//

         
    
  player data;
  data.print();
  database savem;
  savem.save(dname);
  
  
   goto thestart;

}
//**************************//


//****option two***//

//*** list all plyers who has been saved in the text file listplayers ***//
{listplayer:
database savem;
  savem.save(dname);

goto thestart;
//**************************//


}
//*** option three ***//
//*** program game ***//

{   
playgame:
cout<<i<< ".Enter a letter if Left or Right is greater/equal: L  E  R" << endl;    // program's question
cout<< "Q:Quit" << endl;    // i is the number of the question that start from 1 to the last number of a right input(l,e,or r)
cin >> letter;
               

if (letter=="l"||letter=="L"||letter=="r"||letter=="R"||letter=="e"||letter=="E")  // if the variable letter equals l,L,r,R,e,or E do the next command
{                     
cout<< "Answer:"<<letter<< endl;  
i = i + 1 ;    // changing the number of the question to the next number 
if (letter=="l"||letter=="L")   //counting how many the letter l has been entered
{
n_l = n_l + 1;
}

else if (letter=="r"||letter=="R")   //counting how many the letter r has been entered
{
n_r = n_r+1 ;
}

else   //counting how many the letter e has been entered
{
n_e = n_e + 1 ;
}
goto playgame ;    // going to the playgame and repeating all the previous steps 
}

else if (letter=="q"||letter=="Q")    // print the result and exit from the program 
{
i = i-1 ;    // uncounting the the question if the answer is q
cout <<i<<"questions:    "<<n_l<<"L    "<<n_e<<"E    "<<n_r<<"R"<<endl;
cout << "Enter any thing to quit" << endl;
cout << "bye" << endl;
cin>> exit;
}

else 
{
cout <<"Error:Invalid option. Select L-E-R"<<endl;
goto playgame;    //going to the beginning and repeating all the previous steps 
}
}

//***********************//

end:
  return 0;
}

  


player.h
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
  #ifndef player_H
#define player_H
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <string>

using namespace std;


class player {
      
	private:     
	string name,email,gender;
    	string age;
    
  	public:
	//*** class members ***//
        
	void print();
	void player_(string);
	void get_data();
	


  
};
#endif

 
 void player::get_data()
 { 
         cout<< "enter name"<<endl;
         cin>>name;
         cout<< "enter email"<<endl;
         cin>>email;
         cout<< "enter age"<<endl;
         cin>>age;
         cout<< "enter gender"<<endl;
         cin>>gender; 
         
      
      }
 
 
 void player::print(){
     
      

	///**** saving all player's information ***///
	
	     
         
         cout<<" name: "<< name<<endl; 
         cout<<" email: "<< email<<endl;
	     cout<<" age: "<< age<<endl;
	     cout<<" gender: "<< gender<<endl;
 
      
           
    }
        
                  


database.h

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
  #include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include "player.h"
using namespace std;

class database
              {
                   private:
                           player firstname;
                           
                           
                   public:   
                          void save (player);
               };
               
               
void database::save(player dname)
{
     cout<<"vector"<<endl;
     firstname = dname;
          vector<player>playersave;
           playersave.push_back(firstname);
        vector<player>::iterator it; 
           for( it = playersave.begin(); it != playersave.end(); ++it )
		{
          it -> print();
           
            }
           
}




but i got an empty output

1-Add a player
2-list players
3-play game
Q-Quit
1
enter name
aaaaaaaaaaa
enter email
aaaaaaaaaaaaa
enter age
aaaaaaaaaaaaa
enter gender
aaaaaaaaaaaaaaa
name:
email:
age:
gender:
vector
name:
email:
age:
gender:
1-Add a player
2-list players
3-play game
Q-Quit
Last edited on
1
2
3
get_datam.get_data(); //You are reading data in one player
data.print();  //Outputting another
savem.save(dname) //And saving third one 

You probably should use one player for all three operations
how can I do this
You... use... same variable in all operations?
1
2
3
get_datam.get_data();
get_datam.print(); 
savem.save(get_datam);
thank you so much that works fine, but the vector saves and shows just one player I need the the vector show all players when I choose add player more than one time
That is because you are creating bew vector each time when save() is called. Also you are creating new database each time you want to save something

1) make vector a member of database class.
2) make one instance of database persistend: declare it before doing anything lse in main()
closed account (j3Rz8vqX)
According to your original design:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class player{
    private:
    string name,email,gender;
    string age;

    public:
    //*** class members ***//

    void print();
    void player_(string);
    void get_data();

    friend ostream& operator<< (ostream &theStream, player &P);
};
ostream& operator<< (ostream &theStream, player &P)
{
    theStream<<" name: "<< P.name<<'\n';
    theStream<<" email: "<< P.email<<'\n';
    theStream<<" age: "<< P.age<<'\n';
    theStream<<" gender: "<< P.gender<<'\n';
    return theStream;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class database
{
    private:
    player firstname;
    
    
    public:
    void save (player);
};
void database::save(player dname)
{
    firstname = dname;
    vector<player>playersave;
    playersave.push_back(firstname);
    vector<player>::iterator it;
    for( it = playersave.begin(); it != playersave.end(); ++it )
    {
        cout<<*it;
    }
}

You were attempting to send objects of player to the output stream method. If so, you would need to befriend the overloaded stream method.

I cannot tell you how to implement your program nor will I do it for you, but the above will suffice for your original implementation. They're risks, however, as that anyone can print the data of your private data members using the "common" befriend function cout<<; thus countering the purpose of originally privatizing the data members.

@MiiNiPaa:
I do not find it necessary to scrap another persons work simply to give him a solution. If you choose to, that would be your choice.
Last edited on
If so, you would need to befriend the overloaded stream method.
Friendship is the strongest relationship, second only to the inheritance. It is best to avoid it. output operator usually implemented by member print function which either writes into passed stream or simply returns a string.

I do not find it necessary to scrap another persons work
Can you point where I "scrapped another person work"? All what I done is suggested to divide his function which does too many things into two according to one responcibility principle, also he fixed interface problem with it. Resulting code is cleaner, reusable and he haven't written any code aside from he already done.

Please, notice that he solved his output problem. Now he had another problem which will be eventually solved just by moving several lines around without any scrapping.
I am sorry my English is not that good. I know c++ as a shape not as a Terminology, so is this what yo mean by make vector a member of database class

1
2
3
4
5
                   public:   
                          
                          vector<player>datac();
              


I do not know how to do your second point
Last edited on
Guys I am sorry. I am new here and I do not cause problems for any one.
If my article so annoying please do not replay

I am sorry again.
closed account (j3Rz8vqX)
he haven't written any code aside from he already done

We tend to say things we do not mean, including myself, I apologize for "scrap another persons work", however my intentions were:
He had to modify his: main function, player class, and database class.
Ok. Change your print function to this:
1
2
3
4
5
6
7
void player::print()
{
    cout<<" name: "<< name<<endl; 
    cout<<" email: "<< email<<endl;
    cout<<" age: "<< age<<endl;
    cout<<" gender: "<< gender<<endl;
}


use it instead of << operator like
1
2
for( it = player.begin(); it != player.end(); ++it )
    it -> print();

create new function for input and name it say, get_data() (and use it on line 84 of main.cpp instead of print())

Read some articles on flow control and get rid of gotos
Changing quite a bit to simply have it our way.

You and I both know that he could had simply befriended the stream operator and with nearly no changes to his code, produce the same result.

Guys I am sorry. I am new here and I do not cause problems for any one.
If my article so annoying please do not replay

Nah, don't worry about it. We're expressing our views.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class database
{
    private:
    vector<player> playersave;//<-Vector of players


    public:
    void save (player);
};
void database::save(player dname)
{
    cout<<"vector"<<endl;
    
    playersave.push_back(dname);//<-Add player to your vector
    
    vector<player>::iterator it;
    
    for( it = playersave.begin(); it != playersave.end(); ++it )
    {
        it -> print();
    }

}

1
2
3
4
5
6
7
8
9
string line;
    string firstname;
    string lastname;
    string email_;
    string age_;
    string gender_; 
    string optionn;
    player dname;
    database savem;//<-Added a database here, called savem 

Ensure to remove the construction of database everywhere else in your code; I think line 90.
Last edited on
this my code now, but the program keeps printout just the player I have entered when I choose option 2 list player
player.h
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
#ifndef player_H
#define player_H
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <string>

using namespace std;


class player {
      
	private:     
	string name,email,gender;
    	string age;
    
  	public:
	//*** class members ***//
        
	void print();
	void player_(string);
	void get_data();
	


  
};
#endif

 
 void player::get_data()
 { 
              cout<< "enter name"<<endl;
         cin>>name;
         cout<< "enter email"<<endl;
         cin>>email;
         cout<< "enter age"<<endl;
         cin>>age;
         cout<< "enter gender"<<endl;
         cin>>gender; 
         
      
           }
 
 
 void player::print(){
     
      

	///**** saving all player's information ***///
	
	     
         
         cout<<" name: "<< name<<endl; 
         cout<<" email: "<< email<<endl;
	     cout<<" age: "<< age<<endl;
	     cout<<" gender: "<< gender<<endl;
 
      
           
    }
        
                  
     
        




database.h
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
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include "player.h"
using namespace std;

class database
              {
                   private:
                           player get_datam;
                           vector<player>playersave;
                           
                   public:   
                          void save (player);
                          void display (player);
                         
               };
               
void database::save(player get_datam)
{
     vector<player>playersave;
     cout<<"vector"<<endl;   
          
           playersave.push_back(get_datam);
           
        vector<player>::iterator it; 
           for( it = playersave.begin(); it != playersave.end(); ++it )
		{
          it -> print();
           
            }
}




main.cpp

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 


/
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include "database.h"


using namespace std;



int main()
{ 
    string a;
int exit;                     
string letter;           
int i = 1;             
int n_l=0, n_r=0, n_e=0; 
string line;
    string firstname;
    string lastname;
    string email_;
    string age_;
    string gender_; 
    string optionn;
    player get_datam;
    database savem;
    
    
    
    //***the main question***//
    thestart:
    cout<< "1-Add a player" << endl;    // program's question
    cout<< "2-list players" << endl;    
    cout<< "3-play game" << endl;
    cout<< "Q-Quit" << endl;
    cin >> optionn;
               
option:
if (optionn== "1")  // if the variable equation equals 1
{                     

goto addplayer ;    // going to the option 1 add aplayer 
}

else if (optionn== "2")    // going to the option 2 list player 
{
goto listplayer ;
}

else if (optionn== "3")    // going to the option 3 playgame 
{
goto playgame;
}


else if (optionn== "q")    // exit 
{
goto end;
}


else 
{
cout <<"Error:Invalid option."<<endl;
goto thestart;    //going to the beginning and repeating all the previous steps 
}
  


//*** option one***//
{
addplayer: // lable of goto command

get_datam.get_data();
//***** player information ***//    
//*** connect to the class that deal with player's information ***//
 get_datam.print();
  goto thestart;
}
//**************************//


//****option two***//

//*** list all plyers who has been saved in the text file listplayers ***//
{listplayer:


  savem.save(get_datam);
 goto thestart;
//**************************//


}
//*** option three ***//
//*** program game ***//

{   
playgame:
cout<<i<< ".Enter a letter if Left or Right is greater/equal: L  E  R" << endl;    // program's question
cout<< "Q:Quit" << endl;   
cin >> letter;
               

if (letter=="l"||letter=="L"||letter=="r"||letter=="R"||letter=="e"||letter=="E")  
{                     
cout<< "Answer:"<<letter<< endl;  
i = i + 1 ;    // changing the number of the question to the next number 
if (letter=="l"||letter=="L")   //counting how many the letter l has been entered
{
n_l = n_l + 1;
}

else if (letter=="r"||letter=="R")   //counting how many the letter r has been entered
{
n_r = n_r+1 ;
}

else   //counting how many the letter e has been entered
{
n_e = n_e + 1 ;
}
goto playgame ;    // going to the playgame and repeating all the previous steps 
}

else if (letter=="q"||letter=="Q")    // print the result and exit from the program 
{
i = i-1 ;    // uncounting the the question if the answer is q
cout <<i<<"questions:    "<<n_l<<"L    "<<n_e<<"E    "<<n_r<<"R"<<endl;
cout << "Enter any thing to quit" << endl;
cout << "bye" << endl;
cin>> exit;
}

else 
{
cout <<"Error:Invalid option. Select L-E-R"<<endl;
goto playgame;    //going to the beginning and repeating all the previous steps 
}
}

//***********************//

end:
  return 0;
}


I need to display all players
Last edited on
Pages: 12