Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Arduino,将jsonBuffer.parseObject的返回传递给函数_C++_Json_Arduino - Fatal编程技术网

C++ Arduino,将jsonBuffer.parseObject的返回传递给函数

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

我举这个例子:

我想将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 '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"];
}