Thread: std::string declaration in header file error

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    4

    std::string declaration in header file error

    Hi, I need your help a bit in C++.
    I am coding a dll but my source code has 2 errors.
    Can you please help?

    Both issues stem from the same declaration in my header file.

    Code:
    void execute_assert(bool expr, std::string str[]);
    When I call the function:

    Code:
    v_assert(s >= MAX_ACL_DEPTH, "MAX_ACL_DEPTH too small");
    It gives the error.

    Code:
    Error: cannot convert ‘const char*’ to ‘std::string* {aka std::basic_string<char>*}’ for argument ‘2’ to ‘void execute_assert(bool, std::string*)’ 
    The second function call goes like this:

    Code:
    v_assert(s < 1, "Illegal acl size (" + deb + ")");
    And it gives the error.

    Code:
    Error: cannot convert ‘std::basic_string<char>’ to ‘std::string* {aka std::basic_string<char>*}’ for argument ‘2’ to ‘void execute_assert(bool, std::string*)’ 
    How do I fix this?

    Thanks.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Remove the square brackets [] in the parameter list. You're telling the compiler you intend to pass a string array in, but you aren't using the function that way at all.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    > void execute_assert(bool expr, std::string str[]);
    So why would you want to pass an array of strings ?

    It seems to me like some hack job converted
    void execute_assert(bool expr, char str[]);
    to
    void execute_assert(bool expr, std::string str[]);
    without thinking about the consequences.

    Removing the square brackets should fix the first problem.

    > v_assert(s < 1, "Illegal acl size (" + deb + ")");
    What is deb?
    If it isn't a char array, or a char pointer, you might be SoL.

    What you might need to do, to force the right thing to happen with the + (ie, string concatenation), is something like
    v_assert(s < 1, string("Illegal acl size (") + deb + ")");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2016
    Posts
    4
    Removed the square brackets.

    and the more complete code including deb is:

    Code:
    #ifdef DEBUG
        std::string deb;
    #endif
        // Test parameter s
        v_assert(s >= MAX_ACL_DEPTH, "MAX_ACL_DEPTH too small");
    #ifdef DEBUG
        deb=s;
        v_assert(s < 1, "Illegal acl size (" + deb + ")");
    Ok, now the errors are.

    Code:
    Error: cannot convert ‘const char*’ to ‘std::string* {aka std::basic_string<char>*}’ for argument ‘2’ to ‘void execute_assert(bool, std::string*)’
    AND

    Code:
    Error: cannot convert ‘std::basic_string<char>’ to ‘std::string* {aka std::basic_string<char>*}’ for argument ‘2’ to ‘void execute_assert(bool, std::string*)’
    consecutively.

    Code:
    v_assert(s < 1, std::string("Illegal acl size (") + deb + ")");
    Does not work either.


    Any ideas?
    Last edited by loumbut5; 06-29-2016 at 06:34 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    > Removed the square brackets.
    Then why does the error message say that you haven't?

    Error: cannot convert ‘const char*’ to ‘std::string*
    Error: cannot convert ‘std::basic_string<char>’ to ‘std::string*

    That * at the end of std::string means the compiler still thinks there is a square bracket pair at the end of your parameter declaration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2016
    Posts
    4
    Sorry noob mistake, forgot to save, kind of on a schedule.

    New error is:

    Code:
    Error: too many arguments to function ‘void execute_assert(bool, std::string)’

    on both counts.

    What's the fix?

  7. #7
    Registered User
    Join Date
    Jun 2016
    Posts
    4
    I was making the wrong function call.

    Code:
    v_assert(s = MAX_ACL_DEPTH, "MAX_ACL_DEPTH too small");
    should have been

    Code:
    execute_assert(s = MAX_ACL_DEPTH, "MAX_ACL_DEPTH too small");
    Problem fixed.

    Thanks all.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,911
    Just in case, are you sure you ended up with:
    Code:
    execute_assert(s = MAX_ACL_DEPTH, "MAX_ACL_DEPTH too small");
    ? It looks like you have an assignment when you originally had a comparison. Perhaps you should have gone with something closer to your original:
    Code:
    execute_assert(s >= MAX_ACL_DEPTH, "MAX_ACL_DEPTH too small");
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redefinition error with repititions of header file declaration
    By shrink_tubing in forum C++ Programming
    Replies: 5
    Last Post: 08-02-2010, 12:07 AM
  2. error: ISO C++ forbids declaration of 'string' with no type
    By TheBigOnion in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2010, 06:33 AM
  3. New to C++, Header File Error
    By kspill2 in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2007, 02:34 PM
  4. header file error
    By beon in forum C Programming
    Replies: 7
    Last Post: 11-16-2006, 07:33 AM
  5. String header file
    By Stan100 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 01:26 PM

Tags for this Thread