【问题标题】:Expected declaration specifiers or '...' before '(' token?'(' 标记之前的预期声明说明符或'...'?
【发布时间】:2013-12-31 16:21:11
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

/// Global Variables
HANDLE ConsoleHandle;

int RGB (int R, int G, int B); // line 8
int Set_Color (int RGB_Fore, int RGB_Back);

int main (void)
{
  // Get Handle
  ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); 
  char Str [32] = "Happy New Year.\n";
  printf("%s", Str);
  system("pause>nul");
  return 0;
}

int RGB (int R, int G, int B) // line 21
{
  return (R*4 + G*2 + B);
}

int Set_Color (int RGB_Fore, int RGB_Back)
{
  SetConsoleTextAttribute(ConsoleHandle, RGB_Fore*8 + RGB_Back);
}

TDM-GCC 报告:

| line | Message
|  08  | error: expected declaration specifiers or '...' before '(' token
|  21  | error: expected declaration specifiers or '...' before '(' token

为什么?如何解决这个问题呢?谢谢

【问题讨论】:

  • 如果你只是打印一个常量字符串,为什么还需要%s? 8、21 号线在哪里?
  • 在黑暗中拍摄:我假设您包含的其中一个标题包含RGB 的定义。重命名函数,看看是否有帮助。
  • @Devolus 因为它是一个 sn-p?
  • 看起来 RGB 是一种试图查找更多信息的类型或宏。

标签: c gcc


【解决方案1】:

看起来RGB 是一个宏,如果你重命名函数,错误就会消失。此外,Set_Color 需要返回一个您定义的值以返回 int,但您在函数结束时没有返回任何内容。

如果您尝试使用 Set_Color 的值而没有根据 C99 草案标准部分 6.9.1 的明确返回 undefined behavior 函数定义 段落 12 em>:

如果到达终止函数的},并且函数调用的值被 调用者,行为未定义。

这在 C++ 中是 undefined,无论你是否尝试使用返回值。

【讨论】:

  • 在这种情况下我可以使用#undef吗?
  • @KevinDongNaiJia:一般最好不要打系统,所以重命名函数是最简单最好的。如果宏是一个类似函数的宏(如果是,它必须是一个带有 3 个参数的宏),那么您可以通过使用 int (RGB)(int R, int G, int B); 并类似地在每次引用(或定义) 它。这很快就会变老,因此“不要与系统作斗争”的建议。困难在于知道 Windows 标头中所有可能的名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 2011-09-04
  • 2014-06-08
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
相关资源
最近更新 更多