Problem in string assignment

This is a discussion on Problem in string assignment within the C++ Programming forums, part of the General Programming Boards category; Hi, I am facing a problem in assigning a '0' value to string in header file declaration. I have a ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Problem in string assignment

    Hi,
    I am facing a problem in assigning a '0' value to string in header file declaration.

    I have a header file A.h

    Code:
    //A.h 
    
    void show(const string s& = 0,int = 0 );
    but here compiler is giving error as :

    error: default argument for parameter of type 'const string&' has type 'int'
    I am using gcc version 4.2.1 . I have also used NULL but the result were same.

    Where is the problem ??

    Can any body help ??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,775
    I think you want to write:
    Code:
    void show(const string s& = "", int x = 0);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A good default value for an empty string is "" or std::string(). The second is better: it is the string's default constructor that creates an empty string.

    As a side note, you can technically pass 0 (NULL) to string(const char*) constructor, but it will not work as string expects the pointer to be valid and pointing to a null-terminated char array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks for help......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 04:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21