我正在尝试不同版本的构造函数调用,最终得到了这个:
#include<iostream>
#include<string>
using namespace std;
class game{
public:
float version;
string name;
game()
{
name = "game";
version = 1.0;
}
game(float v,string n)
{
version = v;
name = n;
}
game(float v)
{
version = v;
name="any";
}
};
int main()
{
game lol1(1.0,"league of legends"); //functional form
game lol2 = 2.0; //assignment form
game lol3{3.0,"league2"}; //uniform initialization
game *pt = &lol1;
cout<<pt->name<<endl;
return 0;
}
每个语句都可以编译通过,但如果我写成以下形式:
game lol2 = 2.0,"league of legends2"; //code 2
我遇到了一个错误:
但是下面的代码可以正常工作:在字符串常量之前需要限定符标识符
game lol2 = {2.0,"league of legends2"}; //code 3
我不确定第二段代码的问题所在。有什么想法吗?