用于定义数字的数字常量之前的预期unqualified-id [英] Expected unqualified-id before numeric constant for defining a number

查看:100
本文介绍了用于定义数字的数字常量之前的预期unqualified-id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,所以我不知道在phidget代码示例中此错误的含义:

I'm new to C++, so I don't know what they mean with this error in a phidget-code example:


Main.cpp :8:16:错误:数值常量之前的预期unqualified-id

Main.cpp:8:16: error: expected unqualified-id before numeric constant


//verander de volgende informatie naar de informatie voor jouw database

#define dserver "oege.ie.hva.nl"
#define duser "username"
#define dpassword "password"
#define ddatabase "databasename"

#define homeid 1234 //line 8

是否存在语法错误?或者是其他东西?我使用#define而不是int。

Is there a syntax error? Or something else? I use #define instead of int.

编辑:添加了完整的错误日志。

added full error log..


完整的错误日志: http://pastebin.com/3vtbzmXD

完整的main.cpp代码: http://pastebin.com/SDTz8vni

Full main.cpp code: http://pastebin.com/SDTz8vni


推荐答案

完整错误是

error: expected unqualified-id before numeric constant
 note: in expansion of macro ‘homeid’
string homeid;
       ^

您要声明一个与宏同名的变量,但这是不可能的。预处理程序已经踩过程序,将其变成 string 1234; ,这不是有效的声明。预处理程序不了解程序结构,并且宏不遵循语言的范围规则。

You're trying to declare a variable with the same name as a macro, but that can't be done. The preprocessor has already stomped over the program, turning that into string 1234;, which is not a valid declaration. The preprocessor has no knowledge of the program structure, and macros don't follow the language's scope rules.

在可能的情况下,使用常量和内联函数之类的语言功能,而不要使用宏。在这种情况下,您可以使用

Where possible, use language features like constants and inline functions rather than macros. In this case, you might use

const int homeid = 1234;

这将在全局命名空间中作用域,并且可以安全地被名称相同的东西隐藏范围更窄。即使被隐藏,它也始终以 :: homeid 的形式提供。

This will be scoped in the global namespace, and can safely be hidden by something with the same name in a narrower scope. Even when hidden, it's always available as ::homeid.

当您确实需要宏时,明智的做法是请遵循对宏使用 SHOUTY_CAPS 的约定。除了提请注意与宏使用相关的潜在危险和怪异之外,它不会与使用其他大写字母的任何名称发生冲突。

When you really need a macro, it's wise to follow the convention of using SHOUTY_CAPS for macros. As well as drawing attention to the potential dangers and wierdnesses associated with macro use, it won't clash with any name using other capitalisation.

这篇关于用于定义数字的数字常量之前的预期unqualified-id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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