illegal call of non-static member compile error question

O

Ook

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;
}
 
J

John Ratliff

Ook said:
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
 
O

Ook

John Ratliff said:
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?
 
G

Greg

Ook said:
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
 
G

Guest

"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

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.

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
I don't get it - where am I trying to call it from the class?

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

Ali
 
O

Ook

Greg said:
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
 
O

Ook

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
:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top