【问题标题】:No default constructor exists for class c++ [duplicate]c ++类不存在默认构造函数[重复]
【发布时间】:2017-12-12 11:14:19
【问题描述】:

你好, 我正在尝试使用 std::string 变量“名称”实例化一个匿名对象。但是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");

然后智能感知就变得完全没问题了。


GoldGame.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;
    }
}

【问题讨论】:

  • 1. Intellisense 错误不是编译器错误。 2. 你的匿名 Player 实例有什么用?
  • 我不打算使用匿名对象,但有点溜进去了。它抛出错误的事实让我感到困惑。

标签: c++ default-constructor anonymous-objects


【解决方案1】:

Player(name); 并没有按照你的想法去做。它声明一个Player 类型的新变量name 并调用默认构造函数。如果你想实例化一个匿名的Player 变量那么你需要写

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

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多