0

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:
3
  • 3
    don't use arrays, use std::vector or similar in c++ Commented Oct 31, 2015 at 12:27
  • Also, why are you passing the whole array, when setMode() only needs a single string parameter? Commented Oct 31, 2015 at 12:29
  • 1
    The method is not yet implemented, it will used the whole array in the future. Commented Oct 31, 2015 at 12:30

2 Answers 2

5
string mode[argc-1];

This uses a proprietary GCC extension. In standard C++, a raw array's size must be known at compile time.

You need something like this instead:

if (argc < 4) {
    std::cerr << "error\n";
    return EXIT_FAILURE;
}
string mode[4];

Or, what would be very much preferable, use std::vector:

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;
}

This will leave mode[0] empty.

for(int i=0; i<4; i++){
        LEDs[i].setMode(mode);
}

The array (or std::vector) passed to setMode will thus always be such that the first element is an empty string.

void LED::setMode(string mode[]){

This is an attempt to pass an array to a function. What happens really is that a pointer to the first element is passed and the size information is lost.

The correct way of passing a raw array including its size information would be to use a reference:

 void LED::setMode(string (&mode)[4])

But as I mentioned previously, just use a std::vector and you'll be fine. When you need to modify the vector, pass it via &, else via const&:

 void LED::setMode(std::vector<std::string> const& mode)

In either case, inside of the method, you currenly just access the first element:

cout << "setMode: mode: " << mode[0].c_str() << endl;

As we've established before, mode[0] is always empty. That's why nothing is printed.

Sign up to request clarification or add additional context in comments.

Comments

2

You are off by one. You are writing to

mode[1]

in

for(int i=1; i<argc; i++){
    mode[i] = string(argv[i]);

and you use

mode[0] 

for output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.