2012-07-09 154 views
7

我看了一下前面的问题,但仍然不满意,因此我发布了这个。 我试图编译别人编写的C++代码。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 } 
}; 

编译与std C++ 11,我得到以下错误。为什么 '。'已在代码 中使用,同时分配值?

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 

我无法理解错误的原因。请帮忙。

+0

哪些线是编号34,35,36? – 2012-07-09 12:48:32

+3

您已获得C代码,而不是C++代码。尝试一个C编译器。 – 2012-07-09 12:49:23

+0

您不是第一个有此问题的人:http://stackoverflow.com/q/855996/1025391 – moooeeeep 2012-07-09 14:24:55

回答

3

你发布的是C代码,而不是C++代码(注意.c文件的扩展名)。但是,下面的代码:

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

应该可以正常工作。

您还可以阅读wiki中的C++ 11初始化列表。

+0

感谢您的输入。 – 2012-07-09 13:09:02

+0

但我的要求是我必须将源代码编译为C++ for ARMv7嵌入式处理器。由于其他文件是用C++选项编译的,所以我必须在C++中编译上述代码。它适用于C编译器,但我希望它在C++编译:( – 2012-07-09 13:10:41

+0

@AshokaK,所以你不能更改代码? – SingerOfTheFall 2012-07-09 13:15:58

1

指定的集合初始值设定项是一个C99特征,即它是C语言的一个特征。它不在C++中。

如果你坚持把它编译为C++,你必须重写cfg的初始化。

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

该代码将使用称为指定初始化 C99的功能。正如您所观察到的,该功能在C++和C++ 11中不可用。


正如this answer建议你应该使用C++代码的C编译器。您仍然可以将其链接到您的C++应用程序。您可以使用cmake为您执行构建配置。一个简单的例子:

/* 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) 

只需运行从源目录mkdir build; cd build; cmake ..; make

+0

@ moooeeeep ... 谢谢你。将检查解决方案。 正如有人所说:“锤子的钉子,螺丝刀的螺丝... 人类...?:) :)” – 2012-07-10 09:18:40

-1

感谢所有...

后,所有的分析,我发现上面的代码中有一个名为
指定初始化 C99功能。

要在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 */ 

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

这段代码编译正确,没有C++中的错误。

$$克++ -c initialze_list.cpp

$$克++ -c -std = GNU ++ 0x中initialze_list.cpp

+0

你应该接受[SingerOfTheFall](http://stackoverflow.com/a/11395350/1025391)然后我想。 – moooeeeep 2012-07-10 13:14:35

相关问题