-1

Unable to access argv value in parseCmdline function in myarm.cpp which is initialized in the constructor. Don't know how to pass/initialize the value from main(int argc, char *argv[ ]) in constructor?

main.cpp

#include "myarm.h"    
int main(int argc, char *argv[]){
    cout<<"argv is:"<<argv[2]<<endl;
    MyArm arm;

myarm.cpp

#include "myarm.h"

MyArm::MyArm(){
    nj = 0;
    done = false;
    times = 0;
    count = 50;
    which_joint = 0;
    cout<<"argv is:"<<argv[2]<<endl;
    parseCmdline();
}

int MyArm::parseCmdline() {
    params.fromCommand(argc, argv);
    if (!params.check("robot")){
        cout<<"Please specify name of the robot"<<endl;
        cout<<"--robot name (e.g. TechNit)"<<endl;
        return 1;
    }

    else if(!params.check("element")){
        cout<<"Please specify name of the element"<<endl;
        cout<<"--element name (e.g. left_arm)"<<endl;
        return 1;
    }
}
  • 2
    As an argument, like in `main` too? – hlt Nov 29 '15 at 19:44
  • Typically you'd want to handle the params outside of the object. – erip Nov 29 '15 at 19:47
  • 2
    Your code is much too complicated. You are trying to pass a command-line argument to a constructor. Do you know how to pass a command-line argument to a function? Do you know how to pass an argument to a constructor? – Beta Nov 29 '15 at 19:49

2 Answers2

0

Passing the parameters from main to other functions is easy, just declare the other functions like main:

void my_method(int argument_count, char * * arg_list);

int main(int argument_count, char * * arg_list)
{
  my_method(argument_count, arg_list);
  return EXIT_SUCCESS;
}

void my_method(int argument_count, char * * arg_list)
{
  std::cout << "Argument Count: " << argument_count << "\n";
  for (int i = 0; i < argument_count; ++i)
  {
    std::cout << "Argument [" << i << "]: " << arg_list[i] << "\n";
  }
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • i got this but the problem is that i am unable to use argv in the line params.fromCommand(argc, argv); which belongs to a header file. – Nabeeha Deedar Nov 29 '15 at 20:11
0

Add parameters to the constructor:

MyArm::MyArm(int argc, char *argv[])

then instantiate as

MyArm arm(argc, argv);

Edit

but it turns out these parameters need to be used by

int MyArm::parseCmdline()

so parseCmdline also needs to be redefined

int MyArm::parseCmdline(int argc, char *argv[])

and called with

parseCmdline(argc, argv);

See the pattern? If not, I recommend giving the text book a deeper read or getting a better text book.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Got it, but now getting error: ‘argv’ was not declared in this scope which is in myarm.cpp in the above code. How to pass value of argv to this function? params.fromCommand(argc, argv); – Nabeeha Deedar Nov 29 '15 at 20:01
  • 1
    @Nabeeha Deedar: You can't use arc or argv in your constructor MyArm::MyArm() because you didn't pass them to it. Pass them during instantiation and if you need them for other member functions, make them members – Bob__ Nov 29 '15 at 20:48