我见过的最奇怪的错误,又名)*

发布于 2024-08-17 05:55:08 字数 2112 浏览 3 评论 0原文

我正在用 C 语言编写这个很棒的应用程序,至少我认为它很棒,并且与 GObject 完美融合,过了一会儿我开始遇到这个非常非常奇怪的错误。我也相信已经注意到它并不总是出现。然而,这可能只是 IDE 的错误。无论如何...

GCC 显然抱怨:在“*”标记之前预期有“)”;这发生在头文件中。

这是完全相同的头文件。

#pragma once
#include "global.h"

#include "CharcoalApp.h"

GtkListStore *WebsitesListStore;

// that error is reported for this line
void charcoal_websites_list_initialize(CharcoalApp *app); 

据我所知,这来自该函数的 CharcoalApp *app 参数。

因为我无法真正理解为什么会发生此错误,所以我将包含 CharcoalApp.h 文件。 global.h是一个非常简单的头文件,携带了主要的依赖项,主要是GLib、GObject、GThread、GTK+、WebKit等。

CharcoalApp.h


#ifndef __CHARCOAL_APP_H__
#define __CHARCOAL_APP_H__

#include "global.h"
#include "CharcoalDB.h"

#include "CharcoalWindow.h"
#include "CharcoalWebsitesList.h"

G_BEGIN_DECLS

#define CHARCOAL_TYPE_APP             (charcoal_app_get_type())
#define CHARCOAL_APP(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), CHARCOAL_TYPE_APP, CharcoalApp))
#define CHARCOAL_APP_CONST(obj)       (G_TYPE_CHECK_INSTANCE_CAST((obj), CHARCOAL_TYPE_APP, CharcoalApp const))
#define CHARCOAL_APP_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass), CHARCOAL_TYPE_APP, CharcoalAppClass))
#define CHARCOAL_IS_APP(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), CHARCOAL_TYPE_APP))
#define CHARCOAL_IS_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), CHARCOAL_TYPE_APP))
#define CHARCOAL_APP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), CHARCOAL_TYPE_APP, CharcoalAppClass))

typedef struct _CharcoalApp         CharcoalApp;
typedef struct _CharcoalAppClass    CharcoalAppClass;
typedef struct _CharcoalAppPrivate  CharcoalAppPrivate;

struct _CharcoalApp {
  GObject parent;

  CharcoalAppPrivate *priv;

  GtkBuilder *ui;
  CharcoalDB *db;

  // toplevels
  GtkWidget *CharcoalWindow;
};

struct _CharcoalAppClass {
  GObjectClass parent_class;
};

GType charcoal_app_get_type(void) G_GNUC_CONST;
CharcoalApp *charcoal_app_new(void);
void charcoal_app_quit(CharcoalApp *app);

G_END_DECLS

#endif /* __CHARCOAL_APP_H__ */

感谢您的帮助!

I'm writing this awesome application, at least I think it awesome, in C with the magnificent blend of GObject and after a while I start getting this very, extremely strange error. I also believe to have noticed it not appearing always. However this might just be the IDE's fault. Anyhow...

GCC, apparently, complains: expected ')' before '*' token; this happens in a header file.

This is that very same header file.

#pragma once
#include "global.h"

#include "CharcoalApp.h"

GtkListStore *WebsitesListStore;

// that error is reported for this line
void charcoal_websites_list_initialize(CharcoalApp *app); 

As far as I can see, this comes from the CharcoalApp *app parameter for that function.

Because I cannot really understand why this error is happening, I'll include the CharcoalApp.h file. global.h is a very simple header file that carries the main dependencies, mainly GLib, GObject, GThread, GTK+, WebKit and other.

CharcoalApp.h


#ifndef __CHARCOAL_APP_H__
#define __CHARCOAL_APP_H__

#include "global.h"
#include "CharcoalDB.h"

#include "CharcoalWindow.h"
#include "CharcoalWebsitesList.h"

G_BEGIN_DECLS

#define CHARCOAL_TYPE_APP             (charcoal_app_get_type())
#define CHARCOAL_APP(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), CHARCOAL_TYPE_APP, CharcoalApp))
#define CHARCOAL_APP_CONST(obj)       (G_TYPE_CHECK_INSTANCE_CAST((obj), CHARCOAL_TYPE_APP, CharcoalApp const))
#define CHARCOAL_APP_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass), CHARCOAL_TYPE_APP, CharcoalAppClass))
#define CHARCOAL_IS_APP(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), CHARCOAL_TYPE_APP))
#define CHARCOAL_IS_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), CHARCOAL_TYPE_APP))
#define CHARCOAL_APP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), CHARCOAL_TYPE_APP, CharcoalAppClass))

typedef struct _CharcoalApp         CharcoalApp;
typedef struct _CharcoalAppClass    CharcoalAppClass;
typedef struct _CharcoalAppPrivate  CharcoalAppPrivate;

struct _CharcoalApp {
  GObject parent;

  CharcoalAppPrivate *priv;

  GtkBuilder *ui;
  CharcoalDB *db;

  // toplevels
  GtkWidget *CharcoalWindow;
};

struct _CharcoalAppClass {
  GObjectClass parent_class;
};

GType charcoal_app_get_type(void) G_GNUC_CONST;
CharcoalApp *charcoal_app_new(void);
void charcoal_app_quit(CharcoalApp *app);

G_END_DECLS

#endif /* __CHARCOAL_APP_H__ */

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧人九事 2024-08-24 05:55:08

CharcoalApp 未在 global.h 中声明。假设您在 C 文件中包含 CharcoalApp.h

  1. CharcoalApp.h 会被读取到 #include "global.h"
  2. global.h包含
  3. 错误:'*'标记之前预期')'

您应该正确重新排列头文件或使用前向声明(尽管我认为在这种情况下不需要)。


解决办法有很多:

  1. 正确的做法是:从global.h中包含CharcoalApp.h,而不是相反;
  2. typedef struct _CharcoalApp CharcoalApp; 移至 CharcoalApp.h 中的 `#include "global.h" 之前;
  3. CharcoalApp.h 中删除 #include "global.h" 并按正确的顺序包含它们(最后是 global.h)。

CharcoalApp is not declared in global.h. Suppose you are including CharcoalApp.h in your C file:

  1. CharcoalApp.h is read up to #include "global.h"
  2. global.h is included
  3. Error: expected ')' before '*' token

You should rearrange your header files properly or use a forward declaration (although I don't think is needed in this case).


There are many solutions:

  1. the proper way: include CharcoalApp.h from global.h, not the reverse;
  2. move typedef struct _CharcoalApp CharcoalApp; before `#include "global.h" in CharcoalApp.h;
  3. remove #include "global.h" from CharcoalApp.h and include them in the proper order (global.h last).
可是我不能没有你 2024-08-24 05:55:08

我不确定这对每个人来说是否都是显而易见的,但我应该补充一点,它抱怨的原因(我认为)是因为它在解析该代码时不明白 CharcoalApp 是一个类型名称(我见过过去多次出现类似的编译时错误)。我认为它将它作为参数名称而不是参数的类型名称来处理,因此它期望参数列表的结尾(或逗号)而不是另一个不带逗号的参数名称。

I'm not sure if it's obvious to everyone, but I should add that the reason it's complaining (I think) is because it doesn't understand that CharcoalApp is a type name at the time that it's parsing that code (I've seen similar compile-time errors many times in the past). I think it is handling it as a parameter name instead of a parameter's type name, so it expects the end of the argument list (or a comma) instead of another parameter name without a comma.

蓝颜夕 2024-08-24 05:55:08

怎么样:

#pragma once
#include "global.h"

//#include "CharcoalApp.h"
struct CharcoalApp;

GtkListStore *WebsitesListStore;

// that error is reported for this line
void charcoal_websites_list_initialize(CharcoalApp *app); 

它并不完全漂亮,但是删除其他标头中包含的标头并用前向声明替换它们是我曾经被教导过并且总是尝试做的事情。

它显着减少了我花在编译器上的循环依赖上的时间,并且在更改通常包含的头文件时还可以缩短构建时间。

:D

How about:

#pragma once
#include "global.h"

//#include "CharcoalApp.h"
struct CharcoalApp;

GtkListStore *WebsitesListStore;

// that error is reported for this line
void charcoal_websites_list_initialize(CharcoalApp *app); 

Its not exactly pretty, but removing header includes inside other headers and replacing them with forward declarations is something I was taught once and always try to do.

It reduces the amount of time I spend swearing at the compiler over cyclic dependancy significantly, and it also keeps build times down when changing a commonly included header file.

: D

鲜肉鲜肉永远不皱 2024-08-24 05:55:08

在我看来,GtkListStore 没有定义。

Looks to me like GtkListStore is not defined.

中性美 2024-08-24 05:55:08

尝试将代码更改为:

struct _CharcoalApp { 
  GObject parent; 

  CharcoalAppPrivate *priv; 

  GtkBuilder *ui; 
  CharcoalDB *db; 

  // toplevels 
  GtkWidget *CharcoalWindow; 
}; 

struct _CharcoalAppClass { 
  GObjectClass parent_class; 
};

typedef struct _CharcoalApp         CharcoalApp;
typedef struct _CharcoalAppClass    CharcoalAppClass; 
typedef struct _CharcoalAppPrivate  CharcoalAppPrivate; 

当您说出 typedef 时,底层结构类型尚未定义,这可能会混淆一些内容。

我通常只是写这样的东西:

typedef struct { 
  GObject parent; 

  CharcoalAppPrivate *priv; 

  GtkBuilder *ui; 
  CharcoalDB *db; 

  // toplevels 
  GtkWidget *CharcoalWindow; 
} CharcoalApp; 

Try changing your code to:

struct _CharcoalApp { 
  GObject parent; 

  CharcoalAppPrivate *priv; 

  GtkBuilder *ui; 
  CharcoalDB *db; 

  // toplevels 
  GtkWidget *CharcoalWindow; 
}; 

struct _CharcoalAppClass { 
  GObjectClass parent_class; 
};

typedef struct _CharcoalApp         CharcoalApp;
typedef struct _CharcoalAppClass    CharcoalAppClass; 
typedef struct _CharcoalAppPrivate  CharcoalAppPrivate; 

When you uttered the typedef, the underlying structure type wasn't defined yet, and this MIGHT have confused something.

I normally just write something like:

typedef struct { 
  GObject parent; 

  CharcoalAppPrivate *priv; 

  GtkBuilder *ui; 
  CharcoalDB *db; 

  // toplevels 
  GtkWidget *CharcoalWindow; 
} CharcoalApp; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文