函数返回空白

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

Function returning blank

问题

我在尝试测试C++中非常基本的面向对象编程,但遇到了一个问题。我正在编写以下类:

演员类:

  1. #pragma once
  2. #include <string>
  3. class Actor
  4. {
  5. private:
  6. void setName(std::string inputName);
  7. public:
  8. std::string name{};
  9. Actor(std::string inputName = "Actor");
  10. std::string getName();
  11. };
  1. #include "Actor.h"
  2. Actor::Actor(std::string inputName)
  3. {
  4. setName(inputName);
  5. }
  6. void Actor::setName(std::string inputName)
  7. {
  8. name = inputName;
  9. }
  10. std::string Actor::getName()
  11. {
  12. return name;
  13. }

角色类

  1. #pragma once
  2. #include "Actor.h"
  3. class Character : public Actor
  4. {
  5. public:
  6. int maxHealth{};
  7. int currentHealth{};
  8. Character(std::string inputName = "Character", int inputHealth = 100);
  9. int getCurrentHealth();
  10. };
  1. #include "Character.h"
  2. Character::Character(std::string inputName, int inputHealth) :
  3. Actor(inputName), maxHealth(inputHealth), currentHealth(inputHealth)
  4. {
  5. }
  6. int Character::getCurrentHealth()
  7. {
  8. return currentHealth;
  9. }

玩家类

  1. #pragma once
  2. #include "Character.h"
  3. class Player : public Character
  4. {
  5. public:
  6. Player();
  7. };
  1. #include "Player.h"
  2. Player::Player() :
  3. Character("Player", 200)
  4. {
  5. }
  1. #include <iostream>
  2. #include "Player.h"
  3. int main()
  4. {
  5. Player a{};
  6. std::cout << "Actor name is: " + a.getName() << std::endl;
  7. std::cout << "Player health is: " + a.getCurrentHealth() << std::endl;
  8. return 0;
  9. }

在这种情况下,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:

  1. #pragma once
  2. #include &lt;string&gt;
  3. class Actor
  4. {
  5. private:
  6. void setName(std::string inputName);
  7. public:
  8. std::string name{};
  9. Actor(std::string inputName = &quot;Actor&quot;);
  10. std::string getName();
  11. };
  1. #include &quot;Actor.h&quot;
  2. Actor::Actor(std::string inputName)
  3. {
  4. setName(inputName);
  5. }
  6. void Actor::setName(std::string inputName)
  7. {
  8. name = inputName;
  9. }
  10. std::string Actor::getName()
  11. {
  12. return name;
  13. }

Character Class

  1. #pragma once
  2. #include &quot;Actor.h&quot;
  3. class Character : public Actor
  4. {
  5. public:
  6. int maxHealth{};
  7. int currentHealth{};
  8. Character(std::string inputName = &quot;Character&quot;, int inputHealth = 100);
  9. int getCurrentHealth();
  10. };
  1. #include &quot;Character.h&quot;
  2. Character::Character(std::string inputName, int inputHealth) :
  3. Actor{inputName}, maxHealth{inputHealth}, currentHealth{inputHealth}
  4. {
  5. }
  6. int Character::getCurrentHealth()
  7. {
  8. return currentHealth;
  9. }

Player Class

  1. #pragma once
  2. #include &quot;Character.h&quot;
  3. class Player : public Character
  4. {
  5. public:
  6. Player();
  7. };
  1. #include &quot;Player.h&quot;
  2. Player::Player():
  3. Character{&quot;Player&quot;, 200}
  4. {
  5. }
  1. #include &lt;iostream&gt;
  2. #include &quot;Player.h&quot;
  3. int main()
  4. {
  5. Player a{};
  6. std::cout &lt;&lt; &quot;Actor name is: &quot; + a.getName() &lt;&lt; std::endl;
  7. std::cout &lt;&lt; &quot;Player health is: &quot; + a.getCurrentHealth() &lt;&lt; std::endl;
  8. return 0;
  9. }

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++中没有将整数添加到字符串的操作。使用 << 而不是 + 来获得您想要的输出。

  1. 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.

  1. 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:

确定