c++ - 类 c++ 不存在默认构造函数

标签 c++ default-constructor anonymous-objects

<分区>

您好, 我正在尝试使用 std::string 变量“name”实例化一个匿名对象。但是 intellisenen 给我错误提示

E0291   no default constructor exists for class "Player"    GoldGame    e:\C++ Projects\Hello World\GoldGame\GoldGame.cpp   17  

我提供了一个构造函数,它可以只接受一个 std::string 变量,因为其他参数都提供了默认值。

你们能解释一下吗?

更让我困惑的是,当我改变的时候

Player(name);

Player a(name);

或到

Player("test");

然后 intellisense 就完全没问题了。


黄金游戏.cpp

#include "stdafx.h"
#include "Creature.h"
#include "Player.h"
#include <iostream>
#include <string>


int main()
{
    std::cout << "Enter your name: ";
    std::string name;
    std::cin >> name;

    Player(name);


    return 0;
}

生物.h

#pragma once
#include <string>
class Creature
{
public:
    Creature(const std::string &name, const char symbol, const int health, const int damage, const int gold);
    ~Creature();

    //getters
    const std::string& getName() { return m_name; }
    const char getSymbol() { return m_symbol; }
    const int getHealth() { return m_health; }
    const int getDamage() { return m_damage; }
    const int getGold() { return m_gold; }

    //health, gold and dead 
    void reduceHealth(const int healthMinus);
    void addGold(const int gold);
    bool isDead();

private:
    std::string m_name;
    char m_symbol;
    int m_health;
    int m_damage;
    int m_gold;
};

生物.cpp

#include "stdafx.h"
#include "Creature.h"




Creature::Creature(const std::string & name, const char symbol, const int health, const int damage, const int gold)
    :m_name(name), m_symbol(symbol), m_health(health), m_damage(damage), m_gold(gold)
{
}

Creature::~Creature()
{
}

void Creature::reduceHealth(const int healthMinus)
{
    m_health -= healthMinus;
}

void Creature::addGold(const int gold)
{
    m_gold += gold;
}

bool Creature::isDead()
{
    if (m_health>0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

播放器.h

#pragma once
#include "Creature.h"
#include <string>

class Player :
    public Creature
{
public:
    Player(const std::string &name, const char symbol='@', const int health=10, const int damage=1, const int gold=0);
    ~Player();
    const int getLevel() { return m_level; }
    void levelUp();
    bool hasWon();
private:
    int m_level;
};

播放器.cpp

#include "stdafx.h"
#include "Player.h"




Player::Player(const std::string & name, const char symbol, const int health, const int damage, const int gold)
    :Creature(name,symbol,health,damage,gold)
{
}

Player::~Player()
{
}

void Player::levelUp()
{
    ++m_level;
}

bool Player::hasWon()
{
    if (m_level>=20)
    {
        return true;
    }
    else
    {
        return false;
    }
}

最佳答案

Player(name); 并不像您认为的那样。它声明了一个类型为 Player 的新变量 name 并调用了一个默认构造函数。如果你想实例化一个匿名的 Player 变量那么你需要写

(Player(name));
// or
Player{name}; // list initialization since C++11

关于c++ - 类 c++ 不存在默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47771257/

相关文章:

c++ - 如何配置基于 cmake 的项目以使用 Qt 进行构建

c++ - 如何在 std::bind 中使用模板函数参数?

C++ 默认构造函数语法

c++ - Google 测试 - 构造函数声明错误

c++ - 为什么某些编译器拒绝作为指针传递的匿名对象?

c# - 如何将匿名对象序列化为 JSON 而不包含属性名称

java - 匿名对象创建和 GC

从 HTML 中抓取 C++ 屏幕

c++ - 为什么在增加指针的整数值时得到一个随机数?

c++ - 不明确的重载、隐式转换和显式构造函数