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?