Error - Cannot call member function without object... - c++

This is a discussion on Error - Cannot call member function without object... - c++ ; Hello Everyone, I have a function in a header (KeyDialog.h) as such: void setKey(Key&); The function implementation is as such (KeyDialog.cpp): void KeyDialog::setKey(Key& k1) { Key::Key K1 = k1; //unimportant } And I'm calling this function from another cpp file ...

+ Reply to Thread
Results 1 to 3 of 3

Error - Cannot call member function without object...

  1. Default Error - Cannot call member function without object...

    Hello Everyone,

    I have a function in a header (KeyDialog.h) as such:

    void setKey(Key&);

    The function implementation is as such (KeyDialog.cpp):

    void KeyDialog::setKey(Key& k1)
    {
    Key::Key K1 = k1;
    //unimportant
    }

    And I'm calling this function from another cpp file (mainwindow.cpp)
    like so:

    KeyDialog::setKey(enter);

    When I compile I get the error: Cannot call member function 'void
    KeyDialog::setKey(Key&)' without object...

    This is probably stupidly simple, what am i missing?


  2. Default Re: Error - Cannot call member function without object...

    Elliott <ewdicus> wrote:

    > Hello Everyone,
    >
    > I have a function in a header (KeyDialog.h) as such:
    >
    > void setKey(Key&);
    >
    > The function implementation is as such (KeyDialog.cpp):
    >
    > void KeyDialog::setKey(Key& k1)
    > {
    > Key::Key K1 = k1;
    > //unimportant
    > }
    >
    > And I'm calling this function from another cpp file (mainwindow.cpp)
    > like so:
    >
    > KeyDialog::setKey(enter);
    >
    > When I compile I get the error: Cannot call member function 'void
    > KeyDialog::setKey(Key&)' without object...
    >
    > This is probably stupidly simple, what am i missing?


    You are missing an object. Try one of these in the other cpp file:

    KeyDialog kd;
    kd.setKey( enter );

    There is probably some other fundamental error in your code though. It
    may be that your "setKey" function doesn't need an object and therefore
    should not be in the KeyDialog class.

  3. Default Re: Error - Cannot call member function without object...

    Wow, I feel a bit stupid...a bit of a duh moment...

    I had:

    KeyDialog dialog(this);
    KeyDialog::setKey(enter);
    dialog.exec();

    When of course i needed to have:

    KeyDialog dialog(this);
    dialog.setKey(enter);
    dialog.exec();

    Thank you very much, your small bit of code helped tremendously


+ Reply to Thread

Similar Threads

  1. class template member function - compilation error
    By Application Development in forum c++
    Replies: 2
    Last Post: 11-02-2007, 07:33 AM
  2. Replies: 7
    Last Post: 09-17-2007, 02:11 PM
  3. Replies: 3
    Last Post: 07-06-2007, 07:50 AM
  4. Replies: 14
    Last Post: 06-16-2007, 08:56 AM
  5. Replies: 3
    Last Post: 02-19-2007, 02:19 PM