invalid use of non-static data member

Hey there,
So far I've found out that one can't initialize a non-static data member on declaration in a class but i need to do that because i should use that data member as the default argument of one of the function members and change it later on execution ( replace it with user input) . do u know anyway around that error ?

Here's the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  class Permute {
	std::vector<std::string> permutations;
	std::string theString = ""; // Here : error: invalid use of non-static data member
public:
	Permute(){}
	Permute(std::string str){}

	void setString(void); // sets theString with user input
	std::string getString(); // returns the theString
	bool validString(std::string str); // validates user input
        // I need to use it here as the default argument for str
	void permute(std::string const_str = "",std::string str = theString );
	void printPermutations();
};


Thanks in advance.
Empty string is already a default state of any std::string.
It would be initialized automatically.
I tried not initializing it but i got the same error :
invalid use of non-static data member

that's because the default argument should be a known value at compile time, and since my string variable is not initialized, i keep getting that error; this is way I'm trying to initialize it.
Oh, I did not noticed how you use it. It has nothing to do with its value known at compile time or something. You just cannot do that. It is not allowed. Make second overload of your permute function:
1
2
3
4
5
void permute(std::string const_str, std::string str);
void permute(std::string const_str)
{
    permute(std::move(const_str), theString)
}


Edit:
Standard wrote:
8.3.6 Default arguments [dcl.fct.default]
9. [...] Similarly, a non-static member shall not be used in a default argument, even if it is not evaluated
Last edited on
It worked !!
Thanks :)
Topic archived. No new replies allowed.