英文:
Function returning blank
问题
我在尝试测试C++中非常基本的面向对象编程,但遇到了一个问题。我正在编写以下类:
演员类:
#pragma once
#include <string>
class Actor
{
private:
void setName(std::string inputName);
public:
std::string name{};
Actor(std::string inputName = "Actor");
std::string getName();
};
#include "Actor.h"
Actor::Actor(std::string inputName)
{
setName(inputName);
}
void Actor::setName(std::string inputName)
{
name = inputName;
}
std::string Actor::getName()
{
return name;
}
角色类
#pragma once
#include "Actor.h"
class Character : public Actor
{
public:
int maxHealth{};
int currentHealth{};
Character(std::string inputName = "Character", int inputHealth = 100);
int getCurrentHealth();
};
#include "Character.h"
Character::Character(std::string inputName, int inputHealth) :
Actor(inputName), maxHealth(inputHealth), currentHealth(inputHealth)
{
}
int Character::getCurrentHealth()
{
return currentHealth;
}
玩家类
#pragma once
#include "Character.h"
class Player : public Character
{
public:
Player();
};
#include "Player.h"
Player::Player() :
Character("Player", 200)
{
}
#include <iostream>
#include "Player.h"
int main()
{
Player a{};
std::cout << "Actor name is: " + a.getName() << std::endl;
std::cout << "Player health is: " + a.getCurrentHealth() << std::endl;
return 0;
}
在这种情况下,a.getName()
正常打印,返回字符串 "Player"。然而,a.getCurrentHealth
没有打印。不仅仅是值,而是整个句子 "Player health is: " 都没有打印。有关发生了什么的任何想法吗?
英文:
I was trying to test very basic OO in c++, but I ran into a problem. I was writing the following classes:
Actor Class:
#pragma once
#include <string>
class Actor
{
private:
void setName(std::string inputName);
public:
std::string name{};
Actor(std::string inputName = "Actor");
std::string getName();
};
#include "Actor.h"
Actor::Actor(std::string inputName)
{
setName(inputName);
}
void Actor::setName(std::string inputName)
{
name = inputName;
}
std::string Actor::getName()
{
return name;
}
Character Class
#pragma once
#include "Actor.h"
class Character : public Actor
{
public:
int maxHealth{};
int currentHealth{};
Character(std::string inputName = "Character", int inputHealth = 100);
int getCurrentHealth();
};
#include "Character.h"
Character::Character(std::string inputName, int inputHealth) :
Actor{inputName}, maxHealth{inputHealth}, currentHealth{inputHealth}
{
}
int Character::getCurrentHealth()
{
return currentHealth;
}
Player Class
#pragma once
#include "Character.h"
class Player : public Character
{
public:
Player();
};
#include "Player.h"
Player::Player():
Character{"Player", 200}
{
}
#include <iostream>
#include "Player.h"
int main()
{
Player a{};
std::cout << "Actor name is: " + a.getName() << std::endl;
std::cout << "Player health is: " + a.getCurrentHealth() << std::endl;
return 0;
}
In this case, a.getName()
prints normally, returning the string "Player". However, a.getCurrentHealth
does not print. Not just the value, but the entire sentence "Player health is: " is not printing. Any idea on what's happening?
答案1
得分: 5
C++中没有将整数添加到字符串的操作。使用 <<
而不是 +
来获得您想要的输出。
std::cout << "Player health is: " << a.getCurrentHealth() << std::endl;
您的代码正在执行 指针算术。字符串文字被解释为 char
指针,然后将健康值添加到该指针,然后将结果地址(可能指向任何地方)打印出来,就好像它指向了C样式字符串。
英文:
There's no operation in C++ that adds an integer to a string. Use <<
not +
to get the output you want.
std::cout << "Player health is: " << a.getCurrentHealth() << std::endl;
What your code is doing is pointer arithmetic. The string literal was interpreted as a char
pointer, the health value was then added to that pointer and the resulting address (which could have been pointing anywhere) was printed as if it pointed to a C style string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论