Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [SOLVED?] Using "this" pointer to "copy" pointer

[SOLVED?] Using "this" pointer to "copy" pointer

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasedata-structuresquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    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

    L 2 Replies Last reply
    0
    • V 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 to LCD_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.

      V 1 Reply Last reply
      0
      • L Lost User

        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 to LCD_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.

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        "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.

        L 1 Reply Last reply
        0
        • V Vaclav_

          "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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Vaclav_Sal wrote:

          The method is OK, it's the wrong parameters I am passing in

          That's what I said.

          1 Reply Last reply
          0
          • V 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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups