Reputation: 11
I overloaded the '++' operator, I define it in my head and .cpp file. I use the operator in my main file. It says that it is not defined so I am confused why it's not seen by the compiler. I made sure to include a copy constructor as I had that error in a previous project.
player.h
#pragma once
class Player {
private:
int hitsGotten;
int winHits;
int score;
public:
//Constructor
Player(int hits);
Player();
Player(const Player& play);
int gethits() { return hitsGotten; };
int getWinHits() { return winHits; }
int getScore() { return score; };
void setScore(int num) { score = num; };
virtual void gothit() { hitsGotten++; };
virtual void WonGame() { score++; };
void operator++();
};
player.cpp
#include "player.h"
Player::Player(int hits) {
winHits = hits;
hitsGotten = 0;
score = 0;
}
Player::Player(){
winHits = 0;
hitsGotten = 0;
score = 0;
}
Player::Player(const Player& play){
hitsGotten = play.hitsGotten;
winHits = play.winHits;
score = play.score;
}
void Player::operator++(){
this->score = this->score + 1;
}
Main program
#include "board.h"
#include "game.h"
#include "player.h"
#include "humanPlayer.h"
#include <vector>
#include <iostream>
using namespace std;
vector<Game> list;
int main() {
int diff;
int BX = 0;
int BY = 0;
int totalHits = 0;
if (diff == 1) {
cout << "You have selected Easy" << endl;
BX = 5;
BY = 5;
totalHits = 8;
}
else if (diff == 2) {
cout << "You have selected Standard" << endl;
BX = 8;
BY = 8;
totalHits = 17;
}
else if (diff == 3) {
cout << "You have selected Hard" << endl;
BX = 10;
BY = 10;
totalHits = 21;
}
else {
cout << "The difficulty was not selected correctly"
<< "Please take out the game and blow the dust off" << endl;
}
Game G(BX, BY);
Board B(BX, BY);
humanPlayer User(totalHits);
Player Computer(totalHits);
G.startGame();
//G.LossScreen();
//G.WinScreen();
//G.anotherGame();;
if (diff == 1) {
G.gameSetUpE(B, User);
}
else if (diff == 2) {
G.gameSetUpS(B, User);
}
else if (diff == 3) {
G.gameSetUpH(B, User);
}
else {
cout << "Something went wrong with the game set up"
<< "Please take out the game and blow the dust off" << endl;
}
cout << "Time to start the game" << endl;
int gameRes = G.playGame(B, User, Computer);
G.setWinner(gameRes);
list.push_back(G);
if (gameRes == 1) {
G.WinScreen();
User++;
if (G.anotherGame() == 'Y') {
main();
}
else {
G.endGame(list);
G.ByeScreen();
}
}
if (gameRes == 2) {
G.LossScreen();
Computer++;
if (G.anotherGame() == 'Y') {
main();
}
else {
G.endGame(list);
G.ByeScreen();
}
}
return 0;
}
Upvotes: 1
Views: 106
Reputation: 9672
You have defined the pre-increment ++ operator (e.g. ++i) rather than the post increment (e.g. i++). So you probably want to do ++Computer instead, but if you really want post increment (you don't), then you can do:
Player& operator++() //< pre-increment
{
score++;
return *this;
}
Player operator++(int) //< post-increment (returns a copy)
{
Player p(*this); // take copy of old value
score++;
return p;
}
Upvotes: 3
Reputation: 96800
operator++()
declares the prefix increment operator. Allowing it to be used like ++x
. x++
however is a different function, one that you haven't declared/defined in your class. To define a postfix increment operator you declare a new function with a parameter int
:
void operator++(int);
Add int
to your declaration and definition and your code should compile (if that's the only problem, that is).
Upvotes: 2