Thread: ambigious symbol error

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    ambigious symbol error

    If I have "using namespace std" in my code and try to use an ofstream object from the <fstream.h> header I get an Ambigious symbol error for ofstream.

    I need the namespace std for using strings from the <string> header.

    I fixed the problem by replacing "using namespace std" with "#define string std::string".

    It works fine for me, but other people will see this code, so what is the right way to fix this?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You need to use the new headers. fstream.h is deprecated. The new header is the same as old without the .h extension. So you need:

    #include <fstream>

    Most of the old headers such as math.h are now prefixed with a c and the .h removed. So the new math header is

    #include <cmath>
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    standard header files no longer have the .h extension. try using just iostream without the .h if you use the line "using namespace std" in your code.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Using those headers gives me these errors on this line.

    Code:
    ofstream	outfile;
    error C2146: syntax error : missing ';' before identifier 'outfile'
    error C2501: 'ofstream' : missing storage-class or type specifiers
    error C2501: 'outfile' : missing storage-class or type specifiers

    Im at a loss...

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    What compiler are you using? Also are you still using using namespace std; ?
    Because...

    Code:
    #include <fstream>
    
    int main( void )
    {
      std::ofstream outfile;
    
      return 0;
    }
    Should compile no problem.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Whoops, I was missing the namespace std in one of the headers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM