gpt4 book ai didi

c++ - 错误 : expected identifier before string constant or error: 'perf' is not a type

转载 作者:行者123 更新时间:2023-11-30 01:15:42 24 4
gpt4 key购买 nike

我遇到以下错误,我尝试了这两种方法但没有解决。

下面是代码

   class Stat_S{
public:
Stat_S(const char *name) :
{
........
}

~Stat_S();
};


struct temp {
Stat_S sp("ppin");
}

错误:字符串常量之前需要标识符

class Stat_S{
public:
Stat_S(const char *name) :
{
........
}

~Stat_S();
};

const char *temp="ppin";
struct temp {
Stat_S sp(temp);
}

错误:'temp' 不是类型

 class Stat_S{
public:
Stat_S(const char *name) :
{
........
}

~Stat_S();
};

struct temp {
Stat_S*sp = new Stat_S("ppin");
}

工作正常没有错误

main()
{
static temp2 *temp;
temp2 = new temp[2];
}

如何解决第一种或第二种情况?我想从 struct temp 调用 Stat_S 的构造函数。我不想使用第三种情况,因为我已经有了使用点 (.) 作为 sp 的大定义我不想在使用实例后将其更改为 ->。

最佳答案

非静态成员的类内初始化可以使用大括号或相等的初始化器来执行。第三种情况是使用 equal 的实例。要正确执行第一个或第二个,请使用这样的大括号:

struct temp {
Stat_S sp{"ppin"};
}

关于c++ - 错误 : expected identifier before string constant or error: 'perf' is not a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28057519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com