basic string scope in std namespace
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: basic string scope in std namespace

  1. #1
    Join Date
    Jul 2007
    Posts
    16

    basic string scope in std namespace

    I have a simple script but I am missing how vars are used. I get the compile error "error: ‘the_string’ was not declared in this scope" on the line that has " cout << the_string;"

    Whats the correct way to access "the_string".


    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        bool test1 = false;
        if(test1)
        {
            string the_string("its true");
        }else{
            string the_string("its false");
        }
    
        cout << the_string;
        return 0;
    }

  2. #2
    Join Date
    Aug 2008
    Posts
    112

    Re: basic string scope in std namespace

    And again your program means its false. I am sad :sad:
    hi,,,

  3. #3
    Join Date
    Jul 2007
    Posts
    16

    Re: basic string scope in std namespace

    Quote Originally Posted by Khiem View Post
    And again your program means its false. I am sad :sad:
    umm ok?


    If anyone has a real answer. I assume I am doing this the wrong way. Basically I am playing with cURL. I don't have a lot of experience with C++ as its my first project. I need to store the return data in a var. I don't know the best way to store the buffer since its going to be a variable length. My programing experience is all high level stuff like PHP. I have seen examples using the string lib but my use of it must not be right (or maybe g++ issue). Bottom line i'm confused why a var defined inside an if statement is outside the scope after it. I assume its the use of the string lib or the namespace.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: basic string scope in std namespace

    Quote Originally Posted by ryanmills View Post
    Bottom line i'm confused why a var defined inside an if statement is outside the scope after it.
    It shouldn't be. The compiler is telling you this. The variable the_string must exist at the highest level at which it will be used.

    However, it may be *modified* at any level you like under that. I'd change it to:

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        bool test1 = false;
        string the_string;
        if(test1)
        {
            the_string = "its true";
        }else{
            the_string = "its false";
        }
    
        cout << the_string;
        return 0;
    }

  5. #5
    Join Date
    Jul 2007
    Posts
    16

    Re: basic string scope in std namespace

    I guess i am not thinking about scope the right way but your example works. But I do have another question. Is there a risk of a buffer over run if you reset the "the_string" and whats the char limit for string?

    example code:
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        string the_string;
    
        bool test1 = false;
    
        if(test1)
        {
            the_string = "its true";
        }else{
            the_string = "its false";
        }
    
        the_string = "this is a longer string to test for a buffer overrun";
    
        cout << the_string;
        return 0;
    }

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: basic string scope in std namespace

    There is no limit* to the length of a string a string object can store. The string object will re-allocate the memory it stores internally if it needs to, to accomodate the longer string you assign to it.

    *OK, there is a limit, but it's on the order of 2 billion, so for all practical purposes, there is no limit
    Old Unix programmers never die, they just mv to /dev/null

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: basic string scope in std namespace

    No, the std::string class was created specifically to avoid buffer overruns being a possibility during normal operation (it's possible to screw them up, but they make it pretty difficult).

    Its size is limited only by available memory and/or address space.

  8. #8
    Join Date
    Jul 2007
    Posts
    16

    Re: basic string scope in std namespace

    And now what maybe a complex question. Is this the best way to store data as a "buffer" in the context of content returned from a cURL call?

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: basic string scope in std namespace

    I'm not familiar with cURL. However, there are two questions you need to ask:

    1) Will this data ever need to be passed to a function expecting a (non-const) char*?
    2) Is it possible to have a 0 byte prior to the end of the data? (I don't mean the character '0', I mean the NULL byte.)

    If the answer to either of these is yes, you'll be better off with a std::vector<char>. Otherwise a std::string should be just fine.

  10. #10
    Join Date
    Jul 2007
    Posts
    16

    Re: basic string scope in std namespace

    I think I need to get my head wrapped around strings better first. I thought I had a grasp on what * was used for. But I am seeing it used in contexts I don't understand. I can see now why people pick python over C, but I really want to get a handle on this.

    Example that I get:
    Code:
    a = '6';
    b = &a;  // points to the mem address
    c = *b; 
    cout << c; // should return 6
    But then I see this for example, does this mean its passing a reference?

    Code:
    int main(int argc, char* argv[])
    Also when you say "expecting a (non-const) char*" do you mean the function is expecting a pointer and not the real content. Such as my example, its expecting the var c not a?
    Last edited by ryanmills; May 31st, 2009 at 01:26 AM.

  11. #11
    Join Date
    Apr 2008
    Posts
    726

    Re: basic string scope in std namespace

    there is a difference - it depends on whether you are declaring a var, or using a var.

    e.g.:
    Code:
    int* a = new int; // declares a pointer to an int, and sets it pointing to some uninitialized int memory
    int& b = a;  // declares b as a reference, and sets reference to a;
    
    
    int c;
    c = *a; // here * is dereferencing a.  c = <value pointed at by a>
    
    int* d;
    d = &c; //  the pointer d is given the address of c to point to.

    Hopefully you can now tell what the below means:
    Code:
    int main(int argc, char* argv[])
    main expects an int, and a pointer to a char ( the [] means it actually expects there to be more than one char* in a contiguous block of memory )
    Last edited by Amleto; May 31st, 2009 at 05:29 AM.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: basic string scope in std namespace

    char* argv[] is simply an array of C-style strings corresponding to command-line arguments. You'll sometimes see the parameter declared as char ** argv, which is equivalent.

    At the lowest level---where C lives---any string is just an array of characters. Hence the type is char*. However, classes such as std::string were creating specifically so that you wouldn't have to worry about such details. Consider the progression:

    Array of char arrays: char**
    Array of std::strings: std::string*
    std::vector of std::strings: std::vector<std::string>

    At each step we wrap a C++ class around a pointer, allowing us not to worry about the specifics of dealing with pointers. That's not to say that the three types are 100&#37; compatible in all cases, but they're essentially similar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center