避免typedef c++中的冲突声明错误



有没有办法让g++忽略或处理冲突的typedef?

背景:

我正在为gridab_d模拟器编写一些c++代码。我的模型需要连接到一个c++数据库,所以我使用mysql++库。使用mysql++库需要链接到mysql库,所以我使用进行编译

g++ -I/usr/include/mysql -I/usr/local/include/mysql++

问题:

网格标签typedef中的mysql.h和list.h都是一个名为list的结构。这是编译器错误

In file included from /usr/include/mysql/mysql.h:76, 
             from /usr/include/mysql++/common.h:182,
             from /usr/include/mysql++/connection.h:38,
             from /usr/include/mysql++/mysql++.h:56,
             from direct_data.cpp:21:
/usr/include/mysql/my_list.h: At global scope:
/usr/include/mysql/my_list.h:26: error: conflicting declaration 'typedef struct st_list LIST'
../core/list.h:22: error: 'LIST' has a previous declaration as 'typedef struct s_list LIST'

谢谢你的帮助!

也许预处理器包含了问题的解决方案。

#define LIST GRIDLAB_LIST
#include <gridlab_include_file.h>
#undef LIST

当然,这依赖于网格标签,而不是MySQL中的#include

最佳解决方案:

1) 保留您当前的主程序

   EXAMPLE: "main.cpp"

2) 为您的数据库访问编写一个新的模块

   EXAMPLE: dbaccess.cpp, dbaccess.h

3) #在main.cpp 中包含"dbaccess.h"

在dbaccess代码中,您不需要任何对gridab的引用;您不需要在dbaccess.*代码之外引用mySql或mySql列表。

问题已解决:)?

PS:如果你真的需要某种可以在不同模块之间共享的"列表",我鼓励你使用标准C++"vector<>"之类的东西。依我拙见

我假设您在多个文件中使用SSQLS。您是否阅读了有关在多个文件中使用SSQLS的说明。

http://tangentsoft.net/mysql++/头中的doc/html/userman/sqls.html#ssqls

有两种可能性——要么这两种列表类型兼容,要么它们不兼容。如果它们是兼容的,您可以将定义复制到一个新的标头中,并从每个位置包含该标头。如果它们不兼容,您将不得不更改其中一个名称。

编辑:以下是我在谷歌搜索中发现的两个结构定义:

MySQL:

typedef struct st_list {
  struct st_list *prev,*next;
  void *data;
} LIST;

Gridlab:

typedef struct s_listitem {
    void *data;
    struct s_listitem *prev;
    struct s_listitem *next;
} LISTITEM;
typedef struct s_list {
    unsigned int size;
    LISTITEM *first;
    LISTITEM *last;
} LIST;

看着这些,你似乎不会把它们按摩成同一种类型。更改其中一个名称——要么进行大搜索/替换,要么使用一些巧妙的#define技巧——注意,如果选择后一条路线,你不会犯任何错误。

最新更新