函数返回空白

huangapple go评论99阅读模式
英文:

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 &lt;string&gt;
class Actor
{
private:
	void setName(std::string inputName);

public:
	std::string name{};
	
	Actor(std::string inputName = &quot;Actor&quot;);

	std::string getName();

};

#include &quot;Actor.h&quot;

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 &quot;Actor.h&quot;

class Character : public Actor
{
public:
	int maxHealth{};
	int currentHealth{};

	Character(std::string inputName = &quot;Character&quot;, int inputHealth = 100);
	int getCurrentHealth();

};

#include &quot;Character.h&quot;

Character::Character(std::string inputName, int inputHealth) : 
	Actor{inputName}, maxHealth{inputHealth}, currentHealth{inputHealth}
{

}

int Character::getCurrentHealth()
{
	return currentHealth;
}

Player Class

#pragma once
#include &quot;Character.h&quot;
class Player : public Character
{
public:
	Player();

};


#include &quot;Player.h&quot;

Player::Player():
	Character{&quot;Player&quot;, 200}
{
	
}
#include &lt;iostream&gt;
#include &quot;Player.h&quot;

int main()
{
	Player a{};

	std::cout &lt;&lt; &quot;Actor name is: &quot; + a.getName() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;Player health is: &quot; + a.getCurrentHealth() &lt;&lt; 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 &lt;&lt; not + to get the output you want.

std::cout &lt;&lt; &quot;Player health is: &quot; &lt;&lt; a.getCurrentHealth() &lt;&lt; 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.

huangapple
  • 本文由 发表于 2023年8月4日 20:58:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定