首页 > 解决方案 > 如何返回具有 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;


}

标签: c++visual-c++

解决方案


pick()函数中,您创建另一个Location对象(temp),并将值分配给该对象。您应该做的是将 x 和 y 值分配给调用该pick()函数的对象,而不是分配给Locationpick().

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/


推荐阅读