I got an error of
1 2
|
27a1.cpp:30:21: error: no viable overloaded '='
return a.stateName = b.stateName;
|
Below is my source code
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
|
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
class stateCity
{
public:
stateCity (const string& name= " ", const string& city = " ");
friend ostream& operator<< (ostream& ostr, const stateCity& state);
friend bool operator< (const stateCity& a, const stateCity& b);
friend bool operator== (const stateCity& a, const stateCity& b);
private:
string stateName, cityName;
};
ostream& operator<< (ostream& ostr, const stateCity& state)
{
return ostr << state.stateName << " " << state.cityName;
}
bool operator== (const stateCity& a, const stateCity& b)
{
return a.stateName = b.stateName;
}
bool operator< (const stateCity& a, const stateCity& b)
{
return a.stateName < b.stateName;
}
int main() {
stateCity values[] = {stateCity("Arizona", "Phoenix"), stateCity("Illinois", "Chicago"), stateCity("California", "Sacramento")};
set<stateCity> s(values, values + 3);
string state;
cout << "Enter a state: ";
getline(cin, state);
for (set<stateCity>::iterator iter; iter != s.end(); iter++)
{
if (*iter == state)
{
cout << *iter << endl;
}
else
{
cout << "The state does not exist in the set" << endl;
}
}
}
|
// objects stores the state names and city in the state
class stateCity
{
public:
stateCity (const string& name = " ", const string& city = " ");
friend ostream& operator<< (ostream& ostr, const stateCity& state);
// output the state and city name in the format
friend bool operator< (const stateCity& a, const stateCity& b);
friend bool operator== (const stateCity& a, const stateCity& b);
// operators < and == must be defined to use with set object,
// operators use only the stateName as the key
private:
string stateName, cityName;
};
Write a program that declares a set object s having elements of type stateCity with the following as its initial values:
("Arizona" "phoenix") ("Illinois" "Chicago") ("California" "Sacramento")
Input the name of a state, and use the find() function to determine whether the state is in the set. If the object is present, use the << operator to output the state and city; if it is not present, output a message to the effect.
This is my original question
Thanks in advance!