类c ++没有默认构造函数

时间:2017-12-12 11:14:19

标签: 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对那些完全没问题。

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

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

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

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

Player.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 个答案:

答案 0 :(得分:3)

name没有按照您的想法行事。它声明了Player类型的新变量Player并调用默认构造函数。如果要实例化匿名(Player(name)); // or Player{name}; // list initialization since C++11 变量,则需要编写

object NonFatal {
   def apply(t: Throwable): Boolean = t match {
     case _: VirtualMachineError | _: ThreadDeath | _: InterruptedException | _: LinkageError | _: ControlThrowable => false
     case _ => true
   }

  def unapply(t: Throwable): Option[Throwable] = if (apply(t)) Some(t) else None
}