【问题标题】:C++ error: expected primary-expression before ‘.’ tokenC++ 错误:“。”标记之前的预期主表达式
【发布时间】:2012-07-08 20:45:36
【问题描述】:

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

使用标准 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

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

【问题讨论】:

  • 第 34,35,36 行是哪几行?
  • 你得到的是 C 代码,而不是 C++ 代码。试试 C 编译器。
  • 你不是第一个遇到这个问题的人:stackoverflow.com/q/855996/1025391

标签: c++ c++11 initialization


【解决方案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++ 中正确编译,没有错误。

$$ g++ -c initialze_list.cpp

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

【讨论】:

【解决方案2】:
/* 
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

【讨论】:

  • @moooeeeep ...谢谢你。将检查解决方案。正如有人说“钉子的锤子,螺丝刀的螺丝刀......对于人类......?:) :)”
【解决方案3】:

指定的聚合初始值设定项是 C99 的特性,即它是 C 语言的特性。它在 C++ 中不存在。

如果你坚持编译成C++,你就得重写cfg的初始化。

【讨论】:

    【解决方案4】:

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

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

    应该可以正常工作。

    您还可以在wiki 中阅读有关 C++11 初始化列表的信息。

    【讨论】:

    • 但我的要求是我必须将源代码编译为 ARMv7 嵌入式处理器的 C++。由于其他文件是使用 C++ 选项编译的,因此我也必须在 C++ 中编译上述代码。它适用于 C 编译器,但我希望它在 C++ 中编译:(
    • 是的,我不能更改代码。上面的代码是更改了成员/结构名称的精确副本。请帮我用 C++ 编译源代码。我正在使用的确切编译命令是: $ /bin/arm-none-linux-gnueabi-c++ -O3 -std=gnu++0x -march=armv7-a -mtune=cortex-a8 -mfpu=neon -ffast-数学文件.c
    • 不是编译成 C++ 吗?即使这是作为 C 代码编写的,它看起来也应该像 C++ 一样编译(除了导致错误的行,但我从不会用 C 编写),尽管您可能会收到有关您的标头名称的警告已弃用。
    • @Eric Finn: except for the lines that were causing an error -> 没错。
    • @AshokaK:如果您坚持将代码编译为 C++,则必须将其从 C 移植到 C++。
    猜你喜欢
    • 2015-03-31
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2013-01-13
    • 2017-09-24
    相关资源
    最近更新 更多