Hi, beginner here making the move from Java to C++ and having troubles (of course) with const and pass-by-reference. I'll show you the code I'm working on:

Card.h:

#include <iostream>

using namespace std;

class Card 
{
	friend ostream& operator<<(ostream& out, const Card& c);

	public:
		Card(char aSuit, int aPower);
		void setSuit(char newSuit);
		void setPower(int newPower);
		const char getSuit();

	private:
		char suit;
		int power;
};

Card.cpp:

#include <iostream>
#include "Card.h"

using namespace std;

Card::Card(char aSuit, int aPower) 
{
	suit = aSuit;
	power = aPower;
}

void Card::setPower(int newPower)
{
	power = newPower;
}

void Card::setSuit(char newSuit)
{
	suit = newSuit;
}

const char Card::getSuit()
{
	return suit;
}

ostream& operator<<(ostream& out, const Card& c)
{
	out << c.getSuit();
	return out;
}

The implementation of the << operator overloading in the .cpp file seems to be an issue. This is my error message:

"card.cpp(29) : error C2662: 'Card::getSuit' : cannot convert 'this' pointer from 'const Card' to 'Card &' Conversion loses qualifiers"

I'm stumped as to what the problem is, since I declared getSuit() to be const and it should be usable in the operator overloading section of the .cpp file. Am I trying to call the function wrong?

Thank you for using Code-tags!

If your not changing the any of the values in the Class (card) c, why would you want to pass by reference?
To make it easier for yourself, how about:

ostream& operator<<(ostream& out, Card c)
{
	out << c.getSuit();
	return out;
}
[.....]
int main()
{
	Card d('a',1);
	cout << d;
[...]

Output would be 'a' if I'm not mistaken (I'm on the wrong PC, don't have a compiler).
Of course you would have to change the declaration as well

Thanks for the nice reply; I was just trying out the whole "pass by const reference" that programmers use when they don't want to have the computer copy the object file every time they call the function. Supposed to be quite a bit faster (especially if you have a large object) if you simply pass a read only object reference instead, and since the method I was going to call was defined as const and didn't change any data members, I figured that it should compile no problem.

Ah well, your fix does work fine though. My main goal in this puttering about is to try and get a good grip of the differences between C++ and Java (pointers, references, const, overloading operators, etc etc)

Supposed to be quite a bit faster (especially if you have a large object).

True. But with a program this small and on a (I guess) fast PC, this isn't really an issue yet.

My main goal in this puttering about is to try and get a good grip of the differences between C++ and Java (pointers, references, const, overloading operators, etc etc)

For a beginner you're quite advanced already, so keep up the good work

Pass by reference works much the same as passing an array in Java. In fact, when you supply an array as an argument in Java, you don't actually copy the array, you just pass the address (believe it or not). It's just you don't explicitly manage the pointer aspect in Java; Java does all that for you behind the scenes (an array is just a pointer to a block of memory).

Having started with C, I don't quite like/use the C++ pass-by-reference style...I much prefer passing an address and dereferencing (I also tend to do a lot of things in an "archaic", C-fashion...haha).

Either way, I found a few articles that you might find helpful/interesting:

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references
http://www.horstmann.com/ccj2/ccjapp3.html
http://www-ali.cs.umass.edu/~mckinley/377/labs/c++_for_java_programmers.html

If you want to stick with the original ostream& operator<<(ostream& out, const Card& c) then you need to make the getSuit() method also const ..

const char getSuit() const;
// and ...
const char Card::getSuit() const

One more link for you ..
http://en.wikipedia.org/wiki/Const_correctness#Methods

Indeed, but the problem is, I actually did add const to both methods (header and cpp), but the compiler still complains. Its quite frustrating.

Anyway, I appreciate the links, going to read through them right now.

Actually, scratch that. I didn't realize I had to add the 'const' keyword both before and after the method declaration. Compiles just fine now. Thanks :D

I didn't realize I had to add the 'const' keyword both before and after the method declaration.

The 'const' keyword that is after the method is the one that counts in this case. If you use 'const' keyword in front of the method, then that 'const' applies to the method's return value.

If you use 'const' keyword in front of the method, then that 'const' applies to the method's return value.

Ah, thanks for that bit, good to know.

This question has already been answered. Start a new discussion instead.