Results 1 to 4 of 4

Thread: Converting "this" variables

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Posts
    253
    Hello,

    I have a class called "Set<T>" (a template class) and a function outside of it. One of the parameters of the function is "const Set<T> &setCompare2".
    Inside the function, I need to call one of the (public)member functions of the class called "IsMember" (from the setCompare2 variable).

    The problem is that when I compile the program, I get this error:

    "error C2662: 'IsMember' : cannot convert 'this' pointer from 'const class Set<int>' to 'class Set<int> &'"

    Currently, I have 3 solutions to this problem that may work, but every solution has its flaw:

    1. Changing the function parameter from "const Set<T> &setCompare2" to "Set<T> &setCompare2". I can't do that because of the instructions of writing this program (it's an exercise).

    2. Copying the lines of the IsMember function to the problematic function: not efficient.

    3. Declaring a "Set<T>" local variable in the problematic function, copying all the setCompare2's attributes values into it and using it to call the IsMember function:
    requires a lot of time from the processor (many copy instructions).

    Does anyone have another idea?

    Thanks.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What's the code causing the problem? Why do you need the const?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy Hrmm

    Hey, Parksie, could you please have alook at the thread I posted yesterday "Parallel Port-Urgent" please, thanks in advance
    Amon Ra
    The Power of Learning.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Posts
    253
    The line causing the problem is:

    "if (setCompare2.IsMember(setCompare1.ElementsArray[Counter])==NOT_FOUND)"

    (setCompare1 and setCompare2 are declared as "const Set<T>&").

    I need to declare setCompare2 as a constant parameter because the function is not supposed to be able to change it. These are the instructions of writing this program, so I can't change the declaration.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width