Archived

This topic is now archived and is closed to further replies.

Johnny_Blaze93

linked lists

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites