在头文件中 const int NUM_LETTERS = 10; char letters [10] = {'A','B','C','D','E','F','G','H','I','J'};
在Main_Idea.cpp NL.Letters(NL.letters,NL.NUM_LETTERS)中;
我正在尝试使用game.displayResults(.....);
我有2个头文件,一个工作,另一个不起作用。第一个头文件正确运行,我相信它,因为它们在原型函数中没有数据。当我运行第二个Header文件时,它在void displayResults(int gCount,int wCount1,int wCount2)中有数据,以及如何放入game.displayResults(.....);在Main_Idea.cpp中谢谢!
第一个我将只包含部分代码:这是在头文件中。
void EnterB();
double residential();
double business();
这是在cpp
中 Billing B;
Billing* PtrB = &B;
B.EnterB();
PtrB->EnterB();
有效。下一个没有。
这是头文件
#ifndef RPS_H_h
#define RPS_H_h
enum objectType{Rock, Paper, Scissors}; //Put in class?
class RPS_H
{
private:
//enum objectType{Rock, Paper, Scissors};
int gameCount = 0;
int winCount1 = 0;
int winCount2 = 0;
int gameWinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
public:
RPS_H();
void Enter();
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2); //, int wCount1, int wCount2
~RPS_H();
};
#endif // !RPS_H_h
RPS_H.cpp
#include "RPS_H.h"
#include <iostream>
using namespace std;
RPS_H::RPS_H()
{
}
void RPS_H::Enter()
{
displayRules();
cout << "Enter either a Y or y to play the game: ";
cin >> response;
cout << endl;
while (response == 'Y' || 'y')
{
cout << "Player 1 enter your choice: ";
cin >> selection1;
cout << endl;
cout << "Player 2 enter your choice: ";
cin >> selection2;
cout << endl;
if (validSelection(selection1) && validSelection(selection2))
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResult(play1, play2, gameWinner);
if (gameWinner == 1)
winCount1++;
else
if (gameWinner == 2)
winCount2++;
}
cout << "Enter Y/y to play the game again: ";
cin >> response;
cout << endl;
}
displayResults(gameCount, winCount1, winCount2);
}
void RPS_H::displayRules()
{
cout << "Welcome to ...";
}
bool RPS_H::validSelection(char selection) // change
{
switch (selection)
{
case 'R': case 'r':
case 'P': case 'p':
case 'S': case 's': return true;
default:
return false;
}
}
objectType RPS_H::retrievePlay(char selection)
{
objectType object;
switch (selection)
{
case 'R': case 'r': object = Rock;
case 'P': case 'p': object = Paper;
case 'S': case 's': object = Scissors;
}
return object;
}
void RPS_H::convertEnum(objectType object)
{
switch (object)
{
case Rock:
break;
case Paper:
break;
case Scissors:
break;
}
}
objectType RPS_H::winningObject(objectType play1, objectType play2)
{
if ((play1 == Rock && play2 == Scissors)
|| (play2 == Rock && play1 == Scissors))
return Rock;
else
if((play1 == Rock && play2 == Paper)
|| (play2 == Rock && play1 == Paper))
return Paper;
else
return Scissors;
}
void RPS_H::gameResult(objectType play1, objectType play2, int& winner)
{
objectType winnerObject;
if (play1 == play2)
{
winner = 0;
cout << "Both players selected ";
convertEnum(play1);
cout << ". This game is a tie." << endl;
}
else
{
winnerObject = winningObject(play1, play2);
cout << "Player 1 selected ";
convertEnum(play1);
cout << "Player 2 selected ";
convertEnum(play2);
cout << ". ";
if (play1 == winnerObject)
winner = 1;
else
if (play2 == winnerObject)
winner = 2;
cout << "Player " << winner << " wins this game." << endl;
}
}
void RPS_H::displayResults(int gCount, int wCount1, int wCount2) //, int wCount1, int wCount2
{
cout << "The total number of players: " << gCount << endl;
cout << "The number number of player 1: " << wCount1 << endl;
cout << "The number number of player 2: " << wCount2 << endl;
}
RPS_H::~RPS_H()
{
}
Main_Idea.cpp
#include <iostream>
#include <iomanip>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "RPS_H.h"
#include "Billing.h"
using namespace std;
int main()
{
RPS_H game;
RPS_H* PtrG = &game;
cout << "Hello World\n";
game.displayResults(game.displayResults.gameWinner, game.displayResults.winCount1, game.displayResults.winCount2);
//PtrG->gameResult(&PtrG->gameResult->gameWinner,&PtrG->gameResult->winCount1,&PtrG->gameResult->winCount2);
Billing B;
Billing* PtrB = &B;
B.EnterB();
cout << "\nAlso\n";
PtrB->EnterB();
cout << "\nAnother function maybe\n\n";
B.EnterS();
//B.EnterS(B.EnterS.square);
//B.square(y);
cout << "And this way\n";
PtrB->EnterS();
cin.get();
cin.get();
cin.get();
return 0;
}