因此,我正在为涉及班级的学校分配作业,以及如何使用私人公共职能。据我所知,我应该能够设置传递给函数的类实例的值。但是,当我运行程序时,它将变量的值设置为函数指定的值之外的值。我不允许操纵头文件来更改函数的返回类型的值。我可以提示我做错了什么吗?
这是Location类中函数的定义。如您所见,它没有参数,因此我无法放入return temp;
,因此程序知道要保存它。
void Location::pick() {
Location temp;
srand(time(nullptr));
int tmp = rand() % fieldSize + 1;
int tmp = rand() % fieldSize + 1;
switch (tmp) {
case 1:
temp.y = 'a';
break;
case 2:
temp.y = 'b';
break;
case 3:
temp.y = 'c';
break;
case 4:
temp.y = 'd';
break;
case 5:
temp.y = 'e';
break;
case 6:
temp.y = 'f';
break;
}
}
这是该类的原型(如果这是正确的术语)
class Location {
public:
Location(); // void constructor, assigns -1 to X coord, and * to Y coord
void pick(); // picks a random location
void fire(); // asks the user to input coordinates of the next shot
void print() const; // prints location in format "a1"
// predicate returns true if the two locations match
friend bool compare(const Location&, const Location&);
private:
static const int fieldSize = 6; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
这是我试图正确运行的测试代码
int main() {
// srand(time(nullptr)); // random seed
srand(1); // fixed seed
//
// checking location object
//
Location mySpot, userShot;
mySpot.pick(); // selecting a new random location
cout << "Randomly selected location is: "; mySpot.print();
cout << "Input location: ";
userShot.fire(); // having user input a location
if (compare(mySpot, userShot))
cout << "Random location matches user input.\n";
else
cout << "Random location does not match user input.\n";
}
运行测试时,它将使初始化变量的值生效 “随机选择的位置是:-858993460╠” 我不明白我在做什么错。有人可以给我提示或解释吗?我的语法错误吗?逻辑错了吗?
如果您认为问题仍然存在,这里是我到目前为止已完成的所有功能
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "battleship.h"
using std::cin; using std::cout; using std::endl;
Location::Location() {
int x = -1;
char y = '*';
}
void Location::pick() {
Location temp;
srand(time(nullptr));
int tmp = rand() % fieldSize + 1;
switch (tmp) {
case 1:
temp.y = 'a';
break;
case 2:
temp.y = 'b';
break;
case 3:
temp.y = 'c';
break;
case 4:
temp.y = 'd';
break;
case 5:
temp.y = 'e';
break;
case 6:
temp.y = 'f';
break;
}
}
void Location::fire() {
Location loc;
cout << "Input the location you wish to fire at";
cin >> loc.x;
cin >> y;
}
void Location::print()const {
Location loc;
int x = loc.x;
char y = loc.y;
cout << x << y;
}
bool compare(const Location& a, const Location& b) {
if (a.x == b.x && a.y == b.y){
return true;
}
else return false;
}
答案 0 :(得分:0)
在pick()
函数中,创建另一个Location
对象(临时),并为该对象分配值。您应该做的是将x和y值分配给调用pick()
函数的对象,而不是分配给Location
中本地定义的另一个pick()
对象。
void Location::pick() {
srand(time(nullptr));
int tmp = rand() % fieldSize + 1;
switch (tmp) {
case 1:
this->y = 'a';
break;
case 2:
this->y = 'b';
break;
case 3:
this->y = 'c';
break;
case 4:
this->y = 'd';
break;
case 5:
this->y = 'e';
break;
case 6:
this->y = 'f';
break;
}
}
您在print()
函数中遇到相同的问题。您可以将其更改为:
void Location::print()const {
cout << this->x << " " << this->y;
}
this
指针指向调用该方法的当前对象。对于您来说,pick()
方法已由主函数中创建的mySpot
对象调用。
有关this
指针的更多信息:https://www.geeksforgeeks.org/this-pointer-in-c/