我的代码有几个神奇的数字,比如"外部"的特定错误代码,我只需要一次.所以我可以编码
do_something(x, y, 161)
Run Code Online (Sandbox Code Playgroud)
要么
magic_code = 161
do_something(x, y, magic_code)
Run Code Online (Sandbox Code Playgroud)
第二个当然是更好的可读性,因为它将161的含义编码为"magic_code".因此代码变得更具可读性.但是它将语义链接置于语句之外.是否有一种易于输入的内联方式?理想情况下的方向
do_something(x, y, magic_code = 161)
do_something(x, y, other_magic = 162)
Run Code Online (Sandbox Code Playgroud)
这当然不起作用.
当然,我可以将常量收集到一些特定的枚举中.然而,常数"仅适用于一个呼叫".
一些答案建议将魔术常数收集到枚举中.我不想那样做.问题是do_something调用了一些外部系统,我通过逆向工程收集常量.一旦我完成所有呼叫,我可能会决定这样做.但是在这个时候,不这样做会更方便.所以我想要一种方法来加快我的打字速度,同时仍然保持一定的可读性.
到目前为止我发现的最接近的是
do_something(x, y, 161 if "magic" else nil)
do_something(x, y, {"magic":161}["magic"])
def inline_constant(name, value): return value
do_something(x, y, inline_constant("magic", 161))
do_something(x, y, inline_constant("other_magic", 162))
Run Code Online (Sandbox Code Playgroud)
它使语义接近数字,但它看起来很尴尬.
我不想将模块级别设置为常量.我只希望一次命名为常数.到目前为止我发现的最接近的是
do something(x, y, "magic" and 161)
do something(x, y, "other magic" and 162)
Run Code Online (Sandbox Code Playgroud) 我的背景不在C(它在Real Studio中 - 类似于VB)而且我真的很难分割逗号分隔的字符串,因为我不习惯低级字符串处理.
我正在通过串口向Arduino发送字符串.这些字符串是特定格式的命令.例如:
@20,2000,5!
@10,423,0!
Run Code Online (Sandbox Code Playgroud)
'@'是标题,表示新命令和'!' 是标记命令结束的终止页脚.'@'之后的第一个整数是命令id,其余的整数是数据(作为数据传递的整数数可以是0-10个整数).
我写了一个获取命令的草图(剥离了'@'和'!')并调用了一个函数,handleCommand()当有一个命令要处理时.问题是,我真的不知道如何拆分这个命令来处理它!
这是草图代码:
String command; // a string to hold the incoming command
boolean commandReceived = false; // whether the command has been received in full
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// main loop
handleCommand();
}
void serialEvent(){
while (Serial.available()) {
// all we do is construct the incoming command to be handled in the main loop
// …Run Code Online (Sandbox Code Playgroud) 我使用的代码如下:
const int NUMBER_OF_FIELDS = 3;
int fieldIndex = 0;
int values[NUMBER_OF_FIELDS];
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
char ch = Serial.read();
if(ch>='0' && ch <= '9')
{
values[fieldIndex] = (values[fieldIndex]*10 +(ch-'0'));
}
else if (ch == ',')
{
if(fieldIndex < NUMBER_OF_FIELDS -1)
fieldIndex++;
}
else
{
Serial.print(fieldIndex+1);
Serial.println("fields recieved:");
for (int i = 0; i<=fieldIndex; i++);
{
//Serial.println(values[i]);
//values[i]= 0;
}
fieldIndex = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到的错误是:
对于'范围','i'的名称查找已更改为新的ISO'
我不认为我在for循环中做错了什么,为什么我得到这个错误?
在Arduino IDE中,我收到有关如何声明我的构造函数两次的错误.
这是它的代码:
tond.h
#ifndef TOND_H
#define TOND_H
class Tondeuse {
public:
Tondeuse();
Tondeuse(int,int);
};
#endif
Run Code Online (Sandbox Code Playgroud)
tond.cpp
#ifndef TOND
#define TOND
#include "arduino.h"
#include "tond.h"
Tondeuse::Tondeuse()
{
}
Tondeuse::Tondeuse(int h, int w)
{
Serial.println("Hello");
}
#endif
Run Code Online (Sandbox Code Playgroud)
而错误:
Tondeuse.cpp.o: In function `Tondeuse':
/tond.cpp:11: multiple definition of `Tondeuse::Tondeuse()'
tond.cpp.o:C:\Users\DEPANNE\AppData\Local \Temp\build6942484698459603114.tmp/tond.cpp:11: first defined here
Tondeuse.cpp.o: In function `Tondeuse':
/tond.cpp:11: multiple definition of `Tondeuse::Tondeuse()'
tond.cpp.o:C:\Users\DEPANNE\AppData\Local \Temp\build6942484698459603114.tmp/tond.cpp:11: first defined here
Tondeuse.cpp.o: In function `Tondeuse':
/tond.cpp:16: multiple definition of `Tondeuse::Tondeuse(int, int)'
tond.cpp.o:C:\Users\DEPANNE\AppData\Local\Temp\build6942484698459603114.tmp/tond.cpp:16: first defined here
Tondeuse.cpp.o: …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的结构
namespace Binning_ {
template <typename data_type, uint32_t number_of_bins, bool uses_integrals>
struct Binner {
void setup();
/* ... */
Run Code Online (Sandbox Code Playgroud)
现在我想实现它.当然,我希望我必须以某种方式实现它
namespace Binning_ {
template <typename data_type, uint32_t number_of_bins, bool uses_integrals>
void Binner<typename data_type, uint32_t number_of_bins, bool uses_integrals>::setup() {
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
编译器不断告诉我模板参数的错误数量.
/home/udo/dev/libraries/dcf77/dcf77.cpp:305:81: error: wrong number of template arguments (1, should be 3)
void Binner<typename data_type, uint32_t number_of_bins, bool uses_integrals>::setup() {
^
/home/udo/dev/libraries/dcf77/dcf77.cpp:259:12: error: provided for 'template<class data_type, long unsigned int number_of_bins, bool uses_integrals> struct Binning_::Binner'
struct Binner { …Run Code Online (Sandbox Code Playgroud)