C++ Arduino,将jsonBuffer.parseObject的返回传递给函数
我举这个例子: 我想将root传递到函数中,并在函数中使用它C++ Arduino,将jsonBuffer.parseObject的返回传递给函数,c++,json,arduino,C++,Json,Arduino,我举这个例子: 我想将root传递到函数中,并在函数中使用它 //Call it test(root); // Define here void test(JsonObject* root) { int flag = (*root)["success"]; // Not sure how to do it } 编译错误: error: could not convert '& root' from 'ArduinoJson::JsonObject*' to 'ArduinoJs
//Call it
test(root);
// Define here
void test(JsonObject* root) {
int flag = (*root)["success"]; // Not sure how to do it
}
编译错误:
error: could not convert '& root' from 'ArduinoJson::JsonObject*' to 'ArduinoJson::JsonObject'
很抱歉,我对指针的理解很差。由于您的函数正在获取指针,您需要先获取
根目录的地址,然后才能传递它。这是使用&
(addressof)运算符完成的
JsonObject& root = jsonBuffer.parseObject(json);
test( &root ); //Get address of root, then pass that pointer to function.
void test(JsonObject* root) { //Pass pointer by value
int flag = (*root)["success"];
}
但是,您应该使用引用,这样您的代码就会简单一些,并且看起来更干净(没有addressof和未引用的指针)
由于函数使用指针,因此在传递它之前,需要获取根目录的地址。这是使用&
(addressof)运算符完成的
JsonObject& root = jsonBuffer.parseObject(json);
test( &root ); //Get address of root, then pass that pointer to function.
void test(JsonObject* root) { //Pass pointer by value
int flag = (*root)["success"];
}
但是,您应该使用引用,这样您的代码就会简单一些,并且看起来更干净(没有addressof和未引用的指针)
JsonObject& root = jsonBuffer.parseObject(json);
test( root );
void test(JsonObject &root) { // Pass by reference.
int flag = root["success"];
}