error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&, Node<T>*) [with T = int]' without object - c++
This is a discussion on error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&, Node<T>*) [with T = int]' without object - c++ ; Hello everyone,
I'm a first time post here. I tried searching the net for the answer,
but couldn't find anything. I'm having trouble with this error
message:
error: cannot call member function `std: stream&
List<T>::recOstream(std: stream&, Node<T>*) [with T = ...
-
error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&, Node<T>*) [with T = int]' without object
Hello everyone,
I'm a first time post here. I tried searching the net for the answer,
but couldn't find anything. I'm having trouble with this error
message:
error: cannot call member function `std:
stream&
List<T>::recOstream(std:
stream&, Node<T>*) [with T = int]' without
object
The relevent code is:
=====================================
friend ostream& operator<<(ostream& out, List<T>& l)
{
return List<T>::recOstream(out, l.front); // error on this line
}
ostream& recOstream(ostream& o, Node<T>* n)
{
if (n == NULL) {
return o;
}
else {
o << n->item << " ";
return recOstream(o, n->next);
}
}
===================================
I have to use recursion because it's a homework assignment. List<T> is
a singlely linked list. Front is a Node<T>* to the first node in the
list. next is a Node<T>* to next item on the list. (both are public).
Does anyone have any idea's what the problem is? Any help would be
greatly appreciated.
P.S. I'm using gcc on a solaris machine. If you need more info, then
please just let me know.
The full code is:
===================================
#ifndef BERLING_LIST_H
#define BERLING_LIST_H
#include <iostream>
#include <iomanip>
using namespace std;
template <typename T>
class Node
{
public:
T item;
Node<T> *next;
Node(T itemParam = T())
{
item = itemParam;
next = NULL;
}
~Node()
{
if (next != NULL)
delete next;
}
};
template <typename T>
class List
{
protected:
Node<T> *front;
int iSize;
public:
List()
{
front = NULL;
}
List(const List<T>& ol)
{
if (ol.front == NULL)
{
front = NULL;
return;
}
front = new Node<T>(ol.front->item);
Node<T> *on = ol.front; // old node
Node<T> *nn = front; // new node
while (on->next != NULL)
{
nn->next = new Node<T>(on->next->item);
nn = nn->next;
on = on->next;
}
}
~List()
{
delete front;
}
inline int size()
{
return iSize;
}
inline bool empty()
{
return (iSize == 0);
}
void insert(const T& itm)
{
// I know that this is not efficent, but it does
// test the recursion
insert(itm, iSize);
}
void insert(const T& itm, int pos)
{
Node<T>* nn = new Node<T>(itm);
recInsert(front, front, nn, pos);
}
void recInsert(Node<T> *&prevNode, Node<T>* nextNode, Node<T>
*newNode, int pos)
{
if (pos == 0)
{
// insert node
newNode->next = nextNode;
prevNode = newNode;
iSize++;
}
else
{
recInsert(nextNode->next, nextNode->next, newNode, pos - 1);
}
}
void reverse()
{
front = recReverse(NULL, front);
}
Node<T>* recReverse(Node<T>* prevNode, Node<T>* nextNode)
{
if (nextNode == NULL)
{
return prevNode;
}
else
{
Node<T> *currentNode = nextNode->next;
nextNode->next = prevNode;
return recReverse(nextNode, currentNode);
}
}
T get(int i)
{
return recGet(i, front);
}
T recGet(int i, Node<T> *n)
{
if (i == 0)
return n->item;
else
return recGet(i - 1, n->next);
}
/**
// works correctly
friend ostream& operator<<(ostream &out, List<T> &l)
{
Node<T> *cur = l.front;
while (cur != NULL)
{
out << setw(2) << cur->item << ' ';
cur = cur->next;
}
return out;
}
**/
friend ostream& operator<<(ostream& out, List<T>& l)
{
return List<T>::recOstream(out, l.front); // error
}
ostream& recOstream(ostream& o, Node<T>* n)
{
if (n == NULL)
{
return o;
}
else
{
o << n->item << " ";
return recOstream(o, n->next);
}
}
};
#endif
-
-
Re: error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&, Node<T>*) [with T = int]' without object
On Feb 19, 7:04 am, jonberl...@yahoo.com wrote:
> friend ostream& operator<<(ostream& out, List<T>& l)
> {
> return List<T>::recOstream(out, l.front); // error on this line
>
> }
>
> ostream& recOstream(ostream& o, Node<T>* n)
> {
> if (n == NULL) {
> return o;
> }
> else {
> o << n->item << " ";
> return recOstream(o, n->next);
> }}
(Note, this function is actually defined inside the definition
of List<T>).
The problem is that 'recOstream' is a non-static member function,
so in order to call it you have to have an object. So you
could get it to work by changing the function call to:
return l.recOstream(out, l.front);
However there is bad design here: the recOstream function does not
use any properties of the list object, so it should not be a
non-static member function.
One solution would be to leave the call as-is, and change
the recOstream function to be static.
An even better solution would be to make it a free function,
ie. not a member of any class. Then you can call it without
any qualifier.
-
Re: error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&, Node<T>*) [with T = int]' without object
Thank-you for your help. I was able to finish my project just fine and
improve my understanding of C++ at the same time. 
Similar Threads
-
By Application Development in forum c++
Replies: 10
Last Post: 09-10-2007, 08:40 AM
-
By Application Development in forum c++
Replies: 1
Last Post: 08-21-2007, 03:12 AM
-
By Application Development in forum c++
Replies: 2
Last Post: 08-01-2007, 01:50 PM
-
By Application Development in forum c++
Replies: 6
Last Post: 06-23-2007, 02:11 PM
-
By Application Development in forum c++
Replies: 5
Last Post: 02-20-2007, 12:58 PM