Jump to content

no match for 'operator++' in '++north'

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
I've been trying so much to fix this one problem, yet still cannot. I had never saw anything like it =/

#include <iostream>

using namespace std;

class Navigation {
    int north, west;
public:
	void set_values(int,int);
} coords;

void Navigation::set_values (int a, int b) {
	int north = a;
	int west  = b;
}

int main()
{
    Navigation coords;
    Navigation north;
    Navigation west;
	coords.set_values (0,0);
	
	char direction;

	cout << "Would you like to go N, E, S, W?";
	cin >> direction;
	
	switch (direction) {
		case 'N':
			++north;
			break;
		case 'E':
			--west;
			break;
		case 'S':
			--north;
			break;
		case 'W':
			++west;
			break;
		default:
            cout << "N, E, S, W only!";
            break;
	}
	cout << "Your co-ordinates are now: " << north << west << endl;
	

	return 0;
}


/cry ;[


"no match for operator++ in ++north
Posted Image

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
That's because there's no operator ++ that has been overloaded for use in the Navigation class type. What are you trying to do by saying ++north? You haven't told the compiler what that's supposed to mean with the Navigation class.
Wow I changed my sig!

#3
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts

ZekeDragon said:

That's because there's no operator ++ that has been overloaded for use in the Navigation class type. What are you trying to do by saying ++north? You haven't told the compiler what that's supposed to mean with the Navigation class.

Woah, quick reply :P

++north is meant to increase the value of north
Posted Image

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Are you trying to increment the value of "north" in the coords Navigation object, or in the separate north Navigation object. Remember that you've got three Navigation objects, called coords, north, and west, and each of those objects have private properties called north and west, and only one method, set_values(), so there's no real way to even get the value. I'd suggest you have a get_west and get_north set, and do this:
class Navigation {
    int north, west;
public:
	void set_values(int,int);
	int get_west() { return west; }
	int get_north() { return north; }
}; // You don't want a global 'coords' object here if you made a local one in main()

void Navigation::set_values (int a, int b) {
	north = a; // Don't say "int", that will declare a local copy.
	west  = b; // This uses the class copy of north and west instead.
}

int main()
{
    Navigation coords;
	coords.set_values (0,0);
	char direction;

	cout << "Would you like to go N, E, S, W?";
	cin >> direction;
	
	switch (direction) {
		case 'N':
			coords.set_values(coords.get_north() + 1, coords.get_west());
			break;
		case 'E':
			coords.set_values(coords.get_north(), coords.get_west() - 1);
			break;
		case 'S':
			coords.set_values(coords.get_north() - 1, coords.get_west());
			break;
		case 'W':
			coords.set_values(coords.get_north(), coords.get_west() + 1);
			break;
		default:
            cout << "N, E, S, W only!";
            break;
	}
	cout << "Your co-ordinates are now: " << coords.get_north() << coords.get_west() << endl;

	return 0;
}

Wow I changed my sig!

#5
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Thankyou very much, I dunno why I couldn't get it, but thankyou for allowing me to understand:)
Posted Image