2017-12-12 6 views
0

こんにちは、 私がのstd ::文字列変数「name」を持つ匿名オブジェクトをインスタンス化しようとしているために存在しません。しかしintellisenenは私が他のパラメータは、デフォルト値が設けられているので、ただのstd ::文字列変数を取ることができ、コンストラクタを提供しているいいえデフォルトコンストラクタは、クラスC++

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

というエラーを与えます。

あなたはこれについていくつかの光を当てることができますか?

何でももっと私を混乱することは、私は

Player a(name); 

または

Player("test"); 

への

Player(name); 

を変更するときにインテリセンスが、それらと完全に罰金になることです。


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

1.インテリセンスエラーはエラーをコンパイラされていません。あなたの匿名のインスタンスは何のために良いですか? – user1810087

+0

私は匿名のオブジェクトを使用するつもりはありませんでした。そして、それがエラーを投げるという事実は私を混乱させる。 –

答えて

3

Player(name);は、あなたはそれがないと思う何をしません。これは、Playerという新しい変数nameを宣言し、デフォルトのコンストラクタを呼び出します。匿名Player変数をインスタンス化したいなら、あなたは記述する必要が

(Player(name)); 
// or 
Player{name}; // list initialization since C++11 
関連する問題