expected identifier before string constant

You can not initialize tst_ where you declare it. This can only be done for static const primitive types. Instead you will need to have a constructor for class test1.

EDIT: below, you will see a working example I did in ideone.com. Note a few changes I did. First, it is better to have the constructor of test take a const reference to string to avoid copying. Second, if the program succeeds you should return 0 not 1 (with return 1 you get a runtime error in ideone).

#include <iostream>
#include <string>
using namespace std;
class test
{
public:
    test(const std::string& s):str(s){};
private:
    std::string str;
};
 
class test1
{
public:
    test1() : tst_("Hi") {}
    test tst_;
};
 
int main()
{
    return 0;
}

Leave a Comment