C ++无法从类中检索私有变量[关闭]
Posted
技术标签:
【中文标题】C ++无法从类中检索私有变量[关闭]【英文标题】:C++ Can't retrieve private variable from class [closed] 【发布时间】:2020-05-27 17:13:47 【问题描述】:我正在编写一个程序来生成整数并设置用户选择的范围。
例如:
Enter the number of integers: 4
Range: 10
4 9 2 1 are generated
现在用户一次选择 4 个数字,直到它们正确为止。
程序还会告诉用户他们是否部分正确。
例如:
User input: 4 9 0 7
Console << 2 of your answers are correct.
我有三个文件: 驱动程序.cpp
#include <iostream>
#include "Game.h"
using namespace std;
int main()
// Declare variables.
Guess guess;
int numberOfIntegers;
int rangeOfIntegers;
int count = guess.getSum();
//Prompt user input.
while(count != numberOfIntegers)
cout << "Enter the Number of Integers (n): " << endl;
cin >> numberOfIntegers;
cout << "Number of Each Integers from 1 to (m): " << endl;
cin >> rangeOfIntegers;
cout << "Enter your guesses for the " << numberOfIntegers << " integers in the range from 1 to " << rangeOfIntegers << " that have been selected:" << endl;
guess.beginGuessingGame(rangeOfIntegers, numberOfIntegers);
if (count == numberOfIntegers)
cout << "You are correct! Play again? (y/n)";
else
cout << count << " of your guesses are correct." << endl;
;
游戏.h
// identifiers
#ifndef guessing_game
#define guessing_game
class Guess
private :
int * generatedSequence;
int * inputGuess;
int sum;
public :
void generateSequence(int inputRangeOfIntegers, int inputNumberOfIntegers);
void beginGuessingGame(int inputRangeOfIntegers, int inputNumberOfIntegers);
int getSum()
return sum;
;
#endif
和 Game.cpp
#include <iostream>
#include <iomanip>
#include "Game.h"
using namespace std;
void Guess::generateSequence(int inputRangeOfIntegers, int inputNumberOfIntegers)
/// Initialize random number generator.
srand(time(0));
/// Declare array size for the generated sequence to be based on user input.
generatedSequence = new int[inputRangeOfIntegers];
/// Input randomly generated numbers from from 0 to input range into generatedSequence.
for (int i = 0; i < inputNumberOfIntegers; i++)
generatedSequence[i] = rand() % inputRangeOfIntegers + 1;
cout << generatedSequence[i] << " " << endl;
void Guess::beginGuessingGame(int inputRangeOfIntegers, int inputNumberOfIntegers)
/// Call our generateSequence function.
generateSequence(inputRangeOfIntegers, inputNumberOfIntegers);
/// Declare guess size based on user input.
inputGuess = new int[inputNumberOfIntegers];
/// Begin endless loop for user to guess integers.
for (;;)
for (int i = 0; i < inputNumberOfIntegers; i++)
cin >> inputGuess[i];
/// If the user has found the random sequence, we can make sum equal to the number of integers.
sum = 0;
for (int i = 0; i < inputNumberOfIntegers; i++)
for (int j = 0; j < inputNumberOfIntegers; j++)
/// If the user has entered the right guess, we can tally sum to the number of integers entered.
if (generatedSequence[i] == inputGuess[j])
sum++;
break;
我的问题是:我无法在主类中检索 sum 变量来检查它与整数的数量。因为如果它们相等,那么程序就知道用户猜对了。调用 beginGuessingGame 函数后我也无法使用 cout..
有什么建议吗?
谢谢。
【问题讨论】:
你为什么要制作一些你想从课堂之外访问的东西private
?
@ScottHunter 作业指出我们必须使用“数据隐藏”。这会是个例外吗?
为什么不能使用getSum
?
【参考方案1】:
至少这部分程序
Guess guess;
int numberOfIntegers;
int rangeOfIntegers;
int count = guess.getSum();
//Prompt user input.
while(count != numberOfIntegers) //...
没有意义。该程序具有未定义的行为。
类对象guess
的数据成员未初始化所以成员函数getSum
返回对象的数据成员sum
的不确定值。并且这个不确定值在while循环中与未初始化变量numberOfIntegers
的另一个不确定值进行比较。
在函数generateSequence
中,此语句似乎有错字
generatedSequence = new int[inputRangeOfIntegers];
应该有
generatedSequence = new int[inputNumberOfIntegers];
在函数beginGuessingGame
中有一个无限循环
for (;;)
for (int i = 0; i < inputNumberOfIntegers; i++)
cin >> inputGuess[i];
/// If the user has found the random sequence, we can make sum equal to the number of integers.
sum = 0;
for (int i = 0; i < inputNumberOfIntegers; i++)
for (int j = 0; j < inputNumberOfIntegers; j++)
/// If the user has entered the right guess, we can tally sum to the number of integers entered.
if (generatedSequence[i] == inputGuess[j])
sum++;
break;
【讨论】:
改成while(true),结果一样。 @xigedi20 请参阅我的附加帖子。而且您在 main 中的代码没有意义。例如,变量计数未初始化。因此,请选择最佳答案关闭此问题并重新编写程序,同时考虑到我在答案中写的内容。以上是关于C ++无法从类中检索私有变量[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
子类可以继承父类的一切方法,成员变量,甚至是私有的,但是却不能够访问这些私有的成员变量和方法