简体   繁体   English

C ++错误:'。'之前的预期primary-expression 代币

[英]C++ error: expected primary-expression before ‘.’ token

I looked at the earlier questions but still i was not satisfied, hence i am posting this. 我看了之前的问题,但仍然不满意,因此我发布了这个。 I was trying to compile the C++ code written by someone else. 我试图编译其他人编写的C ++代码。

/*
file1.h
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    struct
    {   
        unsigned member1;
        unsigned  member2; 
    } str1;

    struct
    {
        unsigned member3;
        unsigned  member4; 
    } str2;

    struct
    {
        unsigned member5;
        unsigned  member6; 
    } str3;
} CONFIG_T;



/* 
file1.c
*/
CONFIG_T  cfg =
{
    .str1 = { 0x01, 0x02 },
    .str2 = { 0x03, 0x04 },
    .str3 = { 0x05, 0x06 }
};

Compiled with std C++11 and i get below error. 用std C ++ 11编译,我得到以下错误。 Why the '.' 为什么 '。' has been used in code while assigning values ? 在分配值时是否已在代码中使用?

home $$  g++ -c -std=gnu++0x  initialze_list.cpp

initialze_list.cpp:34: error: expected primary-expression before ‘.’ token

initialze_list.cpp:35: error: expected primary-expression before ‘.’ token

initialze_list.cpp:36: error: expected primary-expression before ‘.’ token

I was not able to understand the reason for error. 我无法理解错误的原因。 Please help. 请帮忙。

What you posted is C code, not C++ code (note the .c file extention). 您发布的是C代码,而不是C ++代码(请注意.c文件扩展名)。 However, the following code: 但是,以下代码:

CONFIG_T  cfg =
{
    { 0x01, 0x02 },
    { 0x03, 0x04 },
    { 0x05, 0x06 }
};

should work fine. 应该工作正常。

You can also read about C++11 initialize lists in the wiki . 您还可以阅读Wiki中的C ++ 11初始化列表。

Designated aggregate initializers is a C99 feature, ie it is a feature of C language. 指定的聚合初始化器是C99特性,即它是C语言的一个特性。 It is not present in C++. 它不存在于C ++中。

If you insist on compiling it as C++, you'll have to rewrite the initialization of cfg . 如果你坚持将它编译为C ++,你将不得不重写cfg的初始化。

/* 
file1.c
*/
CONFIG_T  cfg =
{
  .str1 = { 0x01, 0x02 },
  .str2 = { 0x03, 0x04 },
  .str3 = { 0x05, 0x06 }
};

That code is using a C99 feature called designated initializers . 该代码使用称为指定初始化程序的C99功能。 As you have observed, that feature is not available in C++ and C++11. 正如您所观察到的,C ++和C ++ 11中没有该功能。


As suggested in this answer you should use a C compiler for C code. 本回答所示,您应该使用C编译器来代码。 You can still link it to your C++ application. 您仍然可以将它链接到您的C ++应用程序。 You could use cmake to do the build configuration for you. 您可以使用cmake为您执行构建配置。 A simple example: 一个简单的例子:

/* f1.h */
#ifndef MYHEADER
#define MYHEADER

typedef struct { int i, j; } test_t; 
extern test_t t;

#endif

/* f1.c */
#include "f1.h"
test_t t = { .i = 5, .j = 6 };

/* f0.cc */
extern "C" { #include "f1.h" }
#include <iostream>

int main() {
    std::cout << t.i << " " << t.j << std::endl;
}

# CMakeLists.txt
add_executable(my_executable f0.cc f1.c)

just run mkdir build; cd build; cmake ..; make 只需运行mkdir build; cd build; cmake ..; make mkdir build; cd build; cmake ..; make mkdir build; cd build; cmake ..; make from your source directory. mkdir build; cd build; cmake ..; make您的源目录。

Thanks to all ... 谢谢大家 ...

After all the analysis i found that the above code has C99 feature called 经过所有分析后我发现上面的代码都有C99功能调用
Designated initializer . 指定的初始化程序

To compile this code in C++ i have changed the code to normal initialization as below. 要在C ++中编译此代码,我已将代码更改为正常初始化,如下所示。

========================== ==========================

/*
 *  initialze_list.cpp 
 */

#include <stdio.h>

typedef struct
{
    struct
{   unsigned member1;
    unsigned  member2; 
} str1;
struct
{   unsigned member3;
    unsigned  member4; 
} str2;
struct
{   unsigned member5;
    unsigned  member6; 
} str3;
} CONFIG_T;

CONFIG_T  cfg =
{
 { 0x01, 0x02 },
 { 0x03, 0x04 },
 { 0x05, 0x06 }
};
/* End of file  */

================================== ==================================

This code compiled properly without the error in C++. 这段代码编译正确,没有C ++中的错误。

$$ g++ -c initialze_list.cpp $$ g ++ -c initialze_list.cpp

$$ g++ -c -std=gnu++0x initialze_list.cpp $$ g ++ -c -std = gnu ++ 0x initialze_list.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM