Overloading operators

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!
Last edited on
Please reply for me :(
you're incorrectly assigning.
You need to use '==' on line 30.

also:
Please reply for me :(

Don't moan if you haven't had a reply after 4 minutes. You won't be very popular.
Last edited on
closed account (48T7M4Gy)
Just roughly it sounds like you need to write an operator= method because your program doesn't know how to carry out the assignment operation.
http://en.cppreference.com/w/cpp/language/operators
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp7_OperatorOverloading.html see 9.1
Last edited on
Hi,

Line 30 uses the assignment operator, does that give a clue as to why it isn't working? You need to compare each member of the class for equality, and return a result. Doing this for only some of them is misleading, one would naturally expect the whole object to be compared. In other words it's bad form to do unexpected things with operators.

Hope all goes well :+)
closed account (48T7M4Gy)
LOL - Sounds like it might pay to moan - wouldn't be unusual in an undergrad environment. All done in about 15 minutes. Must be a record.
Last edited on
I corrected line 30 to be '==' but I got

1
2
3
4
5
Undefined symbols for architecture x86_64:
  "stateCity::stateCity(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      _main in 27a1-20e56b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


this error....

I've posted many questions but I got almost no answer. I need to be more tolerance :)


I didn't see all those other replies nor the edit to the OP when I made my post earlier.
closed account (48T7M4Gy)
line 30: you've made a blooper by writing =

== maybe?? :]
Last edited on
Hi,

You didn't define your constructor :+)

/tmp/cct4VKg0.o: In function `main': :(.text.startup+0x4c): undefined reference to `stateCity::stateCity(std::string const&, std::string const&)' :(.text.startup+0xb3): undefined reference to `stateCity::stateCity(std::string const&, std::string const&)' :(.text.startup+0x11e): undefined reference to `stateCity::stateCity(std::string const&, std::string const&)' :(.text.startup+0x26f): undefined reference to `stateCity::stateCity(std::string const&, std::string const&)' collect2: error: ld returned 1 exit status
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
#include <iostream>
#include <string>

using namespace std;

class stateCity
{
	public: 
		stateCity (const string& name = "", const string& city = ""): 
			stateName(name), cityName(city) 
		{} 
 
		friend ostream& operator<< (ostream& ostr, const stateCity& state) 
		{ 
			ostr << state.cityName << ", " << state.stateName; 
 
			return ostr; 
		} 
 
		friend bool operator< (const stateCity& a, const stateCity& b) 
		{ 
			return a.stateName < b.stateName; 
		} 
 
		friend bool operator== (const stateCity& a, const stateCity& b) 
		{ 
			return a.stateName == b.stateName; 
		} 
 
	private:
		string stateName, cityName;
};


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;
		}
	}
}


I corrected it, and I could run it but it occurs segmentation fault11

1
2
Enter a state: California
Segmentation fault: 11
line 44:

for (set<stateCity>::iterator iter = s.begin(); iter != s.end(); iter++) // Note: initialize iter
Are you messing with us here?

Why all of a sudden a lack of includes?

It's late my end ZZZZZZzzzzzzz............
closed account (48T7M4Gy)
you have to #include <set>
Last edited on
Thanks guys I figured out it works. Good night TheIdeasMan, I don't know how to tag you. It's been 29 hours since I left my bed. I need to go bed I appreciate it.
closed account (48T7M4Gy)
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 <string>
#include <set>

using namespace std;

class stateCity
{
public:
	string stateName, cityName;

	stateCity(const string& name = "", const string& city = "") :
		stateName(name), cityName(city)
	{}

	friend ostream& operator<< (ostream& ostr, const stateCity& state)
	{
		ostr << state.cityName << ", " << state.stateName;

		return ostr;
	}

	friend bool operator< (const stateCity& a, const stateCity& b)
	{
		return a.stateName < b.stateName;
	}

	friend bool operator== (const stateCity& a, const stateCity& b)
	{
		return a.stateName == b.stateName;
	}

private:

};


int main() 
{
	string state;
	stateCity values[] = { stateCity("Arizona", "Phoenix"), stateCity("Illinois", "Chicago"), stateCity("California", "Sacramento") };
	std::set<stateCity> s(values, values + 3);
	
	cout << "Enter a state: ";
	getline(cin, state);

	stateCity temp;
	
	for (set<stateCity>::iterator iter = s.begin(); iter != s.end(); iter++)
	{
		temp = *iter;
		if (temp.stateName == state)
			cout << *iter << endl;
		else
			cout << "The state does not exist in the set" << endl;
	}

}


This runs but needs a tidy up with the search and getters to overcome public attributes.
Topic archived. No new replies allowed.