Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > illegal call of non-static member compile error question

Reply
Thread Tools

illegal call of non-static member compile error question

 
 
Ook
Guest
Posts: n/a
 
      10-19-2005
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;
}


 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      10-20-2005
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
 
Reply With Quote
 
 
 
 
Ook
Guest
Posts: n/a
 
      10-20-2005

"John Ratliff" <(E-Mail Removed)> 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?


 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-20-2005
Ook wrote:
> "John Ratliff" <(E-Mail Removed)> 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

 
Reply With Quote
 
=?utf-8?Q?Ali_=C3=87ehreli?=
Guest
Posts: n/a
 
      10-20-2005
"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:(E-Mail Removed)...
>
> "John Ratliff" <(E-Mail Removed)> 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

 
Reply With Quote
 
Ook
Guest
Posts: n/a
 
      10-20-2005

"Greg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Ook wrote:
>> "John Ratliff" <(E-Mail Removed)> 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....

/hangs head in extreme embarrassment


 
Reply With Quote
 
Ook
Guest
Posts: n/a
 
      10-20-2005

"Ali Çehreli" <(E-Mail Removed)> wrote in message
news:dj6rul$hp1$(E-Mail Removed)...
<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



 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      10-20-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call member function without object-compile error eric C++ 6 06-22-2011 04:54 AM
error C2614: 'CYYYRegister' : illegal member initialization:'CRequest' is not a base or member Angus C++ 1 03-06-2008 11:38 AM
illegal call of non-static member function [why???] shanknbake C++ 10 04-18-2007 11:35 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM



Advertisments