no match for 'operator='

Hi, im new to c++ and im coming across a problem where i am trying to add a class to an array, here is my code, its a bit mombo jombo because i had to remove some parts of the code but whats there is me trying to add an object to an array

so now the code:


#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
string name;
void addPassenger();
void printPassenger();
Person();
Person& Person::operator = (Person &passenger1);
};
Person passenger;
void Person::addPassenger()
{

cin >> passenger.name;
}

void Person::printPassenger()
{
cout << passenger.name;
}

Person& Person::operator = (Person &passenger1)
{
return *this;
}

Person::Person()
{}

int main()
{
Person passengerDatabase[3];
for (int i = 0; i < 3; i++){
passengerDatabase[i].addPassenger();}
for (int j = 0; j < 3; j++)
passengerDatabase[j].printPassenger();
cout << endl;
system("pause");
return 0;
}


if anybody can help i will be much obliged :)
Which line produces the error? I don't see operator = being called anywhere at all..
Here:
Person& Person::operator = (Person &passenger1);
You are using the scope resolution operator within your class.
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
class Person
{
public:
string name;
void addPassenger();
void printPassenger();
Person();

Person& Person::operator = (Person &passenger1);
};
Person passenger;
void Person::addPassenger()
{
	cin >>  name;
}

void Person::printPassenger()
{
	cout << name;
}

Person& Person::operator =(Person &passenger1)
{

	this->name = passenger1.name;
	return *this;
}


Person::Person()
{
}


int main()
{
Person passengerDatabase[3];
for (int i = 0; i < 3; i++){
passengerDatabase[i].addPassenger();}
for (int j = 0; j < 3; j++)
passengerDatabase[j].printPassenger();
cout << endl;
system("pause");
return 0;
}
woop sorry i had removed a bit of code that made it work, in the first while loop its meant to be
{
passenger = new Person;
passengerDatabase[i].addPassenger();
}
wait a minute IT WORKS!!!! FINALLY like 4 hours wasted :( hahaha thank u so much now my summer wont be wasted lol thank u so much
annoyingly now i came across another problem implementing the previous idea into my main program as follows:


#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

class Person
{
public:
Person();
string forename, surname, passportNum;
int depDay, depMonth, depYear, arrDay, arrMonth, arrYear;
string seatRow;
int seatColumn;
string flightNumber;
double price;
string passengerClass;
void addPassenger(Person &passenger);
void printPassenger(Person &passenger);
Person& Person::operator =(Person &passenger1);
};

Person passenger;

Person::Person()
{}

void Person::addPassenger(Person &passenger)
{
cout << "Enter Passenger Name: ";
cin >> passenger.forename >> passenger.surname;
cout << endl;
cout << "Enter Passport Number: ";
cin >> passenger.passportNum;
cout << endl;
cout << "Date of departure (day month year): ";
cin >> passenger.depDay >> passenger.depMonth >> passenger.depYear;
cout << endl;
cout << "Date of arrival (day month year): ";
cin >> passenger.arrDay >> passenger.arrMonth >> passenger.arrYear;
cout << endl;
cout << "Passenger flight number: ";
cin >> passenger.flightNumber;
cout << endl;
cout << "Passenger class: ";
cin >> passenger.passengerClass;
cout << endl;
}

void Person::printPassenger(Person &passenger)
{
cout << "Name: " << passenger.surname << ", " << passenger.forename << endl;
cout << "Passport No.: " << passenger.passportNum << endl;
cout << "Date of Departure: " << passenger.depDay << "/" << passenger.depMonth << "/" << passenger.depYear << endl;
cout << "Date of Arrival: " << passenger.arrDay << "/" << passenger.arrMonth << "/" << passenger.arrYear << endl;
cout << "Flight Number: " << passenger.flightNumber << endl;
cout << "Passenger Class: " << passenger.passengerClass << endl;
}

Person& Person::operator =(Person &passenger1)
{
this->forename = passenger1.forename;
this->surname = passenger1.surname;
this->passportNum = passenger1.passportNum;
this->depDay = passenger1.depDay;
this->depMonth = passenger1.depMonth;
this->depYear = passenger1.depYear;

return *this;
}

int main()
{
string repeat;
int i = 0;
int j;
Person passengerDatabase[10];
do
{

cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add another Passenger\n"
<< "- Press 1 to display most recent added Passenger\n"
<< "- Press 2 to view a Passenger on the allocated flight\n"
<< "- Press 3 to delete most recent Passenger\n"
<< "- Press 4 to delete a Passenger\n"
<< "- Press 5 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:

passengerDatabase[i].addPassenger(passenger);
i++;
break;
case 1:
passengerDatabase[i].printPassenger(passenger);
break;
case 2:
for (int j = 0; j < i; j++)
passengerDatabase[j].printPassenger(passenger);
break;
case 3:
break;
case 4:
break;
case 5:
exit(1);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
}
while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
system("pause");
return 0;
}


i think it could be something to do with the iteration of my program but im a bit confused on how to get around to fixing it, the program adds an object, then if i add another, the first object that was input is replaced by the second object input, any ideas? :/
:)
ugh sorry, keep confusing myself, this is my main code that seems to have the issue of the iteration:



#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

class Person
{
public:
Person();
string forename, surname, passportNum;
int depDay, depMonth, depYear, arrDay, arrMonth, arrYear;
string seatRow;
int seatColumn;
string flightNumber;
double price;
string passengerClass;
void addPassenger(Person &passenger);
void printPassenger(Person &passenger);
};

Person passenger;

Person::Person()
{}

void Person::addPassenger(Person &passenger)
{
cout << "Enter Passenger Name: ";
cin >> passenger.forename >> passenger.surname;
cout << endl;
cout << "Enter Passport Number: ";
cin >> passenger.passportNum;
cout << endl;
cout << "Date of departure (day month year): ";
cin >> passenger.depDay >> passenger.depMonth >> passenger.depYear;
cout << endl;
cout << "Date of arrival (day month year): ";
cin >> passenger.arrDay >> passenger.arrMonth >> passenger.arrYear;
cout << endl;
cout << "Passenger flight number: ";
cin >> passenger.flightNumber;
cout << endl;
cout << "Passenger class: ";
cin >> passenger.passengerClass;
cout << endl;
}

void Person::printPassenger(Person &passenger)
{
cout << "Name: " << passenger.surname << ", " << passenger.forename << endl;
cout << "Passport No.: " << passenger.passportNum << endl;
cout << "Date of Departure: " << passenger.depDay << "/" << passenger.depMonth << "/" << passenger.depYear << endl;
cout << "Date of Arrival: " << passenger.arrDay << "/" << passenger.arrMonth << "/" << passenger.arrYear << endl;
cout << "Flight Number: " << passenger.flightNumber << endl;
cout << "Passenger Class: " << passenger.passengerClass << endl;
}


int main()
{
string repeat;
int i = 0;
int j;
Person passengerDatabase[10];
do
{

cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add another Passenger\n"
<< "- Press 1 to display most recent added Passenger\n"
<< "- Press 2 to view a Passenger on the allocated flight\n"
<< "- Press 3 to delete most recent Passenger\n"
<< "- Press 4 to delete a Passenger\n"
<< "- Press 5 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:

passengerDatabase[i].addPassenger(passenger);
i++;
break;
case 1:
passengerDatabase[i].printPassenger(passenger);
break;
case 2:
for (int j = 0; j < i; j++)
passengerDatabase[j].printPassenger(passenger);
break;
case 3:
break;
case 4:
break;
case 5:
exit(1);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
}
while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
system("pause");
return 0;
}
When you add an object it doesnt look like i changes so you keep adding to first array element.
You need to iterate through the array for an unused location before adding.
ok and any ideas how its done? :/ pretty sure im stuck on this for 5 hours :(
Sorry I was worng about the index. It looks like your not really adding the passenger to the array.
Your add function changes the values of the global passanger object but I dont think your adding it.

Change
1
2
3
4
5
case 0:

passengerDatabase[i].addPassenger(passenger);
i++;
break;

to
1
2
3
4
5
6
case 0:

passengerDatabase[i].addPassenger(passenger);
passengerDatabase[i] = passenger;
i++;
break;

But you still have issues with the print function.
Last edited on
yeah not sure if it fixed it or not because of the print statement :/
thanks for the help tho
Change passengerDatabase[j].printPassenger(passenger);
to passengerDatabase[j].printPassenger(passengerDatabase[j]);
still the same result, would it be easier if i worked with linked lists?
naraku you were right, i had replaced a different case print message (silly me) but now i finally managed to make it work :) i owe you many thanks naraku and everyone else thank you so much :)

just if anyone is curious how the code works here it is:



#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

class Person
{
public:
Person();
string forename;
string surname;
string passportNum;
int depDay;
int depMonth;
int depYear;
int arrDay;
int arrMonth;
int arrYear;
string seatRow;
int seatColumn;
string flightNumber;
double price;
string passengerClass;
void addPassenger(Person &passenger);
void printPassenger(Person passenger);
};

Person passenger;

Person::Person()
{}

void Person::addPassenger(Person &passenger)
{
cout << "Enter Passenger Name: ";
cin >> passenger.forename;
cin >> passenger.surname;
cout << endl;
cout << "Enter Passport Number: ";
cin >> passenger.passportNum;
cout << endl;
cout << "Date of departure (day month year): ";
cin >> passenger.depDay;
cin >> passenger.depMonth;
cin >> passenger.depYear;
cout << endl;
cout << "Date of arrival (day month year): ";
cin >> passenger.arrDay;
cin >> passenger.arrMonth;
cin >> passenger.arrYear;
cout << endl;
cout << "Passenger flight number: ";
cin >> passenger.flightNumber;
cout << endl;
cout << "Passenger class: ";
cin >> passenger.passengerClass;
cout << endl;
}

void Person::printPassenger(Person passenger)
{
cout << "Name: " << passenger.surname << ", " << passenger.forename << endl;
cout << "Passport No.: " << passenger.passportNum << endl;
cout << "Date of Departure: " << passenger.depDay << "/" << passenger.depMonth << "/" << passenger.depYear << endl;
cout << "Date of Arrival: " << passenger.arrDay << "/" << passenger.arrMonth << "/" << passenger.arrYear << endl;
cout << "Flight Number: " << passenger.flightNumber << endl;
cout << "Passenger Class: " << passenger.passengerClass << endl;
}


int main()
{
string repeat;
int i = 0;
int j;
Person passengerDatabase[10];
do
{

cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add another Passenger\n"
<< "- Press 1 to display most recent added Passenger\n"
<< "- Press 2 to view a Passenger on the allocated flight\n"
<< "- Press 3 to delete most recent Passenger\n"
<< "- Press 4 to delete a Passenger\n"
<< "- Press 5 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:
passengerDatabase[i].addPassenger(passenger);
passengerDatabase[i] = passenger;
i++;
break;
case 1:
break;
case 2:
for (int j = 0; j < i; j++)
passengerDatabase[j].printPassenger(passengerDatabase[j]);
break;
case 3:
break;
case 4:
break;
case 5:
exit(1);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
}
while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
system("pause");
return 0;
}
Topic archived. No new replies allowed.