C ++表示数字常量之前的预期标识符

时间:2015-08-22 11:47:08

标签: 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;
    }
};

3 个答案:

答案 0 :(得分:5)

<强> C ++ 03

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

include_once('../db.php');

session_start();

$seller_name = $_SESSION['seller_name'];
echo $seller_name;

  if (!empty($_POST['flag_image'])){
    $flags_array = $_POST['flag_image'];
    $flags_string = implode(",",$flags_array);
  }else{
     $flags_string = '';
  }

$cont_eu = '';

   // Assign Flags to Continents
    foreach ($flags_array as $flag_image){  
     $qry_find_flag_continent = " SELECT continent FROM flags WHERE flag_image = '$flag_image' ";
     $result = $dbdetails->query($qry_find_flag_continent);

 while($row = mysqli_fetch_array($result)) {
    $continent = $row["continent"];


    if ($continent == 'EU') {
     $cont_eu .= $flag_image;
    }   
} 
echo $cont_eu;

<强> C ++ 11

您必须使用大括号 相同的初始化程序。

class SetData
{
private:
    data obj;

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

为什么不允许使用括号,请参阅Non-static data member initializers提案。向下滚动到&#34; Kona提出的关于标识符范围的问题&#34;

  

类范围查找的动机是我们希望能够   把任何东西都放在我们可以的非静态数据成员的初始化器中   放入mem-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
     

不幸的是,这会使“(表达式列表)”的初始化者   在解析声明时表单不明确:

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();
};
     

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

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

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

类似的解决方案是目前应用另一个现有规则   仅在模板中使用,如果T可以是类型或其他类型,   那就是别的了;如果我们真的是这个意思,我们可以使用“typename”   一种类型:基本上

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

这两种解决方案都会引入可能存在的微妙之处   被许多用户误解(正如许多问题所证明的那样)   comp.lang.c ++关于为什么“int i();”在块作用域中没有声明a   default-initialized int)。

     

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

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

答案 1 :(得分:1)

不允许以这种方式初始化非静态数据成员。您应该使用支撑或相等的初始化程序

class SetData
{
private:
    // data obj = {43,"185 Awan Market","Talha"}; is also valid
    data obj{43,"185 Awan Market","Talha"};

(CFR)。 non-static data members initialization

替代解决方案:构造函数初始化列表

class SetData
{
private:
    data obj;

public:
    SetData() : obj(43,"185 Awan Market","Talha") {}

    void show() 
    ...
};

至于为什么非静态数据成员初始化不支持括号,我建议阅读这篇文章:Why C++11 in-class initializer cannot use parentheses?

答案 2 :(得分:0)

您无法像这样初始化内联对象,您必须在构造函数初始值设定项列表中执行此操作:

class SetData
{
private:
    data obj;

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