Problem with object instantiation in class definition

Hi all, I'm writing a command line/config file parser for a project I'm working on and I'm having trouble.

There are 3 classes involved,
The Option class whose constructor takes a token definition and a docstring (eg Option configFile("-v" "Run with verbose mode")).
You would optionally then tell the class what options the token should expect, if any.

The Parser class which is a virtual class with the logic necessary to use Options registered in a std::map to parse config files or command line arguments.

The Parser subclass which simply defines and registers Options necessary for the current program. Here are the relevant sections of code:

Option Constructors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Option::Option(std::string& op, std::string& doc, bool isNecessary)
{
  commandOption = op;
  docString = doc;

  isOptionNecessary = isNecessary;
  optionValue = NULL;
}

Option::Option(char* op, char* doc, bool isNecessary)
{
  commandOption.assign(op);
  docString.assign(doc);

  isOptionNecessary = isNecessary;
  optionValue = NULL;
}


Subclass definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <cstdlib>
#include <string>

#include "CommandParser.h"
#include "Option.h"

using namespace std;

class CommandParserTestImplementation : public CommandParser
{
 private:
  Option configFile("-f", "Config file path");
  Option verbosity("-v", "Verbosity level");
  Option recursive("-r", "Run recursively");

snip(.....)


So, I declare a number of relevant options, and the constructor will then register them in the map. But when I compile this I get errors on all for Option instantiations.

1
2
3
4
5
6
CommandParserTest.cpp:14: error: expected identifier before string constant
CommandParserTest.cpp:14: error: expected ',' or '...' before string constant
CommandParserTest.cpp:15: error: expected identifier before string constant
CommandParserTest.cpp:15: error: expected ',' or '...' before string constant
CommandParserTest.cpp:16: error: expected identifier before string constant
CommandParserTest.cpp:16: error: expected ',' or '...' before string constant


I'm stumped, at first I thought the compiler was seeing the string literal construction as ambiguous with the string and char* constructors, but I removed each of them leaving the other and the error messages were still the same. If I declare Option* and instantiate them in the constructor via xxxx = new Option("-f", "config file"); it works and just gives me errors about the deprecated conversion from string literals to char*. Does anyone know why the above doesn't work?
Thanks in advance.

<edit>
Forgot to mention, none of my classes are declared in a custom namespace.
Last edited on
That's not how you construct objects.
1
2
3
4
5
6
CommandParserTestImplementation::CommandParserTestImplementation(/*...*/)
		:configFile("-f", "Config file path"),
		verbosity("-v", "Verbosity level"),
		recursive("-r", "Run recursively"){
	//...
}
Thanks helios, I am apparently an idiot :p To much C programming lately.
Topic archived. No new replies allowed.