pointer to function error

This is a discussion on pointer to function error within the C++ Programming forums, part of the General Programming Boards category; i'm attempting this: Code: void KeyPositioner::set_key_indices(Key k){ for(int i = 0; i < 8; i++){ index[i] = k[i]; } } ...

  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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21