linked lists

Started by
4 comments, last by Johnny_Blaze93 21 years, 8 months ago
just messin around with linked lists, this code:
    
#include <fstream.h>

#include <iostream> 

#include <stdio.h> 

#include <string.h> 

#include <conio.h> 

using namespace std; 

class Element { 
public: 
   Element (); 
   ~Element (); 
   char *String; 
   Element *Next_Element; 
}; 

Element::Element () { 
   String = new char; 
} 

Element::~Element () { 
   delete [] String; 
} 

void main () { 
			ofstream linkedlist("LinkedList.txt");
   char *String_Temp; 
   String_Temp = new char; 

   Element *Element_Temp; 
   Element *First_Element = 0; 
   Element *Last_Element = 0; 

   for (;;) { 
      cout << "Enter a string.\n"; 
      gets (String_Temp); 

      if (strcmp(String_Temp , "stop") == 0) break; 
       
      Element_Temp = new Element; 

      strcpy (Element_Temp->String , String_Temp); 
      Element_Temp->Next_Element = 0; 

      if (Last_Element) 
         Last_Element->Next_Element = Element_Temp; 
      else 
         First_Element = Element_Temp; 

      Last_Element = Element_Temp; 

   } 
   if (First_Element) { 
      cout << "Now print the whole list!\n"; 

      Element_Temp = First_Element; 

      do { 

         cout << Element_Temp->String<<endl; 

         Element_Temp = Element_Temp->Next_Element; 


		linkedlist<< Element_Temp->String;
		linkedlist<<"\n";

      } 
      while (Element_Temp); 
   } 

cout << "Press any key to continue!\n"; 
getch(); 
_flushall();
linkedlist.close();
} 

   
gives me these errors : C:\Windows\Desktop\linkedlists\main.cpp(26) : error C2872: 'ofstream' : ambiguous symbol C:\Windows\Desktop\linkedlists\main.cpp(35) : error C2872: 'cout' : ambiguous symbol C:\Windows\Desktop\linkedlists\main.cpp(54) : error C2872: 'cout' : ambiguous symbol C:\Windows\Desktop\linkedlists\main.cpp(60) : error C2872: 'cout' : ambiguous symbol C:\Windows\Desktop\linkedlists\main.cpp(72) : error C2872: 'cout' : ambiguous symbol i dont get it, could someone help me? [edited by - johnny_blaze93 on August 12, 2002 5:37:33 PM] [/source] [edited by - johnny_blaze93 on August 12, 2002 5:38:00 PM]
I OWN YOU
Advertisement
i think it should be not
Rodger
ok, that cut out the most important part of what i was saying. what i sad was it should be iostream.h instead of just iostream.
Rodger
thanks alot
I OWN YOU
There''s two problems, one is that you''re mixing old-style with new-style headers. Don''t use the ".h" version of STL headers, they''re not standard. So include <fstream> and not <fstream.h>

The other problem will manifest itself once you can actually compile and run it. The line:

String = new char;

Will create only 1 character, which isn''t what you want. Since you''re using the STL already, I suggest you use the string class, which is a lot simpler.

If I had my way, I''d have all of you shot!

codeka.com - Just click it.
Besides #include<string>, there''s also #include <sstream> which lets you create string streams. They work like iostream and/or fstream, expcet they write everything into a std::string.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement