错误:字符串常量之前的预期标识符或错误:'perf'不是类型

error: expected identifier before string constant or error: 'perf' is not a type

本文关键字:错误 类型 perf 常量 字符串 标识符      更新时间:2023-10-16

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

下面是代码

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

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

非静态成员的类内初始化可以使用大括号或等值初始值设定项执行。第 3 种情况是使用等于的实例。要正确执行第 1 个或第 2 个,请使用如下大括号:

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