[SOLVED?] Using "this" pointer to "copy" pointer
-
Apparently compiler does not like to have both constructor and class variable names to be same. this->variable_name = variable_name I am using "this" pointer to pass / copy parameters passed to class constructor so they can be accessed by class methods. I have managed to do it for simple int Here is the declaration of int disp_x_size_, disp_y_size_; in the class int disp_x_size_, disp_y_size_; //size x y int *ControlPin; // control pins array int *DB; // data bus pins array and here is how I use "this" pointer in the constructor : this->disp_x_size_ = disp_x_size_; // class local variable = paramater passed to constructor works fine. Now I tried to do same for passed pointer - for ControlPin declaration see above and here is the "this" pointer code this->ControlPin = ControlPin; this->DB = DB; Compiles OK , but when I try to use it all I am getting is the actual content of the first member of the array. I have limited ways to check the pointer. When I try to actually use it in this function LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position it does not compile with the following error C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h: In member function 'void TFT_LCD::setXY(word, word, word, word)': C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h:9297:87: error: no matching function for call to 'TFT_LCD::LCD_Write_COM_DATA_t(int*&, int*&, uint32_t, uint32_t, int)' LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position I believe that instead of simple int* I am passing the contents of the first array member int*& which obviously is not the TYPE expected by the template function.( And I can verify that) BTW that function works when correct TYPE pointer is passed to it. I basically need someone to explain to me what am I doing wrong using "this" pointer that I end up with int*& instead of just int*. I hope it makes sense, if not I can explain it more. But don't ask for full code - it is now over 10000 lines of giant mess Appreciate any help, as always Cheers Vaclav
-
Apparently compiler does not like to have both constructor and class variable names to be same. this->variable_name = variable_name I am using "this" pointer to pass / copy parameters passed to class constructor so they can be accessed by class methods. I have managed to do it for simple int Here is the declaration of int disp_x_size_, disp_y_size_; in the class int disp_x_size_, disp_y_size_; //size x y int *ControlPin; // control pins array int *DB; // data bus pins array and here is how I use "this" pointer in the constructor : this->disp_x_size_ = disp_x_size_; // class local variable = paramater passed to constructor works fine. Now I tried to do same for passed pointer - for ControlPin declaration see above and here is the "this" pointer code this->ControlPin = ControlPin; this->DB = DB; Compiles OK , but when I try to use it all I am getting is the actual content of the first member of the array. I have limited ways to check the pointer. When I try to actually use it in this function LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position it does not compile with the following error C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h: In member function 'void TFT_LCD::setXY(word, word, word, word)': C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h:9297:87: error: no matching function for call to 'TFT_LCD::LCD_Write_COM_DATA_t(int*&, int*&, uint32_t, uint32_t, int)' LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position I believe that instead of simple int* I am passing the contents of the first array member int*& which obviously is not the TYPE expected by the template function.( And I can verify that) BTW that function works when correct TYPE pointer is passed to it. I basically need someone to explain to me what am I doing wrong using "this" pointer that I end up with int*& instead of just int*. I hope it makes sense, if not I can explain it more. But don't ask for full code - it is now over 10000 lines of giant mess Appreciate any help, as always Cheers Vaclav
Vaclav_Sal wrote:
what am I doing wrong using "this"
Nothing.
this
is merely a pointer to the data space of 'this' object, when in the code of that object, i.e. inside the methods declared in the class. Using it in the constructor has no relevance to your problem. The problem is that your call toLCD_Write_COM_DATA_t
contains parameters that do not match the declaration of the method. Taker a look at Pointer to Pointer and Reference to Pointer[^] for an explanation. -
Vaclav_Sal wrote:
what am I doing wrong using "this"
Nothing.
this
is merely a pointer to the data space of 'this' object, when in the code of that object, i.e. inside the methods declared in the class. Using it in the constructor has no relevance to your problem. The problem is that your call toLCD_Write_COM_DATA_t
contains parameters that do not match the declaration of the method. Taker a look at Pointer to Pointer and Reference to Pointer[^] for an explanation."contains parameters that do not match the declaration of the method." Sorry, but it is just the opposite. The method is OK, it's the wrong parameters I am passing in and I really do not know why. I do not want contents , I need pointer to the array. If I change the method I would have to rebuild it so it will take a contents, not pointer. I'll take a look at the reference, thanks you for that.
-
"contains parameters that do not match the declaration of the method." Sorry, but it is just the opposite. The method is OK, it's the wrong parameters I am passing in and I really do not know why. I do not want contents , I need pointer to the array. If I change the method I would have to rebuild it so it will take a contents, not pointer. I'll take a look at the reference, thanks you for that.
-
Apparently compiler does not like to have both constructor and class variable names to be same. this->variable_name = variable_name I am using "this" pointer to pass / copy parameters passed to class constructor so they can be accessed by class methods. I have managed to do it for simple int Here is the declaration of int disp_x_size_, disp_y_size_; in the class int disp_x_size_, disp_y_size_; //size x y int *ControlPin; // control pins array int *DB; // data bus pins array and here is how I use "this" pointer in the constructor : this->disp_x_size_ = disp_x_size_; // class local variable = paramater passed to constructor works fine. Now I tried to do same for passed pointer - for ControlPin declaration see above and here is the "this" pointer code this->ControlPin = ControlPin; this->DB = DB; Compiles OK , but when I try to use it all I am getting is the actual content of the first member of the array. I have limited ways to check the pointer. When I try to actually use it in this function LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position it does not compile with the following error C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h: In member function 'void TFT_LCD::setXY(word, word, word, word)': C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h:9297:87: error: no matching function for call to 'TFT_LCD::LCD_Write_COM_DATA_t(int*&, int*&, uint32_t, uint32_t, int)' LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position I believe that instead of simple int* I am passing the contents of the first array member int*& which obviously is not the TYPE expected by the template function.( And I can verify that) BTW that function works when correct TYPE pointer is passed to it. I basically need someone to explain to me what am I doing wrong using "this" pointer that I end up with int*& instead of just int*. I hope it makes sense, if not I can explain it more. But don't ask for full code - it is now over 10000 lines of giant mess Appreciate any help, as always Cheers Vaclav
Vaclav_Sal wrote:
Apparently compiler does not like to have both constructor and class variable names to be same
Not true, it does not matter. That is why you need to use the
this
pointer, so the compiler knows which variable you are referring to. Consider the following:Class Foo
{
int var1;foo(int var1) { var1 = var1; // referring only to the parameter var1 this->var1 = var1; // now it knows that the first one is the class variable. }
None of which has any relevance to the issue you are describing. Forget about what goes on in the constructor, it is not connected to the probelm. You need to go back to the documentation and check carefully the definition of the
TFT_LCD::LCD_Write_COM_DATA_t
method.