🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #702

help!

#702
From: Palmer M J
Date: Mon, 25 Jan 1993 14:00
98 lines
3105 bytes
I've recently been trying to get to grips with c++. I wrote the following
small programme to implement a list template. However, it does not behave as
I would have expected. If I define a list of chars or ints in main then all is
ok. But defining the list to be of type char* does not work. If I insert a
string item by typing 
   i aaa
then insert another string by typing
   i bbb
then type 
   d
to display the contents of the list, it prints out
   bbb bbb
If I then type
   s ccc
and then
   d
then what is printed out is 
   ccc ccc ccc
It seems that by reading in a string to the local char array item in main
causes the value of each element in list to be changed to the value of the
newly read in string. Could someone please put me out of my misery explain
what is going on.

Thanks in advance.
Mike Palmer.

PS. I am using gcc version 2.2.2

//////////////////////////////////////////////////////////////////////////////
// List template definition
//////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <iostream.h>

enum Boolean { false = 0, true = 1 };
template <class Type> class Item;

template <class Type> 
class List {
   public:
      List() { firstItem = 0; }
      ~List() {}
      void insert(Type);
      void display();
   private:
      Item<Type> *firstItem;
};

template <class Type> 
class Item {
   friend class List<Type>;
   private:
      Item(Type value) { val = value; next = 0; }
      Type val;
      Item *next;
};

template <class Type> 
void List<Type>::insert(Type val) {
   Item<Type> *pt = new Item<Type>(val);
   assert( pt != 0 );   // catch free store exhaustion
   pt->next = firstItem;
   firstItem = pt;
}


template <class Type> 
void List<Type>::display() {
   for ( Item<Type> *pt = firstItem; pt; pt = pt->next )
      cout << pt->val << " ";
   cout << endl;
}

//////////////////////////////////////////////////////////////////////////////
// Define and test a list of strings.
////////////////////////////////////////////////////////////////////////////// 
main () { 
   typedef char *Type;	
   List<Type> *list = new List<Type>; // create a list of strings
   char ch; 		// select test operation
   char  item[10];	// input string buffer
   while ( cin >> ch )
      switch(ch) {
	 case 'q':
	    return 1;
	 case 's':
            cin >> item;
            cout << item << '\n';
	    break;
         case 'i':
            cin >> item;
            list->insert(item);
            break;
         case 'd':
            list->display();
            break;
      }
}
Technical Details
Message-ID: <7684@sersun1.essex.ac.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!warwick!uknet!sersun1!csc2!palmm
References: <1993Jan21.004406.6508@informix.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!warwick!uknet!sersun1!csc2!palmm
From: palmm@csc2.essex.ac.uk (Palmer M J)
Newsgroups: gnu.g++.help
Subject: help!
Keywords: Linker, ld, collect
Message-ID: <7684@sersun1.essex.ac.uk>
Date: 25 Jan 93 14:00:00 GMT
References: <1993Jan21.004406.6508@informix.com>
Sender: news@sersun1.essex.ac.uk
Reply-To: palmm@essex.ac.uk (Palmer M J)
Organization: University of Essex, Colchester, UK
Lines: 98