Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6099375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:15:58+00:00 2026-05-23T13:15:58+00:00

For an assignment (and for myself) I need to be able to print a

  • 0

For an assignment (and for myself) I need to be able to print a singlely linked list of “Stock” objects.

Each Stock as the following private data members:
1 int
1 string

This is my print function in the linked list class I had to use/develop. I am not allowed to use STL::list().

//list.h
template<class NODETYPE>
void List< NODETYPE>::print() const
{
if(isEmpty() ){
    cout<<"the list is empty\n\n";
    return;
}

ListNode< NODETYPE> *currentPtr=firstPtr;

cout<<"The list is: ";

while (currentPtr!=0){
    //cout << currentPtr->data << ' ';
    currentPtr = currentPtr->nextPtr;
}

cout<< "\n\n";
}

Now for the Nodes in the List, listnode.h:

//ListNode.h
//ListNode Template Definition
#ifndef LISTNODE_H
#define LISTNODE_H

template< class NODETYPE > class List;

template < class NODETYPE>
class ListNode {
friend class List < NODETYPE>;

public:
    ListNode (const NODETYPE &); //constructor
    NODETYPE getData() const;

private:
   NODETYPE data;
   ListNode< NODETYPE> *nextPtr;

};

 template<class NODETYPE>
 ListNode< NODETYPE >::ListNode(const NODETYPE &info): data (info), nextPtr(0)
 {

 }
 template<class NODETYPE>
 NODETYPE ListNode<NODETYPE>::getData() const{
    return data;
 }

#endif

Now my Stock Class:

class Stock{
public:
    Stock(); //default constructor
    Stock(string, int); //overloaded constructor
    ~Stock(); //deconstructor
    void setSymbol(string); //sets stock symbol
    void setShares(int);
    string getSymbol();
    int getShares();
private:
    string symbol;
    int shares;
};
Stock::Stock(){
symbol=" ";
shares=0;
}
Stock::Stock(string s, int num){
    cout<<"Creating Stock with supplied Values! \n\n"<<endl;
    symbol=s;
    shares=num;
    price=p;
}

Simple int main() for testing:

int main(){

List < Stock > portfolio_list;

Stock Google("GOOG", 500);
Stock VMware("VMW", 90);
Stock Apple("APL", 350);

portfolio_list.insertAtFront(Google);
portfolio_list.insertAtFront(VMware);
portfolio_list.insertAtFront(Apple);

portfolio_list.print();

return 0;

}

So obviously, I get operator errors for “<<” in list.h, because it can’t output the contents of my stock class. My end goal is to loop through every element in a linked list and print the string symbol and int. I’m assuming I will need to use some sort of operator overloading? Or am I way off base? If I do, should this be implemented in my Stock class? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:15:59+00:00Added an answer on May 23, 2026 at 1:15 pm

    If I understood your code, you’ll have to overload the << operator to both ListNode and List classes.

    //Representation of ListNode
    template <class T>
    ostream& operator << (ostream& ostr, ListNode<T>& ls) {
        return ostr << "Foo"; //replace "Foo" with the proper representation
    }
    
    //Representation of List
    template <class T>
    ostream& operator << (ostream& ostr, List<T>& ls) {
        ostr << "[ ";
        for(ListNode<T> *i = ls.firstPtr; i != NULL; i = i->nextPtr)
            ostr << *i << " ";
        return ostr << "]";
    }
    

    Would print [ Foo Foo Foo ] for a 3 element list.

    BTW, you should mark those functions as friend if you need to access private members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm teaching myself c++ by creating my own data structure class (a matrix, to
For a school assignment I have to write x86 assembly code, except I can't
For my university assignment I have to design some basic managment system for sicknesses
For a database assignment I have to model a system for a school. Part
For my assignment I have been assigned the task of creating a DTD for
For my university assignment I have to make a networkable version of pacman. I
For a homework assignment I was given a Card class that has enumerated types
I am writing an assignment in MASM32 Assembly and I almost completed it but
I have an assignment in a language-independent class, and part of it is using
I'm currently working through an assignment that deals with Big-O and running times. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.