访问字符串会给出空值,即使它已赋值 对于一个项目,我正在做一个简单的基于文本的战斗游戏,在C++中,我不太熟悉。p>
我在将玩家的名字返回游戏控制器时遇到问题。 使用VisualStudio的watch功能,我可以看到在构造时正在设置名称,但是当我尝试在“getName”调用中访问它时,它是空的。这可能与指针有关,但我不确定 代码和图片如下 Game.cpp访问字符串会给出空值,即使它已赋值 对于一个项目,我正在做一个简单的基于文本的战斗游戏,在C++中,我不太熟悉。p>,c++,string,pointers,std,C++,String,Pointers,Std,我在将玩家的名字返回游戏控制器时遇到问题。 使用VisualStudio的watch功能,我可以看到在构造时正在设置名称,但是当我尝试在“getName”调用中访问它时,它是空的。这可能与指针有关,但我不确定 代码和图片如下 Game.cpp #include "Game.h" Game::Game() { Player user = Player("Foo"); gameLoop(); } void Game::gameLoop() { std::string
#include "Game.h"
Game::Game()
{
Player user = Player("Foo");
gameLoop();
}
void Game::gameLoop()
{
std::string name = user.getName();
printf("name: %s", name.c_str());
}
#include "Player.h"
Player::Player(std::string name)
{
playerName = name;
}
std::string Player::getName() {
std::string nameWatch = playerName;
return playerName;
}
Game.h
#include <stdio.h>
#include <string>
#include "Player.h"
class Game
{
public:
Game();
private:
Player user;
void gameLoop();
};
#include <stdio.h>
#include <stdlib.h>
#include <string>
class Player
{
public:
Player(std::string name);
Player() {}
std::string getName();
private:
std::string playerName;
};
Player.h
#include <stdio.h>
#include <string>
#include "Player.h"
class Game
{
public:
Game();
private:
Player user;
void gameLoop();
};
#include <stdio.h>
#include <stdlib.h>
#include <string>
class Player
{
public:
Player(std::string name);
Player() {}
std::string getName();
private:
std::string playerName;
};
#包括
#包括
#包括
职业选手
{
公众:
播放器(std::字符串名称);
播放器(){}
std::string getName();
私人:
std::字符串播放器名称;
};
[
[在
创建一个局部变量user
,它隐藏this->user
要初始化成员变量,可以执行以下操作
Game::Game() : user("Foo")
{
gameLoop();
}
如果要初始化多个成员:
Game::Game() : user("Foo"), comp("Monster")
{
gameLoop();
}
做
创建一个默认的用户
/comp
并在其值后赋值,因此它要求播放器
在
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();
}
创建一个局部变量user
,它隐藏this->user
要初始化成员变量,可以执行以下操作
Game::Game() : user("Foo")
{
gameLoop();
}
如果要初始化多个成员:
Game::Game() : user("Foo"), comp("Monster")
{
gameLoop();
}
做
创建一个默认的
用户
/comp
并在其值后赋值,因此它要求播放器
是默认可构造的嗨,谢谢你这么快回答-我现在知道我做错了什么。我现在知道我正在覆盖头文件播放器构造函数。但是你的解决方案似乎有点困难如果我有多个玩家,我会这么做。我现在正在使用这个,看起来很好。code
user=Player(“玩家”);comp=Player(“怪物”);code
@user1972995:在SO上用后引号环绕代码以突出显示它们。编辑以处理多个成员。嗨,谢谢你这么快回答-我现在知道我做错了什么。我现在了解到我正在覆盖头文件播放器构造函数。但是,如果我有多个播放器,你的解决方案似乎有点困难。我我现在使用这个,它看起来很好。code
user=Player(“Player”);comp=Player(“Monster”);code
@user1972995:用后引号将代码环绕起来,以便在SO上突出显示它们。编辑以处理多个成员。
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();
}