illegal call of non-static member compile error question

Discussion in 'C++' started by Ook, Oct 20, 2005.

  1. Ook

    Ook Guest

    What am I doing wrong? This code gives a compile error:
    'SortedList<T>::insert' : illegal call of non-static member function. I've
    tried several variations of this, but keep getting the same error.


    template <typename T>
    class SortedList
    {
    public:
    void insert( T item); // Write this
    };
    //-------------------------------------------------
    template <typename T>
    void SortedList<T>::insert( T item)
    {
    }
    int main()
    {
    int i = 123;
    SortedList<int>::insert(i);
    return 0;
    }
     
    Ook, Oct 20, 2005
    #1
    1. Advertising

  2. Ook

    John Ratliff Guest

    Ook wrote:
    > What am I doing wrong? This code gives a compile error:
    > 'SortedList<T>::insert' : illegal call of non-static member function. I've
    > tried several variations of this, but keep getting the same error.
    >
    >
    > template <typename T>
    > class SortedList
    > {
    > public:
    > void insert( T item); // Write this
    > };
    > //-------------------------------------------------
    > template <typename T>
    > void SortedList<T>::insert( T item)
    > {
    > }
    > int main()
    > {
    > int i = 123;
    > SortedList<int>::insert(i);
    > return 0;
    > }
    >
    >


    It looks like you are trying to call a member function from a class,
    which cannot be done.

    Try this insted:

    int main() {
    int i = 123;
    SortedList<int> slist;
    slist.insert(i);
    }

    --John Ratliff
     
    John Ratliff, Oct 20, 2005
    #2
    1. Advertising

  3. Ook

    Ook Guest

    "John Ratliff" <> wrote in message
    news:4pB5f.258071$084.103411@attbi_s22...
    > Ook wrote:
    >> What am I doing wrong? This code gives a compile error:
    >> 'SortedList<T>::insert' : illegal call of non-static member function.
    >> I've tried several variations of this, but keep getting the same error.
    >>
    >>
    >> template <typename T>
    >> class SortedList
    >> {
    >> public:
    >> void insert( T item); // Write this
    >> };
    >> //-------------------------------------------------
    >> template <typename T>
    >> void SortedList<T>::insert( T item)
    >> {
    >> }
    >> int main()
    >> {
    >> int i = 123;
    >> SortedList<int>::insert(i);
    >> return 0;
    >> }

    >
    > It looks like you are trying to call a member function from a class, which
    > cannot be done.
    >
    > Try this insted:
    >
    > int main() {
    > int i = 123;
    > SortedList<int> slist;
    > slist.insert(i);
    > }
    >
    > --John Ratliff


    I don't get it - where am I trying to call it from the class?
     
    Ook, Oct 20, 2005
    #3
  4. Ook

    Greg Guest

    Ook wrote:
    > "John Ratliff" <> wrote in message
    > news:4pB5f.258071$084.103411@attbi_s22...
    > > Ook wrote:
    > >> What am I doing wrong? This code gives a compile error:
    > >> 'SortedList<T>::insert' : illegal call of non-static member function.
    > >> I've tried several variations of this, but keep getting the same error.
    > >>
    > >>
    > >> template <typename T>
    > >> class SortedList
    > >> {
    > >> public:
    > >> void insert( T item); // Write this
    > >> };
    > >> //-------------------------------------------------
    > >> template <typename T>
    > >> void SortedList<T>::insert( T item)
    > >> {
    > >> }
    > >> int main()
    > >> {
    > >> int i = 123;
    > >> SortedList<int>::insert(i);
    > >> return 0;
    > >> }

    > >
    > > It looks like you are trying to call a member function from a class, which
    > > cannot be done.
    > >
    > > Try this insted:
    > >
    > > int main() {
    > > int i = 123;
    > > SortedList<int> slist;
    > > slist.insert(i);
    > > }
    > >
    > > --John Ratliff

    >
    > I don't get it - where am I trying to call it from the class?


    The second line of main tries to call a class method, insert(), without
    a class instance.

    Greg
     
    Greg, Oct 20, 2005
    #4
  5. "Ook" <Ook Don't send me any freakin' spam at zootal dot com delete the
    Don't send me any freakin' spam> wrote in message
    news:...
    >
    > "John Ratliff" <> wrote in message
    > news:4pB5f.258071$084.103411@attbi_s22...
    >> Ook wrote:
    >>> What am I doing wrong? This code gives a compile error:
    >>> 'SortedList<T>::insert' : illegal call of non-static member function.
    >>> I've tried several variations of this, but keep getting the same error.
    >>>
    >>>
    >>> template <typename T>
    >>> class SortedList
    >>> {
    >>> public:
    >>> void insert( T item); // Write this
    >>> };
    >>> //-------------------------------------------------
    >>> template <typename T>
    >>> void SortedList<T>::insert( T item)
    >>> {
    >>> }
    >>> int main()
    >>> {
    >>> int i = 123;
    >>> SortedList<int>::insert(i);


    On the above line, you are calling a member function of SortedList<int>.
    Since there is no object involved on that line, you can use that syntax only
    with static member functions.

    Since it is natural to 'insert' into a SortedList object, I don't think you
    want that function to be static.

    >>> return 0;
    >>> }

    >>
    >> It looks like you are trying to call a member function from a class,
    >> which cannot be done.
    >>
    >> Try this insted:
    >>
    >> int main() {
    >> int i = 123;
    >> SortedList<int> slist;
    >> slist.insert(i);


    On the line above, John Ratliff is showing you how to call a non-static
    member function with an object. There, slist is an jobject of type
    SortedList<int>, and the member function thas is being called is 'insert'.

    >> }
    >>
    >> --John Ratliff

    >
    > I don't get it - where am I trying to call it from the class?


    Where you use the Class::function syntax...

    Ali
     
    =?utf-8?Q?Ali_=C3=87ehreli?=, Oct 20, 2005
    #5
  6. Ook

    Ook Guest

    "Greg" <> wrote in message
    news:...
    > Ook wrote:
    >> "John Ratliff" <> wrote in message
    >> news:4pB5f.258071$084.103411@attbi_s22...
    >> > Ook wrote:
    >> >> What am I doing wrong? This code gives a compile error:
    >> >> 'SortedList<T>::insert' : illegal call of non-static member function.
    >> >> I've tried several variations of this, but keep getting the same
    >> >> error.
    >> >>
    >> >>
    >> >> template <typename T>
    >> >> class SortedList
    >> >> {
    >> >> public:
    >> >> void insert( T item); // Write this
    >> >> };
    >> >> //-------------------------------------------------
    >> >> template <typename T>
    >> >> void SortedList<T>::insert( T item)
    >> >> {
    >> >> }
    >> >> int main()
    >> >> {
    >> >> int i = 123;
    >> >> SortedList<int>::insert(i);
    >> >> return 0;
    >> >> }
    >> >
    >> > It looks like you are trying to call a member function from a class,
    >> > which
    >> > cannot be done.
    >> >
    >> > Try this insted:
    >> >
    >> > int main() {
    >> > int i = 123;
    >> > SortedList<int> slist;
    >> > slist.insert(i);
    >> > }
    >> >
    >> > --John Ratliff

    >>
    >> I don't get it - where am I trying to call it from the class?

    >
    > The second line of main tries to call a class method, insert(), without
    > a class instance.
    >
    > Greg
    >


    Dooooohhhh!!!! I forgot to instantiate SortedList! Ack, I can't believe I
    didn't see that - and John even showed how to do it.... :p

    /hangs head in extreme embarrassment
     
    Ook, Oct 20, 2005
    #6
  7. Ook

    Ook Guest

    "Ali Çehreli" <> wrote in message
    news:dj6rul$hp1$...
    <snip>
    > On the line above, John Ratliff is showing you how to call a non-static
    > member function with an object. There, slist is an jobject of type
    > SortedList<int>, and the member function thas is being called is 'insert'.


    Yes, I stupidly missed that, even though it was quite obvious. My apologies
    for being so dense lol, and thanks as usual to all of the quick help I get
    here. Next time I'll read the FAQ and try to come up with a tougher question
    :)
     
    Ook, Oct 20, 2005
    #7
  8. Ook

    David White Guest

    Ook wrote:
    > Next time I'll read the FAQ and try to come up
    > with a tougher question :)


    If you do that you will simply be referred to the FAQ. :)

    DW
     
    David White, Oct 20, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    581
    Chris Uppal
    Nov 17, 2005
  2. Gabrielle  A. Grün

    illegal reference to non-static member

    Gabrielle A. Grün, Jan 7, 2006, in forum: C++
    Replies:
    2
    Views:
    6,235
    David Harmon
    Jan 7, 2006
  3. shanknbake
    Replies:
    10
    Views:
    3,060
  4. dolphin
    Replies:
    3
    Views:
    1,381
    Pete Becker
    Dec 5, 2007
  5. Angus
    Replies:
    1
    Views:
    2,689
Loading...

Share This Page