次のコードが機能しない理由:
#include <iostream>
class Entity
{
public:
/*
Entity()
{
std::cout << "Create Entity with default constructor" << std::endl;
}
*/
Entity(int x)
{
std::cout << "Create Entity with " << x << std::endl;
}
};
class Example
{
Entity ent;
int age;
public:
Example()
//: ent(7), age(7)
{
ent=Entity(7);
age=7;
}
};
int main() {
Example s1;
return 0;
}
エンティティのデフォルトコンストラクタが必要だと言っていますが、それはなぜですか? 私が使用している唯一の Entity オブジェクトは、1 つの引数を使用するコンストラクターを使用して構築されています。
Example s1;
さらに、 に変更する と、コードが別の方法で動作するようになるのはなぜですかExample s1();
(画面に印刷が表示されません。