[GCC] C / C ++ Struct Ambiguity [英] [GCC] C/C++ Struct Ambiguity

查看:75
本文介绍了[GCC] C / C ++ Struct Ambiguity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用C语言编写的带有C ++绑定的游戏引擎,并且遇到了一些含糊不清的问题。这是我标题中有问题部分的简化版本:





I'm working on a game engine written in C with C++ bindings and have run into slight snag with ambiguity. Here's a simplified version of the problematic part of my header:


/* C Code """""""""""""""""""""""""""""""""""""""""" */
extern "C"
{

struct Engine_Module
  {
    char *name;
  };

typedef struct Module Module;


extern int Engine_Module_Begin(Engine_Module *this);



}
/* CPP Code """""""""""""""""""""""""""""""""""""""""" */
namespace Engine
{
class Module
  {
    char *name;


    public: int Begin();
  }:
}





当用户用C编写游戏时,主要来源是< br $>




When the user is writing the game in C, the main source would be

struct Application
  {
    Engine_Module base;
  };

typedef struct Application Application;


int Application_Begin(Application *this)
  {
    printf("Hello World\n");
  }


int main(int argc, char *argv[])
  {
    Application application;
    return Application_Begin(&application);
  }





当用户用C ++编写游戏时,主要来源是





When the user is writing the game in C++, the main source would be

class Application : public Engine::Module
  {
    public: int Begin()
      {
        printf("Hello World\n");
      }
  };


int main(int argc, char *argv[])
  {
    Application application;
    return application.Begin();
  };







在使用各自的文件类型进行编译时,两者都能正常工作,但如果我尝试使用C ++中的Engine_Module结构我得到一个歧义错误,说有两个Engine_Module结构的定义。我假设这是因为GCC的C ++类以某种方式派生于结构。这不是一个主要问题(如果有的话,它甚至有助于强制执行我正在编写的引擎的用户只使用C文件中的C和C ++文件中的C ++),但我想知道是否有替代方案除此之外分支到C函数。



有什么想法吗?




Both work fine when compiled with their respective file types, but if I attempt to use the Engine_Module struct from C++ I get an ambiguity error saying there are 2 definitions for the Engine_Module struct. I'm assuming this is because GCC's C++ classes derive from structs in some way. It's not a major issue (if anything, it even helps to enforce users of the engine I'm writing to only use C in C files and C++ in C++ files), but I'm wondering if there is a alternative to this aside from branching to a C function.

Any ideas?

推荐答案

typedef struct Module Module;
typedef struct Application Application;



这完全是胡说八道(实际上可能导致警告或错误)。 C ++(以及AFAIK ANSI C)已经允许你使用struct name而不必在它之前加上struct限定符!



在旁注上:使用self-定义的类型名称与先前定义的类型的名称相同至少令人困惑,AFAIK也是一个错误:每当conpiler找到名称Application时,它将不知道您是否表示您的typedef名称或者这个名字的结构!您可以合法地在同一范围内定义两个相同名称的变量(最后定义的变量将隐藏对第一个变量的访问),但您无法定义同名的类型。这可能是你问题的原因。



That's complete nonsense (and may in fact lead to warnings or errors). C++ (and AFAIK ANSI C as well) already let you use the struct name without having to precede it with the struct qualifier!

On a sidenote: using a self-defined type name that is identical to the name of a previously defined type is at the very least confusing, and AFAIK an error, too: whenever the conpiler finds the name Application it won't know whether you mean your typedef'd name or the struct by that name! You can define two variables of the same name within the same scope legally (to the effect that the variable defined last will hide access to the first), but you can not define types of the same name. This may in fact be the cause of your problem.

extern int Engine_Module_Begin(Engine_Module *this);



另一段废话:这个在C中没有任何意义,但可能会让那些了解C ++的人感到困惑。事实上,如果你试图用C ++编译它,你的编译器可能会抱怨,因为这个是一个保留的关键字!



至于你的问题,你所展示的代码甚至不使用 Engine_Module (它使用 Engine :: Module 代替),所以问题必须出在代码的其他部分。



更一般地说,我建议你放弃双C ++ / C程序结构的想法。如果你想让你的引擎可以被C和C ++程序员访问,那么你应该用C ++编写它,然后 - 当它完成时 - 提供隐藏C ++特定接口的C函数包装器。总是试图让你的API同时适应这两种情况,你就会不必要地妨碍你的开发,而这根本不会起作用!





PS:

检查引用后我发现ANSI C中的结构仍然需要使用struct关键字,所以仍然需要某种typedef。但是,在C ++中没有必要,并且将结构名称重用为typedef名称实际上可能是错误的原因。因此,如果为C和C ++编译相同的代码,则需要确保为C ++编译排除那些typedef!


Another piece of nonsense: this has no meaning in C, but may lead to confusion for those who know C++. In fact, if you try to compile that in C++ your compiler will likely complain, because this is a reserved keyword!

As for your problem, the code you've shown doesn't even use Engine_Module (it uses Engine::Module instead), so the problem must be in other parts of your code.

More generally, I advise you to drop the dual C++/C program structure idea. If you want to make your engine accessible to both C and C++ programmers then you should just write it in C++ and then - when it's done - provide C function wrappers that hide the C++-specific interface. You're needlessly hamstringing your development by always trying to fit your API to both at once, and that simply will not work!


P.S.:
After checking the references I've found that structs in ANSI C still require the struct keyword to be used, so some sort of typedef is still necessary. However, in C++ it is not necessary, and reusing the struct name as typedef name may in fact be the casue of errors. So if the same code is compiled for both C and C++ you need to make sure those typedefs are excluded for C++ compilation!


这篇关于[GCC] C / C ++ Struct Ambiguity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆