const parameter access problem

I've got an exercise to design and implement a class which holds name and age vectors. Further exercises ask me to implement a global == operator overload which will compare two objects. In order to implement that, I have to loop through the lists within the objects and compare element by element.

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
// Name_pairs.h
// Name_pairs class declarations

#include <iostream>
#include <string>
#include <vector>

class Name_pairs{
	public:
		void read_names();
		void read_ages();
		void sort();
		std::string getName( int n );
		double getAge( int n );
		int getSize();
		void print();
	private:
		std::vector<std::string> name;
		std::vector<double> age;
		int Size = 0;
};

bool operator==( const Name_pairs& a, const Name_pairs& b );
bool operator!=( const Name_pairs& a, const Name_pairs& b );

std::ostream& operator<<( std::ostream& os, const Name_pairs& p );



Here's the relevant parts of the implementation:
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
// Name_pairs.cpp
// Name_pairs class implementation
#include "Name_pairs.h"
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::string;


int Name_pairs::getSize()
{
	return Size;
}


bool operator==( const Name_pairs& a, const Name_pairs& b)
{
	bool equal = true;

	if( a.getSize() != b.getSize() )
		return false;

	for( int i = 0; i < a.getSize(); i++ )
	return equal;
}


the == operator is obviously incomplete.
The problem I'm having is on the if( a.getSize() != b.getSize() ) line. It doesn't like the use of getSize() with the const references. If I remove the 'const' from the parameters it works, but I'm wondering why it won't work with const parameters.
Last edited on
The function isnt const.

1
2
int Name_pairs::getSize() const
{ return Size  }


const objects can only call const functions.

non const objects can call both non-const and const functions.
Thanks, that helped remove my head from its rectal entrapment :)
Topic archived. No new replies allowed.