0

I want to out put like below:

The Stack & the Class Template (in-lab portion)
test nodes: 101(-> / ) 212(-> / )
test next:  101(->212) 212(-> / )

But my out put will be like below:

The Stack & the Class Template (in-lab portion)
test nodes: 101 (-> / )  212 (-> / ) 
test next:  101 (->212 (-> / ) )  212 (-> / )

My header IntNode is like below:

class IntNode {
public:
    IntNode( int new_value );
    int      get_data( ) const;
    void     set_next( IntNode* new_next );
    IntNode* get_next( ) const;
    void     debug_write( ostream& outfile ) const;

private:
    int      payload;
    IntNode* next = nullptr;
};

ostream& operator<<( ostream& outfile, const IntNode& node );

The implementation of write function is below:

void IntNode::debug_write( ostream& outfile ) const{
        outfile << payload;
        if(next == nullptr)
            outfile << "(-> / ) ";  
        else
            outfile << "(->" << *next << ") ";
}

ostream& operator<<( ostream& outfile, const IntNode& node ){
    node.debug_write(outfile);
    return outfile;
}

The main function is like below:

int main(){

    cout << "The Stack & the Class Template (in-lab portion)\n";
    IntNode int_item_1( 101 );
    IntNode int_item_2( 212 );
    cout << "test nodes: " << int_item_1 << ' ' << int_item_2 << "\n";
    int_item_1.set_next( &int_item_2 );
    cout << "test next:  " << int_item_1 << ' ' << int_item_2 << "\n\n";

    return 0;
}
Johny
  • 15
  • 3

1 Answers1

-1

Your immediate issue is in attempting to pass a dereferenced next pointer (e.g. *next) for output in your debug.write() function. According to your shown output you need to pass the payload for the next node for output, e.g. next->payload. That will provide the output you show.

Additionally, you must have using namespace std; somewhere in your code. Please look at Why is “using namespace std;” considered bad practice?.

You don't show the implementation for your remaining member functions, so we have to guess a bit that your class implementation looks similar to:

class IntNode {
  public:
    IntNode (int new_value) { payload = new_value; }
    int      get_data() const { return payload; }
    void     set_next (IntNode* new_next) { this->next = new_next; }
    IntNode *get_next() const { return next; }
    void     debug_write (std::ostream& outfile) const;
    
  private:
    int      payload = 0;
    IntNode *next = nullptr;
};

By ordering your function definitions in the right order, you can do away with the prototype for the overload of <<, e.g.

std::ostream& operator <<(std::ostream& outfile, const IntNode& node)
{
    node.debug_write (outfile);
    
    return outfile;
}

void IntNode::debug_write (std::ostream& outfile) const {
        outfile << payload;
        if(next == nullptr)
            outfile << "(-> / ) ";  
        else
            outfile << "(->" << next->payload << ") ";
}

Then with the main() you show:

int main (void) {

    std::cout << "The Stack & the Class Template (in-lab portion)\n";
    
    IntNode int_item_1 (101);
    IntNode int_item_2 (212);
    
    std::cout << "test nodes: " << int_item_1 << ' ' << int_item_2 << "\n";
    
    int_item_1.set_next (&int_item_2);
    std::cout << "test next:  " << int_item_1 << ' ' << int_item_2 << "\n\n";
}

Example Use/Output

You receive the output you describe wanting:

$ ./bin/ll-intnode
The Stack & the Class Template (in-lab portion)
test nodes: 101(-> / )  212(-> / )
test next:  101(->212)  212(-> / )

When overloading << and >> for class use, you also have the option of declaring the overload function as a friend function allowing protected members to be directly accessed by the overloaded operator. Here you simply provide a public "getter" function used by the overloaded operator which is 100% fine.

look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85