C++ 数字常量前的预期 ',' 或 '...'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15652642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:35:59  来源:igfitidea点击:

expected ',' or '...' before numeric constant

c++arduino

提问by Patashu

I give up. Nor google answers me or the documentation. Whats wrong in here?

我放弃。谷歌也不回答我或文档。这里有什么问题吗?

" In file included from x.cpp:7: /Users/user/Dropbox/Projects/Arduino/libraries/Range/Range.h:27: error: expected identifier before numeric constant /Users/user/Dropbox/Projects/Arduino/libraries/Range/Range.h:27: error: expected ',' or '...' before numeric constant "

" 在 x.cpp:7 包含的文件中:/Users/user/Dropbox/Projects/Arduino/libraries/Range/Range.h:27: 错误:数字常量之前的预期标识符 /Users/user/Dropbox/Projects/Arduino/ libraries/Range/Range.h:27: 错误: 数字常量之前的预期 ',' 或 '...'

//
//  Range.h
//  Handles range to ground and altitude
//
//  Created by x on 2013-03-27.
//
//

#ifndef RANGE_H_
#define RANGE_H_

#include "NewPing.h"

#define BOTTOM_RF_PIN       5
#define BOTTOM_RF_ECHO_PIN  10
#define BOTTOM_RF_MAX       200


class Range {
public:
    Range();
    void init();
    float toGround();
    float toCeiling();
    float altitude();
private:
    NewPing bottomRF(BOTTOM_RF_PIN, BOTTOM_RF_ECHO_PIN, BOTTOM_RF_MAX);

};

#endif /* RANGE_H_ */

回答by Patashu

Instead of

代替

NewPing bottomRF(BOTTOM_RF_PIN, BOTTOM_RF_ECHO_PIN, BOTTOM_RF_MAX);

NewPing bottomRF(BOTTOM_RF_PIN, BOTTOM_RF_ECHO_PIN, BOTTOM_RF_MAX);

Try

尝试

NewPing bottomRF(int, int, int);

NewPing bottomRF(int, int, int);

Reason: You cannot declare a function to take literals. Only types. intis a type, 5 10 and 200 are literals.

原因:您不能声明一个函数来接受文字。只有类型。int是一种类型,5 10 和 200 是文字。

回答by Tushar

In case bottomRFis a data member, and not a function, and you are trying to instantiate it in the class declaration:

如果bottomRF是数据成员而不是函数,并且您试图在类声明中实例化它:

You cannot instantiate class-type items in the class declaration. A good place to do so is in the constructor initialization list.

您不能在类声明中实例化类类型项。这样做的好地方是在构造函数初始化列表中。

public:
    Range() :bottomRF(BOTTOM_RF_PIN, BOTTOM_RF_ECHO_PIN, BOTTOM_RF_MAX) {}

回答by Archer

Don't you need to specify the type of parameters? e.g.

不需要指定参数的类型吗?例如

NewPing bottomRF(int a1 = BOTTOM_RF_PIN, int a2= BOTTOM_RF_ECHO_PIN, int a3 = BOTTOM_RF_MAX);