class string initialize error !why ?

Write your question here.

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

class dad{

public:
   const string public1("I am a bitch    adsadfadf");

} ming ;

int main()
{
    cout<< ming.public1 << endl ;


}

what's wrong with my code ? just put const string public1("I am a bitch adsadfadf"); into a main() function it will work out okay ?
why in Class ,it always reports error ?
D:\Users\ff\Desktop\C++\PUSS\main.cpp|8|error: expected identifier before string constant|
D:\Users\ff\Desktop\C++\PUSS\main.cpp|8|error: expected ',' or '...' before string constant|
D:\Users\ff\Desktop\C++\PUSS\main.cpp|14|error: no match for 'operator<<' in 'std::cout << ming.dad::public1'|
why ?I am so confused。。
What is ming doing there on line 10?
 What is ming doing there on line 10?

its a global object of type A or more likely a static


@sail456852
you cannot define an object inside a class,
define it in constructor or in other member function.

Last edited on
Line 8: The compiler thinks that is a function declaration that returns a const string.

Try this instead:
8
9
10
11
    dad () : public1 ("I am a bitch    adsadfadf")
    {}
    
    string public1;



> The compiler thinks that is a function declaration that returns a const string.

No, there is no MVP here. ( "I am a bitch adsadfadf" is not the name of a type).

Only a brace-or-equal initializer is allowed for an in-class member initializer.

Any one of these would be fine:
1
2
3
4
5
6
class dad{

public:
   const std::string public1 { "I am a bitch    adsadfadf" } ;

} ming ;


1
2
3
4
5
6
class dad{

public:
   const std::string public1 = "I am a bitch    adsadfadf"  ;

} ming ;


1
2
3
4
5
6
class dad{

public:
   const std::string public1 = { "I am a bitch    adsadfadf" } ;

} ming ;
With C++11 you can initialize class members like this, too

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

class Example {
public:
   const string msg = "Hello world!";

} ex1;

int main() {
    cout<< ex1.msg << "\n";

    return 0;
}


Andy

Last edited on
@Lorence30: Thanks, I learn so much from this site!

@andywestken: I really need to get more into C++11!
@JLBorges
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <string>
using namespace std;

const string public1("I am a bitch1 ");
const string public2="I am a bitch2 ";
const string public3={"I am a bitch3 "};
const string public4 {"I am a bitch4"} ;




int main()
{
    cout<< public1 << endl ;
     cout<< public2 << endl ;
      cout<< public3 << endl ;
       cout<< public4 << endl ;


}

I summarized what I've learned about string initialisation,and tested each form declaration in class and global respectively :
1.All 4 forms work out okay in global declaration
2.Only public1 compilation error in class initialisation ! ....
I wanna know a little deeper about this :
as I know ,string a class itself ....
okay...I should yahoo it .....

Thanks anyway
Hey ,guys ,here comes new question :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <string>
using namespace std;

const string public1("I am a bitch1 ");
const string public2="I am a bitch2 ";
const string public3={"I am a bitch3 "};
const string public4 {"I am a bitch4"} ;




int main()
{
    for(int i=1,i<5,i++)
    cout  << public(i) << endl ;
    


}

How do I output these strings using loop!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <functional>

const std::string one( "one" );
const std::string two = "two" ;
const std::string three { "three" };
const std::string four = { "four" };

template < typename T > void print( const T& v ) { std::cout << v << ' ' ; }

template < typename FIRST, typename... REST > void print( const FIRST& first, const REST&... rest )
{
    print(first) ;
    print(rest...) ;
}

int main()
{
    // copy-elision is possible
    for( const auto& str : { one, two, three, four } ) std::cout << str << ' ' ;
    std::cout << '\n' ;

    // explicitly avoid making copies of the strings (wrap the references)
    for( const std::string& str : { std::cref(one), std::cref(two), std::cref(three), std::cref(four) } )
        std::cout << str << ' ' ;
    std::cout << '\n' ;

    // loop at compile time (variadic template)
    print( one, two, three, four, 12345, 67.89, '\n' ) ;
}

http://coliru.stacked-crooked.com/a/f0496ce7b3c27c08
Topic archived. No new replies allowed.