Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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++初学者,以前用过C,但从来没有用过C++。这是我的第一个程序之一,它应该做一些非常简单的事情,但是我甚至不能在方法之间传递字符串。。。当我使用字符串数组调用方法setMode时,方法实例接收到一个空数组,而不是我发送的数组。为什么会这样 #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; #define LED_PATH "sys/class/leds/beaglebone:green:usr" class LED{ private: string path; int number; public: LED(int number); virtual void setMode(string mode[]); virtual ~LED(); }; LED::LED(int number){ ostringstream fs; this->number = number; fs << LED_PATH << number; this->path = string(fs.str()); cout << this->path << endl; } LED::~LED(){ } void LED::setMode(string mode[]){ //Will use all fields of mode[] in the future cout << "setMode: mode: " << mode[0].c_str() << endl; } int main(int argc, char* argv[]){ LED LEDs[4] = {LED(0), LED(1), LED(2), LED(3)}; string mode[argc-1]; //TODO Perform argc value safety check for(int i=1; i<argc; i++){ mode[i] = string(argv[i]); cout << mode[i].c_str()<<endl; } for(int i=0; i<4; i++){ LEDs[i].setMode(mode); } return 0; }_C++ - Fatal编程技术网

将字符串数组传递给方法 我是C++初学者,以前用过C,但从来没有用过C++。这是我的第一个程序之一,它应该做一些非常简单的事情,但是我甚至不能在方法之间传递字符串。。。当我使用字符串数组调用方法setMode时,方法实例接收到一个空数组,而不是我发送的数组。为什么会这样 #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; #define LED_PATH "sys/class/leds/beaglebone:green:usr" class LED{ private: string path; int number; public: LED(int number); virtual void setMode(string mode[]); virtual ~LED(); }; LED::LED(int number){ ostringstream fs; this->number = number; fs << LED_PATH << number; this->path = string(fs.str()); cout << this->path << endl; } LED::~LED(){ } void LED::setMode(string mode[]){ //Will use all fields of mode[] in the future cout << "setMode: mode: " << mode[0].c_str() << endl; } int main(int argc, char* argv[]){ LED LEDs[4] = {LED(0), LED(1), LED(2), LED(3)}; string mode[argc-1]; //TODO Perform argc value safety check for(int i=1; i<argc; i++){ mode[i] = string(argv[i]); cout << mode[i].c_str()<<endl; } for(int i=0; i<4; i++){ LEDs[i].setMode(mode); } return 0; }

将字符串数组传递给方法 我是C++初学者,以前用过C,但从来没有用过C++。这是我的第一个程序之一,它应该做一些非常简单的事情,但是我甚至不能在方法之间传递字符串。。。当我使用字符串数组调用方法setMode时,方法实例接收到一个空数组,而不是我发送的数组。为什么会这样 #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; #define LED_PATH "sys/class/leds/beaglebone:green:usr" class LED{ private: string path; int number; public: LED(int number); virtual void setMode(string mode[]); virtual ~LED(); }; LED::LED(int number){ ostringstream fs; this->number = number; fs << LED_PATH << number; this->path = string(fs.str()); cout << this->path << endl; } LED::~LED(){ } void LED::setMode(string mode[]){ //Will use all fields of mode[] in the future cout << "setMode: mode: " << mode[0].c_str() << endl; } int main(int argc, char* argv[]){ LED LEDs[4] = {LED(0), LED(1), LED(2), LED(3)}; string mode[argc-1]; //TODO Perform argc value safety check for(int i=1; i<argc; i++){ mode[i] = string(argv[i]); cout << mode[i].c_str()<<endl; } for(int i=0; i<4; i++){ LEDs[i].setMode(mode); } return 0; },c++,C++,你差一点走。你在写信给 mode[1] 在 用于输出 这使用了一个专有的GCC扩展。在标准C++中,原始数组的大小必须在编译时知道。 你需要这样的东西: 因此,传递给setMode的数组(或std::vector)将始终确保第一个元素是空字符串 这是向函数传递数组的尝试。实际情况是,传递了指向第一个元素的指针,而大小信息丢失了 传递原始数组(包括其大小信息)的正确方法是使用引用: void LED::setMode(string (&mode)[4]) 但正如我前面提到的,只要使用

你差一点走。你在写信给

mode[1]

用于输出

这使用了一个专有的GCC扩展。在标准C++中,原始数组的大小必须在编译时知道。 你需要这样的东西:

因此,传递给
setMode
的数组(或
std::vector
)将始终确保第一个元素是空字符串

这是向函数传递数组的尝试。实际情况是,传递了指向第一个元素的指针,而大小信息丢失了

传递原始数组(包括其大小信息)的正确方法是使用引用:

 void LED::setMode(string (&mode)[4])
但正如我前面提到的,只要使用
std::vector
,您就可以了。需要修改向量时,通过
传递,否则通过
常量和
传递:

 void LED::setMode(std::vector<std::string> const& mode)
void发光二极管::设置模式(标准::矢量常量和模式)
在这两种情况下,在方法内部,您当前只需访问第一个元素:


不能使用数组,也可以使用c++中的
std::vector
或类似工具,为什么要传递整个数组,而
setMode()
只需要一个
string
参数?该方法尚未实现,将来将使用整个数组。
mode[0] 
string mode[argc-1];
if (argc < 4) {
    std::cerr << "error\n";
    return EXIT_FAILURE;
}
string mode[4];
std::vector<std::string> mode(argc-1);
for(int i=1; i<argc; i++){
    mode[i] = string(argv[i]);
    cout << mode[i].c_str()<<endl;
}
for(int i=0; i<4; i++){
        LEDs[i].setMode(mode);
}
void LED::setMode(string mode[]){
 void LED::setMode(string (&mode)[4])
 void LED::setMode(std::vector<std::string> const& mode)
cout << "setMode: mode: " << mode[0].c_str() << endl;