write vector to file

Hi, I have lots of problems in my program, but now I need some one to tell me what is wrong with writing the vector to the file I have tried this way out side the database class and it worked fine,but here it gives me

33 database.h no match for 'operator<<' in 'outf <<

My code is

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


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



using namespace std;




int main()
{ 
int exit;                     
int i =0 ;             
int n_l=0, n_r=0, n_e=0; 
    string letter;
    string line; 
    string optionn;
    player get_datam;
    database savem;
    
    
    //***the main question***//
    thestart:
    cout<< "1-select a player" << endl;          
    cout<< "2-Add a player" << endl;  
    cout<< "3-Remove a player" << endl; 
    cout<< "4-Retrieve player’s information" << endl;  
    cout<< "5-list players" << endl;    
    cout<< "6-play game" << endl;
    cout<< "Q-Quit" << endl;
    cin >> optionn;
               
option:
       
if (optionn== "1")    // going to the option 1 select a player 
{
goto selectplayer;
}      
else if (optionn== "2")  
{                     

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


else if (optionn== "3")  
{                     

goto removeplayer ;    // going to the option 3 remove a aplayer 
}

else if (optionn== "4")  
{                     

                         // going to the option 4 4. Retrieve player’s information
}

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

else if (optionn== "6")    // going to the option 6 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 ***//
{
selectplayer:
savem.list();             
goto thestart;  //going to the beginning and repeating all the previous steps//
}
//**************************//

//*** option two ***//
/* save and print player information     
by connecting to the player and database classes */
{
addplayer:               
	get_datam.get_data(); 
	get_datam.print();
	savem.save(get_datam);

goto thestart;  //going to the beginning and repeating all the previous steps//
}
//**************************//

//*** option two ***//
removeplayer:
savem.removep();
goto thestart;  //going to the beginning and repeating all the previous steps//


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

//**** option five***//
//*** display all saved players by connecting to database class ***// 
{
listplayer:
savem.display(get_datam);
goto thestart;  //going to the beginning and repeating all the previous steps//
}

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



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

{   
playgame:
goto thestart;
}
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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,age;
	vector<string>playersavep;
    
  	public:
	//*** class members ***//
        
	void print();
	void player_();
	void get_data();
	


  
};
#endif


 void player::player_()
 {    
      
      cout<<"Name: "<< name << endl;
      cout<<"************"<<endl;
      
      
      }

 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;
	 ofstream outf("tttt.txt",fstream::app);
	  playersavep.push_back(name);
  outf << playersavep.size() <<' '; // Notice the space, it's important
  for(long i=0;i<playersavep.size();i++)
  {
    outf << playersavep[i]<<endl;
    outf << " ";
  }
  outf.close();
	}

	void player::print()
	{
     
	 cout<<"Name: "<< name << endl;
	 cout<<"Email: "<< email << endl;
	 cout<<"Age: "<< age << endl;
	 cout<<"Gender: "<< gender << endl;
	 cout<<"*************************"<<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
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
166
167
168
169
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include <cstdio>
#include "player.h"
using namespace std;

class database
              {
                   private:
                           player get_datam;
                           vector<player>playersave;
                           
                   public:   
                          void save (player);
                          void display (player);
                          void list ();
                          void removep ();
                         
               };
               
void database::save(player get_datam)
	{
     
	 playersave.push_back(get_datam);
	 ofstream outf("tttt.txt",fstream::app);
  outf << playersave.size() <<' '; // Notice the space, it's important
  for(long i=0;i<playersave.size();i++)
  {
    outf << playersave[i]<<endl;
    outf << " ";
	 
	}

void database::display(player get_datam)
	{
  
	 vector<player>::iterator it;
	for(it = playersave.begin(); it != playersave.end(); ++it)
	{
	 it -> player_();
	 
	}
	}
//*** option one   "1-select a player"***//
void database::list()
     {
      string ps;
      string user;
      cout << "Select one from the following players by choosing his number" << endl;
      ifstream in("playerlist.txt");
      string temp;
      while(getline(in, temp))
      cout << temp << endl;
      in.close();
      cin>> ps;
      if (ps== "1")  
      {                     
       user= "George";
       cout << "the player has been selected" << endl;
       cout << "You have been redirected to the main menu" << endl;
      }

      else if (ps== "2")     
      {
       user= "Katherine"; 
       cout << "the player has been selected" << endl;
       cout << "You have been redirected to the main menu" << endl;   
      }
      else if (ps== "3")     
      {
       user= "Hang";
       cout << "the player has been selected" << endl;
       cout << "You have been redirected to the main menu" << endl;
      }

      else if (ps== "4")    
      {
       user= "Cindy";
       cout << "the player has been selected" << endl;
       cout << "You have been redirected to the main menu" << endl;
      }
      else 
      {
      cout <<"Error:Invalid option."<<endl;
      }

      }
//*****************************************************//
void database::removep()
     {
      string pr;
      string line;
      cout << "Remove one from the following players by choosing his number" << endl;
      ifstream in("playerlist.txt");
      ofstream out("outfile.txt");
      string temp;
      while(getline(in, temp))
      cout << temp << endl;
      in.close();
      cin>> pr;
      if (pr== "1")  
      {                     
       while( getline(in,line) )
    {
        if(line != "George")
            out << line << "\n";
    }
    in.close();
    out.close();   
    remove("playerlist.txt");
    rename("outfile.txt","playerlist.txt");
    cout << "the player has been removed" << endl;
    cout << "You have been redirected to the main menu" << endl;
      }

      else if (pr== "2")     
      {
        while( getline(in,line) )
    {
        if(line != "Katherine")
            out << line << "\n";
    }
    in.close();
    out.close();   
    remove("playerlist.txt");
    rename("outfile.txt","playerlist2.txt"); 
    cout << "the player has been removed" << endl;
    cout << "You have been redirected to the main menu" << endl; 
      }
      else if (pr== "3")     
      {
       while( getline(in,line) )
    {
        if(line != "Hang")
            out << line << "\n";
    }
    in.close();
    out.close();   
    remove("playerlist.txt");
    rename("outfile.txt","playerlist2.txt");
    cout << "the player has been removed" << endl;
    cout << "You have been redirected to the main menu" << endl;
      }

      else if (pr== "4")    
      {
       while( getline(in,line) )
    {
        if(line != "Cindy")
            out << line << "\n";
    }
    in.close();
    out.close();   
    remove("playerlist.txt");
    rename("outfile.txt","playerlist2.txt");
    cout << "the player has been removed" << endl;
    cout << "You have been redirected to the main menu" << endl;
      }
      else 
      {
      cout <<"Error:Invalid option."<<endl;
      }

      }      
The line that causes an error, contains:
outf << playersave[i]
Given:
1
2
vector<player>playersave;
ofstream outf;

We can now say that the line would like to call:
ostream & operator<< ( ostream &, const player & )
Alas, you don't have that.
sorry, but I do not understand what you mean, I am new to c++ and to English
You're trying to do outf << somePlayerObject;.
But outf doesn't know how to "<<" a player object.

So you need to either define what "<<" means for a player object or manually outf << each of the data members.
I did this
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
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include <cstdio>
#include "player.h"
using namespace std;


class database
              {
              friend ostream & operator<<( ostream &, const player & );
                   private:
                           player get_datam;
                           vector<player>playersave;
                           
                           
                   public:   
                          void save (player);
                          void display (player);
                          void list ();
                          void removep ();
                          
                         
               };
               
void database::save(player get_datam)
	{
     
	 playersave.push_back(get_datam);
	 ofstream outf("tttt.txt",fstream::app);
  outf << playersave.size() <<' '; // Notice the space, it's important
  for(long i=0;i<playersave.size();i++)
  {
    outf << playersave[i]<<endl;
    outf << " ";
    }
	}


but I got

C:\Users\hg\AppData\Local\Temp\ccgTdaaa.o(.text+0x9b1) In function `ZN8database4saveE6player':
[Linker error] undefined reference to `operator<<(std::ostream&, player const&)'
C:\Users\hg\AppData\Local\Temp\ccgTdaaa.o(.text+0x9b1) ld returned 1 exit status
The friend ostream & operator<<( ostream &, const player & ); part should probably go under the player class, not the database class.

Then you need to actually define it somewhere.
Then you need to actually define it somewhere.


what that means and how I can do it
Somewhere in your code, you need to have something like
1
2
3
4
5
ostream& operator<<(ostream& os, const player& p)
{
    // ...
    return os;
}
For starters, you can make this function do something similar to what you have in your player::print function (except with os instead of cout).
yes, yes, I got it, but now the problem is my file tttt.txt is empty
I did not find any thing inside it except numbers
closed account (j3Rz8vqX)
So, I am assuming you did the below:
1
2
3
4
5
ostream& operator<<(ostream& os, const player& p)
{
    os<<p.name<<' '<<p.email<<' '<<p.gender<<' '<<p.age;
    return os;
}
Topic archived. No new replies allowed.