【问题标题】:Pointer Function return value of Struct in Class [duplicate]类中结构的指针函数返回值
【发布时间】:2015-09-15 16:54:38
【问题描述】:

我一直在尝试创建一个函数getLocation(),它利用指针返回Character 类中声明的结构Location 的值。我很好奇我的语法(或我的结构)的问题。知道星号* 应该引用该值,为什么我使用与符号string& Character::getInventory 的函数能够返回该特定索引的值(其返回不需要转换)?

正在尝试Location& Character::getLocation() {return position; } 当运行结果为error C2679: binary '<<': no operator found

也不是 Location* 由于没有转换,因此无法运行。

我读到以下可能是最合适的,因为它指定了结构所在的范围,但仍然导致需要并返回一个临时的。

Character::Location* const & Character::getLocation() {return &position; }

任何建议或意见将不胜感激,在此先感谢。 下面是我的main.cpp,当然会显示Location的十六进制地址。

#include <iostream>
#include <string>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;

class Character {

private:

    string name;
    string inventory[4];

public:

    struct Location {
        int x; int y;
    };
    Location position;

public:

    void Character::setName(string x) { name = x; }
    string Character::getName() { return name; }

    void Character::setLocation(int x, int y) {
        position.x = x; position.y = y;
    }

    Location* Character::getLocation() {return &position; }

    void Character::setInventory(string(&x)[4]) { for (int i = 0; i < 4; ++i) { inventory[i] = x[i]; } }
    string& Character::getInventory(int itemNumber) { return inventory[itemNumber]; }
};

void showUser(Character Character);

int main() {

        try {

            string items[4] = { "Sword", "Shield", "Potion", "Cloak" };

            Character CharacterI;

            CharacterI.setName("Some Character");
            CharacterI.setInventory(items);
            CharacterI.setLocation(1, 30);

            cout << "\n" << "Retrieving Character Info..." << "\n" << endl;
            showUser(CharacterI);

        }
        catch (std::exception & e) {
            cerr << "\nError : " << e.what() << '\n';
        }

    system("pause");
    return 0;
}

void showUser(Character character) {
    cout << "Name : " << character.getName() << endl;
    cout << "Location : " << character.getLocation() << endl;
    for (int i = 0; i < 4; ++i) {
        cout << "Inventory " << i + 1 << " : " << character.getInventory(i) << endl;
    }
}

【问题讨论】:

  • 将其移至评论而不是答案,因为它并没有真正回答您的问题:您的函数 getLocationsetLocation 创建 struct Location 的临时实例,每个实例在函数返回。您需要创建一个成员变量来保存角色的位置,然后从该变量中设置/获取。
  • 你声称想做一些非常简单的事情,然后你贴出一堵代码墙。请发布一个小型、独立的代码示例,说明您要解决的问题。
  • @Carlton 想要回答这个问题吗?这是正确的。其实我看到这是一个答案,你删除了它。为什么?
  • 我在写完之后重新阅读了 OP,并决定虽然我的答案可能是正确的,但它似乎并没有真正回答这个问题,这似乎是关于功能被转换。如果您认为合适,我会重新发布作为答案。
  • 我第二个@juanchopanza。尝试一个包含 int 的普通类。具体理解,虽然是的,*表达式 中表示“值”(即取消引用地址),但在 表达式中表示相反的意思i>声明。这是因为 C's/C++ 的声明语法模仿表达式并显示 您需要对声明的标识符执行什么操作 以获取基本类型,此处为 Location。如果您需要取消引用标识符,则标识符表示一个指针(类似的东西)。

标签: c++ function class pointers struct


【解决方案1】:

好的,我想我现在更好地理解了这个问题。 getInventory 可以成功返回引用而getLocation 不能成功返回的原因是因为getLocation 返回了对临时变量的引用,这是不好的。有关详细信息,请参阅@NathanOliver 评论中的链接。此外,套用@Peter Schneider 先前的评论,表达式中的* 取消引用指针以返回值,而在声明中它表示变量将是指针类型。这两种用法或多或少是相反的。示例:

int* p = new int;  //Declares a pointer to int
int x = *p;        //Dereferences a pointer and returns an int

您需要做的是创建一个成员变量来保存角色的位置,然后从该变量中设置/获取,而不是创建临时变量。您已经为 nameinventory 这样做了,继续使用相同的模式。

此外,每当您在Character 类范围之外使用Location 结构时,您需要使用Character::Location 对其进行完全限定。

例子:

#include <iostream>
using namespace std;

class Character {
public:
    struct Location {
        int x;
        int y;
    };

    Location loc;

    void SetLocation(int x, int y) {loc.x = x; loc.y = y;}
    Location& GetLocation() {return loc;}
};


int main ()
{
    Character c;
    c.SetLocation(1,42);
    Character::Location l = c.GetLocation();

    cout << l.x << endl << l.y << endl;

    return 0;
}

输出:

1

42

【讨论】:

  • 嘿,谢谢;我的问题有点令人困惑。这缓解了我的大部分问题。我想给你荣誉,但为了后代,我认为@Peter Schneider 上面的评论“* 表示表达式中的“值”(即取消引用地址),它表示声明中的相反“是问题的另一半。如果你包括在内,我很乐意接受。
  • @Austin:我更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2016-04-21
  • 1970-01-01
  • 2022-06-15
相关资源
最近更新 更多