C ++在数字常数之前表示期望的标识符 [英] C++ says expected identifier before numeric constant

查看:69
本文介绍了C ++在数字常数之前表示期望的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class data
{
private:
    int ID;
    string address,name;
public:
    data(int i,string a,string n):ID(i),address(a),name(n){}
    friend class SetData;
};
class SetData
{
private:
    data obj(43,"185 Awan Market","Talha"); //this is where the error happens

public:
    void show()
    {
        cout<<"Name is: "<<obj.name<<endl;
        cout<<"Address is: "<<obj.address<<endl;
        cout<<"ID is: "<<obj.ID;
    }
};

推荐答案

C ++ 03

它属于构造函数的mem-initializer:

It belongs in the constructor's mem-initializer:

class SetData
{
private:
    data obj;

public:
    SetData() : obj(43,"185 Awan Market","Talha")
    {
    }
    // Rest goes here...
};

C ++ 11

您必须使用括号 相等的初始化程序.

You must use a brace or equal initializer.

// Fine
data obj{43,"185 Awan Market","Talha"};
// Fine, too
data obj = data(43,"185 Awan Market","Talha"); //this is where the error happens

为什么不允许使用括号,请参见非静态数据成员初始化程序建议.向下滚动到在Kona中提出的有关标识符范围的问题"

For why parentheses are not allowed, see the Non-static data member initializers proposal. Scroll down to "An issue raised in Kona regarding scope of identifiers"

进行类范围查找的动机是,我们希望能够将任何东西放在非静态数据成员的初始化器中,我们可以放入mem初始化程序,而不会显着改变语义(模直接初始化与复制初始化):

The motivation for class-scope lookup is that we’d like to be able to put anything in a non-static data member’s initializer that we could put in a mem-initializer without significantly changing the semantics (modulo direct initialization vs. copy initialization):

int x();

struct S {
    int i;
    S() : i(x()) {} // currently well-formed, uses S::x()
    // ...
    static int x();
};

struct T {
    int i = x(); // should use T::x(), ::x() would be a surprise
    // ...
    static int x();
};

不幸的是,这使(expression-list)"的初始化程序解析声明时的表单不明确:

Unfortunately, this makes initializers of the "( expression-list )" form ambiguous at the time that the declaration is being parsed:

struct S {
    int i(x); // data member with initializer
    // ...
    static int x;
};

struct T {
    int i(x); // member function declaration
    // ...
    typedef int x;
};

一种可能的解决方案是依靠现有规则,如果声明可以是一个对象或一个函数,然后是一个函数:

One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function:

struct S {
    int i(j); // ill-formed...parsed as a member function,
              // type j looked up but not found
    // ...
    static int j;
};

一种类似的解决方案是应用另一条现有规则,目前仅在模板中使用,如果T可以是类型或其他类型,那是另外一回事了;如果真的意思是我们可以使用类型名"类型:本质上

A similar solution would be to apply another existing rule, currently used only in templates, that if T could be a type or something else, then it’s something else; and we can use "typename" if we really mean a type: Essentially

struct S {
    int i(x); // unabmiguously a data member
    int j(typename y); // unabmiguously a member function
};

这两种解决方案都可能引入一些细微之处被许多用户误解了(如以下问题所证明的:关于为什么"int i();"的comp.lang.c ++在区块范围内未声明默认初始化的int).

Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why "int i();" at block scope doesn’t declare a default-initialized int).

本文提出的解决方案是仅允许初始化"= initializer-clause"和"{initializer-list}"形式.那解决了大多数情况下的歧义问题,例如:

The solution proposed in this paper is to allow only initializers of the "= initializer-clause" and "{ initializer-list }" forms. That solves the ambiguity problem in most cases, for example:

HashingFunction hash_algorithm{"MD5"};

这篇关于C ++在数字常数之前表示期望的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆