Thread: pointer to function error

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    pointer to function error

    i'm attempting this:
    Code:
    void KeyPositioner::set_key_indices(Key k){
    
        for(int i = 0; i < 8; i++){
    
            index[i] = k[i];
        }
    }
    where the index array is a private member of KeyPositioner and k is a key, obviously.

    I think i need to overload "[]" to accomplish what I'm attempting here, though another solution/work-around will do the job as well.

    Anyway, it's giving me the error:
    Code:
    error: pointer to function used in arithmetic
    error: no match for operator[] in k[i]

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think i need to overload "[]" to accomplish what I'm attempting here
    Yes, at least with the given syntax.

    By the way, what is index? That is probably what is causing the first error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How is Key defined? If I'm reading the error message correctly, a Key is a function? So why wouldn't you call k(i) instead?

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Key is a class with private data: array size 8 of chars.

    KeyPositioner is a class with private data: "index" which is an array of size 8 as well. "Index" is not the same here as the index of the array "an_array[i]". Index defines the position of certain elements in a larger array.

    That might sound silly, but I'm manipulating arrays of chars as they go into a larger array basically.

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    anyway i fixed the errors, i just created a get_element(int i) function which will return the needed "char" at the specified pos.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How is Key defined? If I'm reading the error message correctly, a Key is a function? So why wouldn't you call k(i) instead?
    Yeah, I think my interpretation is wrong since I missed the part about index being an array.

    dudeomanodude, I hope this shows you that saying "k is a key" is meaningless since we do not know what is a Key. Even stating "index array is a private member of KeyPositioner" is providing scant information since we do not know the type of index. Sometimes, it is good to post the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    srry, i shall provide more info next time!

  8. #8
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    If I got it right, you have a class Key, that has a member char index[8]. To access it from outside the class, you must use a friend function, and you should use k.index[i], that is

    Code:
    index[i] = k.index[i]
    but it won't work unless the class Keypositioner or the function is a friend of Key, (I suppose).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM