开发者社区> 问答> 正文

c++ 不支持 c 的 typedef struct 语法?报错

用eclipse 创建c++项目,

定义c的结构体报错

结构体如下:

typedef struct aeEventLoop {
    int maxfd;   /* highest file descriptor currently registered */
    int setsize; /* max number of file descriptors tracked */
    long long timeEventNextId;
    time_t lastTime;     /* Used to detect system clock skew */
    aeFileEvent *events; /* Registered events */
    aeFiredEvent *fired; /* Fired events */
    aeTimeEvent *timeEventHead;
    int stop;
    void *apidata; /* This is used for polling API specific data */
    aeBeforeSleepProc *beforesleep;
}aeEventLoop;

 

异常如下

error: changes meaning of ‘aeEventLoop’ from ‘typedef struct aeEventLoop aeEventLoop’ [-fpermissive]

执行命令如下

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/server.d" -MT"src/server.o" -o "src/server.o" "../src/server.cpp"

 

执行命令如下

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/server.d" -MT"src/server.o" -o "src/server.o" "../src/server.cpp"

展开
收起
爱吃鱼的程序员 2020-06-08 12:00:03 708 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    aeEventLoop明显重名了。

    typedefstruct{...}aeEventLoop;

    这样不行吗?或者typedefstructaeEventLoopEx这样换个名字

    不行,还是报错

    }aeEventLoop; 这一行直接修改为 }; 就可以了。

    不行。
    [root@localhostDocuments]#cattest.c#include<iostream>typedefstruct{inta;}aeEventLoop;intmain(){aeEventLoopael;ael.a=3456;std::cout<<ael.a<<std::endl;return0;}[root@localhostDocuments]#g++test.c[root@localhostDocuments]#./a.out3456[root@localhostDocuments]#

    写法是没问题,我这里可以编译

    我是使用eclipse作为ide,真的不知道哪里报错了。

    typedef 去掉。

     

    去掉其他地方报错,那么饿其他地方需要修改成structaeEventLoop×aeEventLoop
    $catserver.cpp#include<iostream>typedefstructaeEventLoop{intmaxfd;intminfd;};intmain(){aeEventLoopael;ael.maxfd=10;ael.minfd=0;std::cout<<ael.maxfd<<"\n"<<ael.minfd<<std::endl;return0;}$g++server.cppserver.cpp:3:1:warning:'typedef'wasignoredinthisdeclarationtypedefstructaeEventLoop{^~~~~~~$./a.exe100

    gccversion7.1.0(Rev2,BuiltbyMSYS2project)

    2020-06-08 12:00:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载