By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

illegal call of non-static member compile error question

P: n/a
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;
}
Oct 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Oct 20 '05 #2

P: n/a
Ook

"John Ratliff" <us**@example.net> 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?
Oct 20 '05 #3

P: n/a
Ook wrote:
"John Ratliff" <us**@example.net> 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

Oct 20 '05 #4

P: n/a
"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:As********************@giganews.com...

"John Ratliff" <us**@example.net> 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

Oct 20 '05 #5

P: n/a
Ook

"Greg" <gr****@pacbell.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Ook wrote:
"John Ratliff" <us**@example.net> 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
Oct 20 '05 #6

P: n/a
Ook

"Ali «ehreli" <ac******@yahoo.com> wrote in message
news:dj**********@domitilla.aioe.org...
<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
:-)
Oct 20 '05 #7

P: n/a
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
Oct 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.