Error with this-> keyword

Hi Guys,

i'm facing a little issue with a new class i'm trying to write.

Here is the description for my class :

class TERMINALSYNC {
	public:
		TERMINALSYNC(void);
		int  runcommand(int);
		void showhelp(); //show all available commands
		void showtime();
		void showdate();
		void showfullconf();
		void sendata(char msg);


	private:
		const char* commandList[][91];


		int debug=true; //use to display debug info

};

And the corresponding cpp.

TERMINALSYNC::TERMINALSYNC()
{
	const char* this->commandList[][91] =
	{
		/*
		* Array use this format :
		* {"Command","hint "}
		*/
		{"HELP",         "Display command information"},
		{"SYNC",         "description label 1"},
		{"SHOWDATE",     "description label 2"},
		{"SHOWTIME",     "display current time "},
		{"SHOWALL",      "Show current config from memory"},
	};

On compiling, it show me this error message :

C:\Users\xxxxx\Dropbox\Arduino\libraries\terminalsync\terminalsync.cpp:10:14: error: expected unqualified-id before 'this'

  const char* this->commandList[][91] =
                   ^

C:\Users\xxxxx\Dropbox\Arduino\libraries\terminalsync\terminalsync.cpp:10:14: error: expected initializer before 'this'

C:\Users\xxxx\Dropbox\Arduino\libraries\terminalsync\terminalsync.cpp:71:1: error: expected '}' at end of input

 }
^

I do not understand why. I check some official library and "this->" keyword is used (for methods, not attributes but i think i'ts the same !).

Can someone have any clue ?

Thanks !
BR.

You are trying to declare a new variable in the constructor. You should not be doing that. There are no shortcuts for populating an array that you have already declared.

So this happen because this is a const array ? Using a non-const should be ok ?

In my case, i have to set it up in my class definition ?

Thanks for your time.

You are deferencing a pointer with -> which makes no sense at all in a declaration. Declare the
variable in the class definition like any other variable.

So this happen because this is a const array ?

No. But, because the array is const, you won't be able to value it in the constructor. You can't make the array const.

Try like this:

class MyClass{
  public:
    MyClass();
    static char commands[3][32];
  private:
};

MyClass::MyClass()
{
  
}

char MyClass::commands[3][32] = {"Command1", "Command2", "Command3"};

MyClass myClass;
void setup() 
{
  Serial.begin(9600);
  for(int i = 0; i < 3; i++)
  {
    Serial.println(myClass.commands[i]);
  }
}

void loop() 
{

}

and to demonstrate the static-ness:

class MyClass{
  public:
    MyClass();
    static char commands[3][32];
  private:
};

MyClass::MyClass()
{
  
}

char MyClass::commands[3][32] = {"Command1", "Command2", "Command3"};

MyClass myClass;
MyClass myOtherOne;
void setup() 
{
  Serial.begin(9600);
  for(int i = 0; i < 3; i++)
  {
    Serial.println(myClass.commands[i]);
  }
  Serial.println("The Other One");
  for(int i = 0; i < 3; i++)
  {
    Serial.println(myOtherOne.commands[i]);
  }
  Serial.println("the third one");
  MyClass* c;
  c = new MyClass;
  for(int i = 0; i < 3; i++)
  {
    Serial.println(c->commands[i]);
  }
}

void loop() 
{
}

You must NOT declare a variable called "this". "this" is the name of the extra (hidden) parameter to each method.

Mark

holmes4:
You must NOT declare a variable called "this". "this" is the name of the extra (hidden) parameter to each method.

Mark

Where do you see OP trying to do that?