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&amp; List&lt;T&gt;::recOstream(std: stream&amp;, Node&lt;T&gt;*) [with T = ...

+ Reply to Thread
Results 1 to 4 of 4

error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&, Node<T>*) [with T = int]' without object

  1. Default 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


  2. Default Re: error: cannot call member function `std::ostream& List<T>::recOstream(std::ostream&,Node<T>*) [with T = int]' without object

    jonberling@yahoo.com wrote:
    > 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


    It looks like recOstream isn't declared as a static member function, so
    you can't use List<T>::recOstream to call it, you have to use a List<T>
    object. Is l.recOstream(out, l.front) what you are after?

    --
    Ian Collins.

  3. Default 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.


  4. Default 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.



+ Reply to Thread

Similar Threads

  1. Curious question about the STL ostream
    By Application Development in forum c++
    Replies: 10
    Last Post: 09-10-2007, 08:40 AM
  2. istream, ostream and iostream
    By Application Development in forum c++
    Replies: 1
    Last Post: 08-21-2007, 03:12 AM
  3. Error - Cannot call member function without object...
    By Application Development in forum c++
    Replies: 2
    Last Post: 08-01-2007, 01:50 PM
  4. Inheritance from ostream
    By Application Development in forum c++
    Replies: 6
    Last Post: 06-23-2007, 02:11 PM
  5. ostream::operator <<( size_t )
    By Application Development in forum c++
    Replies: 5
    Last Post: 02-20-2007, 12:58 PM