将字符串数组传递给方法 我是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;