【问题标题】:Accessing a string gives empty value even though it is assigned即使分配了字符串,访问字符串也会给出空值
【发布时间】:2015-11-03 23:27:46
【问题描述】:

对于一个项目,我正在用 C++ 制作一个简单的基于文本的战斗游戏,但我并不十分熟悉。

我在将玩家姓名返回到游戏控制器时遇到问题。 使用 Visual Studio 的监视功能,我可以看到在构造时正在设置名称,但是当我尝试在“getName”调用中访问它时,它是空的。这可能与指针有关,但我不确定。

代码和图片如下。

Game.cpp

#include "Game.h"

Game::Game() 
{
    Player user = Player("Foo");
    gameLoop();
}

void Game::gameLoop() 
{
    std::string name = user.getName();
    printf("name: %s", name.c_str());
}

Game.h

#include <stdio.h>
#include <string>
#include "Player.h"


class Game
{
public:
    Game();
private:
    Player user;

    void gameLoop();
};

Player.cpp

#include "Player.h"

Player::Player(std::string name)
{
    playerName = name;

}

std::string Player::getName() {
    std::string nameWatch = playerName;
    return playerName;
}

Player.h

#include <stdio.h>
#include <stdlib.h>
#include <string>

class Player
{
public:
    Player(std::string name);
    Player() {}

    std::string getName();

private:
    std::string playerName;
};

[1

[2

【问题讨论】:

    标签: c++ string pointers std


    【解决方案1】:

    Game::Game() 
    {
        Player user = Player("Foo");
        gameLoop();
    }
    

    你创建了一个局部变量user,它隐藏了this-&gt;user

    要初始化你的成员变量,你可以这样做

    Game::Game() : user("Foo")
    {
        gameLoop();
    }
    

    如果你有几个成员要初始化:

    Game::Game() : user("Foo"), comp("Monster")
    {
        gameLoop();
    }
    

    在做

    Game::Game()
    {
        user = Player("Foo");
        comp = Player("Monster");
        gameLoop();
    }
    

    创建一个默认的user/comp 并为其赋值,因此它要求Player 是默认可构造的

    【讨论】:

    • 您好,感谢您如此迅速地回答 - 我现在知道我做错了什么。我现在明白我正在覆盖头文件 Player 构造函数。但是,如果我有多个玩家,您的解决方案似乎有点难以做到。我现在正在使用它,它似乎很好。 code user = Player("Player"); comp = Player("怪物"); code
    • @user1972995:用反引号包围代码以在 SO 上突出显示它们。编辑处理多个成员。
    【解决方案2】:
    Game::Game()
    {
        Player user = Player("Foo"); 
        // you create local object with name user that override class-member object 
        // with same name. At least, you have user object like class member is empty cause
        // constr initialization-list is empty, and compiler call default const for std::string class that
        // exactly empty string, and local Player object with name user that die on close brace end.
    
        // First of all, use "Hungarian notation" style to detect class-member variables, for example:
        // 
        // class Hello
        // {
        //  private:
        //    int m_user;
        // }
    
    
        // At second, if one of your class-members haven't default constr and you should explicit call correct 
        // constructor use "Constructor initialization-list, like
        //
        // Game::Game() :
        //  m_user("Mike")
        // {
        //
        // }
    
        gameLoop();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多