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

时间:2015-01-21 00:12:13

标签: c++ gcc

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

以下是代码

   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我不想将其更改为 - >使用实例后。

1 个答案:

答案 0 :(得分:5)

可以使用大括号或相等的初始化程序来执行非静态成员的类内初始化。第三种情况是使用相等的实例。要正确执行第1个或第2个,请使用以下大括号:

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