How do you declare a list to be global.....

This is a discussion on How do you declare a list to be global..... within the C++ Programming forums, part of the General Programming Boards category; I have a class that is used to control the users input. In that class I have a function that ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    How do you declare a list to be global.....

    I have a class that is used to control the users input. In that class I have a function that looks like this

    Code:
    void Controller::AddArtBandName() 
    { 
    
    	string artName; 
    
    		cout << endl << "Enter the bands name: " << flush; 
    		cin >> artName; 
    	
    	Artist *artist = new Artist(artName);	
    
    	list<Artist> myArtist;
    
    		myArtist.push_back( *(new Artist(artName)) );	
    		
    		//Informs user what they just added
    		cout << "\tAdded : " << artName << endl << endl ;	
    	
    }
    This function is called upon when a user asks to enter an Artists name. Now I want to make another function that displays what is inside that list. This is what I have so far...

    Code:
    void Controller::DisplayArtName()
    {
    
    	list<Artist> myArtist;
    	
    		cout << endl << "List of Artist: " << endl;
    
    	list<Artist>::iterator iter;
    
    		for(iter = myArtist.begin(); iter != myArtist.end(); iter++)
    			
    			cout << (*iter).getArtBandName() << endl;
    }
    When I call upon this function it does not display anything in the list. I am guessing that this is because the list is redefined in my DisplayArtName function. So with that said the only solution I can come up with is to declare the list global in my .h file, but I can't figure out how to do it. Any suggestion?

    Thanks

    Chad

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Maybe you should re-look your class hierarchy to see where the list might fit in?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by laserlight
    Maybe you should re-look your class hierarchy to see where the list might fit in?
    I am fairly new to programming (about 6 months). Can you please further explain you comment. I know what you mean when you say hierarchy but I am failing to grasp what you mean when you say it in conjunction with class.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You need to not create the list in any function, you need to make it a member variable of your controller class. Ex.
    Code:
    Class Controller
    {
       private:
         list<Artist> myArtist;
       public:
         void AddArtBandName();
         void DisplayArtName()
         ... other declarations
    };
    
    void Controller::AddArtBandName() 
    { 
    
    	string artName; 
    
    		cout << endl << "Enter the bands name: " << flush; 
    		cin >> artName; 
    	
    	Artist artist (artName);	
    
    	//list<Artist> myArtist; <--DELETE THIS ... use member variable
    
    		myArtist.push_back( artist );	
    		
    		//Informs user what they just added
    		cout << "\tAdded : " << artName << endl << endl ;	
    	
    } 
    void Controller::DisplayArtName()
    {
    
    	//list<Artist> myArtist; <- DELETE THIS LINE...use member variable
    	
    		cout << endl << "List of Artist: " << endl;
    
    	list<Artist>::iterator iter;
    
    		for(iter = myArtist.begin(); iter != myArtist.end(); iter++)
    			
    			cout << (*iter).getArtBandName() << endl;
    }
    One other thing I just notice, you are going to have a big memory leak by derefencing the artist pointers and pushing them as objects onto the list. The list will create copies of those object in the list and you will lose the pointers to the objects that you new'ed and thus won't be able to delete them when your program cleans up.
    Since list handles memory management, you could just local copies onto the list that then will be copied, see code
    Last edited by Darryl; 07-06-2005 at 11:26 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    cin >> artName; 
    	
    Artist *artist = new Artist(artName);	
    
    ...
    
    myArtist.push_back( *(new Artist(artName)) );
    You are creating a memory leak here by allocating and assigning memory to the pointer artist which you do not appear to ever use. Also, your second use of new to dynamically allocate an Artist object, followed by the dereference of the pointer in the push_back constitutes a second memory leak in the same function. The use of new here should not be needed at all. I think you'd do better with:

    Code:
    cin >> artName;
    
    // Assume myArtist is declared as member of Controller class, or global,
    // or passed in to the function as a parameter.
    
    myArtist.push_back( Artist(artName) );
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Declaring a list like this

    Code:
    private:
    
    list<Artist> myArtist;
    leaves me with a syntax error. Also when I remove

    Code:
    list<Artist> myArtist;
    from my two functions it tells me that myArtist is no longer declared.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by hk_mp5kpdw
    Code:
    cin >> artName; 
    	
    Artist *artist = new Artist(artName);	
    
    ...
    
    myArtist.push_back( *(new Artist(artName)) );
    You are creating a memory leak here by allocating and assigning memory to the pointer artist which you do not appear to ever use. Also, your second use of new to dynamically allocate an Artist object, followed by the dereference of the pointer in the push_back constitutes a second memory leak in the same function. The use of new here should not be needed at all. I think you'd do better with:

    Code:
    cin >> artName;
    
    // Assume myArtist is declared as member of Controller class, or global,
    // or passed in to the function as a parameter.
    
    myArtist.push_back( Artist(artName) );
    Would it make a diffrence if I said Artist is another class that is being used to get and set data?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by chadsxe
    Would it make a diffrence if I said Artist is another class that is being used to get and set data?
    I get that it is another class... and no it wouldn't make a difference.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    To fix the syntax error, you should post it here so we know what it was.

    You should also probably use std::list instead of list, make sure you've included <list> in the header file, and make sure you've included the file that declares the Artist class.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    This is a broken down version of what I have in .cpp file

    Code:
    #include "CdInfo.h"
    #include "Controller.h"
    #include "Artist.h"
    #include "Menu.h"
    #include "SubMenu.h"
    #include <iostream>
    #include <list>
    #include <string>
    using namespace std;
    
    //*********************MAIN*CONTROL********************
    
    //Takes input from user and processes it
    void Controller::CommandControl(int input)
    {
    	Menu menu;
    	
    	if (input == 1)
    	{
    		menu.Info(); //Displays info about program
    	}
    	else if (input == 2)
    	{
    		menu.MainMenu(); //Displays menu
    	}
    	else if (input == 3)
    	{
    		ArtMenu(); //Displays Art Menu
    	}
    	else if (input == 4)
    	{
    		CdInfoMenu();
    	}
    	else if (input == 5)
    	{
    		cout << "Goodbye" << endl; //End Program
    	}
    	else
    	{
    		cout << input << ":  is not a valid command" << endl;
    		
    	}
    }
    
    //********************ARTIST**********************
    
    //Takes input from user (Artist) and processes it
    void Controller::ArtMenu()
    {
    	int input;
    	Menu menu;
    	SubMenu submenu;
    	
    		submenu.Artist();
    
    			cout << endl << "Enter a number from the Artist Menu: " << flush;
    			cin >> input;
    
    	if (input == 1)
    	{
    		AddArtBandName();
    	}
    	else if (input == 2)
    	{
    		AddSoloName();
    	}
    	else if (input == 3)
    	{
    		DisplayArtName();
    	}
    	else if (input == 4)
    	{
    		menu.MainMenu();
    	}
    	else 
    	{
    		cout << input << ": is not a valid command" << endl;
    	}
    }
    
    //Adds Artist Name
    void Controller::AddArtBandName() 
    { 
    
    	string artName; 
    
    		cout << endl << "Enter the bands name: " << flush; 
    		cin >> artName; 
    	
    	Artist *artist = new Artist(artName);	
    
    	list<Artist> myArtist;
    
    		myArtist.push_back( *(new Artist(artName)) );	
    		
    		//Informs user what they just added
    		cout << "\tAdded : " << artName << endl << endl ;	
    	
    } 
    
    
    //Displays Artist List 
    //Still trying to get this to function properly
    void Controller::DisplayArtName()
    {
    
    	list<Artist> myArtist;
    	
    		cout << endl << "List of Artist: " << endl;
    
    	list<Artist>::iterator iter;
    
    		for(iter = myArtist.begin(); iter != myArtist.end(); iter++)
    			
    			cout << (*iter).getArtBandName() << endl;
    }
    and in my .h file

    Code:
    #ifndef _CONTROLLER_H
    #define _CONTROLLER_H
    #include <string>
    using namespace std;
    
    class Controller
    {
    	public:
    		
    		string artName;
    		string cdName;
    
    		void CommandControl(int input);
    		
    	
    		// Adds Artist Info
    		void AddArtBandName();
    		// Displays Artist Menu and processes users input
    		void ArtMenu();
    		// Diplays Artist Name
    		void DisplayArtName();
    
    		Controller();
    		
    };
    #endif _CONTROLLER_H
    when i declare my list in my header
    Code:
    public:
     list<Artist> myArtist;
    I get

    error C2143: syntax error : missing ';' before '<'
    Last edited by chadsxe; 07-06-2005 at 12:47 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> You should also probably use std::list instead of list, make sure you've included <list> in the header file, and make sure you've included the file that declares the Artist class.

    In the code you posted you didn't do any of what I said above - you didn't include <list> in your header file, you didn't use std::list instead of list, and you didn't include "Artist.h" in the header file. Do those things and the error should go away. (Note that since you have "using namespace std;" in your header file, you don't need to use std::list). Also don't forget to remove the list<Artist> MyArtist declarations in the functions, once you fix the header file problems the errors you got when you did that will go away.
    Last edited by Daved; 07-06-2005 at 12:54 PM.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Thanks Daved it works now. I was getting confused when you were telling me to inlcude Artist in my header file, but I figured it out. Like I said I am still a noob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. How to declare a list in another list???
    By zaracattle in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2006, 08:24 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21