c++ - 如何返回具有 void 成员函数的类的实例?
问题描述
因此,我正在为涉及课程以及如何访问私人公共功能的学校分配作业。据我所知,我应该能够设置传递给函数的类实例的值。但是,当我运行程序时,它会将变量的值设置为函数指定的值之外的值。我不允许操纵头文件来更改函数的返回类型的值。我可以提示我做错了什么吗?
这是类 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;
}
解决方案
在pick()
函数中,您创建另一个Location
对象(temp),并将值分配给该对象。您应该做的是将 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/
推荐阅读
- xslt - 如何从重复的父标签中选择子标签的最后一个值(如果存在)?
- python - 如何获取默认麦克风的全名?
- javascript - ajax 在 Laravel 中不起作用,显示找不到页面
- xml - Groovy 在 SoapUI 响应中从 XML 获取节点值
- java - Java Array 中的 add 和 element 的时间复杂度是多少?
- javascript - 如何解决 Jest Test Code mockRestore 不起作用的问题
- c# - 使用 asp.net c# 代码增加 WebBrowser 的高度然后出现错误:参数无效
- react-native - 为什么 expo sdk 34 的应用程序大小比之前的 sdks 大?
- javascript - 同一个promise实例可以有多个thenable吗?
- javascript - 尝试整合 dialogflow 和 firebase