避免typedef c++中的冲突声明错误
Avoiding conflicting declaration errors in 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
技巧——注意,如果选择后一条路线,你不会犯任何错误。
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 错误:使用通用引用的声明冲突
- 前向声明中的名称冲突
- VS2017 #error: : snprintf 的宏定义与标准库函数声明冲突
- 冲突声明/重新定义:不同的基本类型
- HDF5 C++ Qt 冲突声明
- 冲突的声明(外部基础与派生)
- 错误:使用 tesseract 引擎时 C++ 中的声明'typedef struct tagBLOB BLOB'冲突
- 具有相同名称的方法和内部类(错误:.. 与前面的声明冲突)
- 重载与使用声明冲突
- 类静态成员的冲突声明
- C++冲突声明的标准定义
- 错误:多个文件中的结构声明冲突
- C++ h 和 cpp 文件中的指针声明冲突
- constexpr静态结构类成员的声明冲突
- C++ 中的声明冲突
- 冲突的声明类型/价值不匹配
- 解耦嵌套类:使用模板别名时出现"模板声明冲突"错误
- 避免typedef c++中的冲突声明错误
- c++类冲突声明错误