Working with strings

Hey all I'm trying to get a handle on the string class and how to use it properly. I've written the following code and don't understand why line 8 doesn't work to initialize the string but lines 7 and 9 do. Also, why can't I use the operator .length() in line 12 to return the size of my_string3?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string my_string ("This works");
    string my_string2 ("This", "Doesn't", "work"); // why doesn't this work?
    string my_string3 [3] = {"This", "also works"};
    cout<<my_string.length();
    cout<<my_string2.length();  //return length of second string 
    cout<<my_string3.length(); //why does this return an error saying its unable to
    //resolve identifier length?
    return 0;
    
}
string my_string2 ("This", "Doesn't", "work"); // why doesn't this work?
Because there is no string constructor that takes three pointers to char.

1
2
   cout<<my_string3.length(); //why does this return an error saying its unable to
    //resolve identifier length? 

my_string3 is not a string. my_string3 is an array of strings.

1
2
    cout << my_string3[0].length() ;
    cout << my_string3[1].length() ;
Topic archived. No new replies allowed.