I am a beginner to C++, I have used C before but never used C++. This is one of my first programs, and it's supposed to do something really simple, however I'm unable to even pass strings between methods... When I call the method setMode with a string array, the method instance recieves an empty array, and not the one I've sent. Why is this happening?
#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;
}
Output:
debian@beaglebone:~/Desktop/LED_Cpp$ ./led on 1
sys/class/leds/beaglebone:green:usr0
sys/class/leds/beaglebone:green:usr1
sys/class/leds/beaglebone:green:usr2
sys/class/leds/beaglebone:green:usr3
on
1
setMode: mode:
setMode: mode:
setMode: mode:
setMode: mode:
std::vectoror similar in c++setMode()only needs a singlestringparameter?