0

If I have two files myclass.h and myclass.cpp how to separate this code to declaration of class member and implementation of constructor where I need to set the values in brackets {}?

std::string predefinedArgs[] = { "Some", "Other", "Strings" };

I tried header:

class Wrapper {
    public:
        std::map<std::string,std::string> arguments;
        Wrapper(int argc, char ** argv);
    private:
        int argc; char ** argv;
        std::vector<std::string> args;
        std::string predefinedArgs[12];
        void parseArguments();
};

And .cpp:

Wrapper::Wrapper(int argc, char ** argv):
  argc(argc),
  argv(argv),
  args(argv, argv+argc)
  {
      this->predefinedArgs[] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", 
    "Aug", "Sep", "Oct", "Nov", "Dec" };*/
     // this->parseArguments();
  };

which is wrapper.cpp(8): error C2059: syntax error : ']'

I searched net but I cannot find one serious example showing this. Don't you use classes? (just a rhetorical question)

Edit: Init. list also failed:

#include "wrapper.h"

Wrapper::Wrapper(int argc, char ** argv):
  argc(argc),
  argv(argv),
  args(argv, argv+argc),
  predefinedArgs({ "Jan", "Feb", "Mar", "April", "May", "June", "July", 
    "Aug", "Sep", "Oct", "Nov", "Dec" })
  {
      // this->parseArguments();
  };

wrapper.cpp(7): error C2143: syntax error : missing ')' before '{'

John Boe
  • 3,501
  • 10
  • 37
  • 71

1 Answers1

2

From the name of predefinedArgs I guess you might consider using static const member (or other solutions) and put it outside of the constructor completely.

E. g. like this:

.hpp

class Wrapper {
    // ...
    private:
        static const std::string predefinedArgs[12];
};

.cpp

const std::string Wrapper::predefinedArgs[12] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
Jan Korous
  • 666
  • 4
  • 11
  • Thank you very much. One more question. Why it is not possible to place it in implementation of constructor? I planned to add it there and then run parsing method to parse arguments from the constructor. This way I will need to add one more command to main function just after copy constructor of the class. But I am glad this works. *Edit: or not.. I can place the line before the implementation of constructor.* – John Boe Jun 10 '16 at 16:40
  • 1
    @JohnBoe Assignment during definition is just a way how to express initialization and rules are different for initialization (e. g. you can initialize array values). In body of the constructor `predefinedArgs` is already constructed and assignment is just an assignment not an initialization. – Jan Korous Jun 10 '16 at 16:47
  • @JohnBoe _"Why it is not possible to place it in implementation of constructor?"_ Because VS2010 is too old, and doesn't support the current standard. – πάντα ῥεῖ Jun 10 '16 at 16:55
  • @Jan Korous: pls could you yet tell me how to create pointer to the type? Something like this fails: `const std::string[12] * pArgs = predefinedArgs;` and this `const std::string[] * pArgs = predefinedArgs;` or this `std::string * pArgs = predefinedArgs;` also doesn't work. – John Boe Jun 10 '16 at 20:18
  • @JohnBoe Not sure I understand you. Are you aware of the fact that in C and C++ build-in array *is* a pointer? Do you really need pointer to that array (i. e. pointer to pointer)? Isn't the array sufficient? Maybe try to look at this Answer http://stackoverflow.com/a/14309142/1052689 – Jan Korous Jun 10 '16 at 20:24
  • @Jan Korous: Then I should copy the pointer to array because when I need to go through the array I need to increase the pointer myarray++; And because I lose the information about the begin, I need to copy the begin index before I start to increase the pointer. So It is not clear to me how to declare it. Something like this fails too `const std::string[12] pArgs = this->predefinedArgs;` Anyway ... `this->predefinedArgs++;` error C2105: '++' needs l-value ... I think I need to remove the const modifier. – John Boe Jun 10 '16 at 20:49
  • 1
    @JohnBoe If you need to iterate over `const std::string Wrapper::predefinedArgs[12]` just use `const std::string* arg_ptr = predefinedArgs; for(int i=0; i<12; ++i) { do_whatever_you_like( *(arg_ptr+i) ); }`. But I would really recommend reading some introductory book or text as C++ is not easy for newcomers and you'll get stuck probably pretty soon again. What about checking some of those? http://stackoverflow.com/a/388282/1052689 – Jan Korous Jun 10 '16 at 21:07