Thread: compile time error...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    compile time error...

    when I use this:
    Code:
    string reason( "connect() failed" );
    throw ( logic_error( reason ) );  // this is line 65.
    I get the following compile-time errors:
    Code:
    C:\Ruchikar\RnD\wbsrvc1\Session.cpp(65) : error C2061: syntax error : identifier 'reason'
    C:\Ruchikar\RnD\wbsrvc1\Session.cpp(65) : error C2066: cast to function type is illegal
    C:\Ruchikar\RnD\wbsrvc1\Session.cpp(65) : error C2059: syntax error : ';'
    But using this:
    Code:
    throw ( logic_error( string( "connect() failed." ) ) );
    works just fine! What's the difference?

    I'm using MS VC++ on Win2K.

    Thanks in advance.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Your first 3 error codes are most likely trying to tell you it doesn't know what "string" is. You need to include <string.h> if you have not already done so, then everything should work.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    not string.h, <string>

    That said, it has me puzzled.

    If the following is acceptable, then logic_error an take a string (or a type that can convert to one).

    throw ( logic_error( string( "connect() failed." ) ) );

    But, this then fails

    string reason( "connect() failed" );
    throw ( logic_error( reason ) ); // this is line 65.

    Which is almost the same thing. logic_error is still getting construced with a string as a parameter.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by SilentStrike
    not string.h, <string>

    That said, it has me puzzled.

    If the following is acceptable, then logic_error an take a string (or a type that can convert to one).

    throw ( logic_error( string( "connect() failed." ) ) );

    But, this then fails

    string reason( "connect() failed" );
    throw ( logic_error( reason ) ); // this is line 65.

    Which is almost the same thing. logic_error is still getting construced with a string as a parameter.
    Ah, silly namespaces Yes, I always was puzzled on why the first way didn't work and an almost identical second did not. The first 3 errors though usually mean the header file was missing.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Maybe 'reason' goes out of scope. check for syntax errors immediately above line 65. If that doesnt work, you might have to post the code inbetween

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    <string> is included. That's why compiler didn't complain at line 64:
    Code:
    string reason( "connect() failed" );
    The only errors I get are the ones in the original post.

    I can't see any difference between the string object created in both the cases. If first one goes out of scope, so should second one also. Isn't it?

    Can anybody try it with Borland and let me know if it is not some strange behaviour on MS VC++ part. Although, I think that the problem is with the code and not compiler.
    Last edited by Ruchikar; 07-09-2002 at 02:03 AM.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The following compiled warningless with all warnings enabled on gcc 2.96 and 3.0.4.

    Code:
    #include <stdexcept>
    #include <string>
    
    using namespace std;
    
    int main() {
    	string reason( "connect() failed" );
    	throw ( logic_error( reason ) );  // this is line 65.
    
    	throw ( logic_error( string( "connect() failed." ) ) );
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    Well, the same code as SilentStrike's, when compiled on MS VC++ gave this:
    Code:
    Compiling...
    test.c
    c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error :  "eh.h is only for C++!"
    Error executing cl.exe.
    
    test.exe - 1 error(s), 0 warning(s)
    Talk about being consistent.

    Is using MS VC++ not that good an idea?
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM