20

Having a program like this:

#include <iostream>
#include <string>
using namespace std;
class test
{
public:
    test(std::string s):str(s){};
private:
    std::string str;
};

class test1
{
public:
    test tst_("Hi");
};

int main()
{
    return 1;
}

…why am I getting the following when I execute

g++ main.cpp

main.cpp:16:12: error: expected identifier before string constant
main.cpp:16:12: error: expected ‘,’ or ‘...’ before string constant
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
rahman
  • 4,820
  • 16
  • 52
  • 86

2 Answers2

25

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;
}
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • *"You can not initialize `tst_` where you declare it. This can only be done for `static const` primitive types."* -- out of curiosity, "why not?" I mean what could possibly go wrong if it was possible to initialize `tst_` where it was declared? Is it to avoid some possible bad behavior? Thanks a lot in advance! – Milan Jul 15 '22 at 18:33
  • I can't give an exact reason for that as I am not the creator of the language. I don't think something bad would happen but probably the implementation (of the language) was easier this way – Ivaylo Strandjev Jul 15 '22 at 20:35
1

There is another and more simplified way of doing what you want:Just change your statement from test tst_("Hi"); to test tst_{"Hi"}; and it will work. Below is the modified code and it works as expected.

#include <iostream>
#include <string>
using namespace std;
class test
{
public:
    test(std::string s):str(s){cout<<"str is: "<<s;}
private:
    std::string str;
};

class test1
{
public:
    test tst_{"Hi"};
};

int main()
{   test1 obj;
    return 0;
}

Note that i have just changed test tst_("Hi"); to test tst_{"Hi"}; and everything else is exactly the same. Just for confirmation that this works i have added one cout to check that it initialize the str variable correctly. I think this one line solution is more elegant(at least to me) and and up to date with the new standard.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Why exactly `test tst_{"Hi"}` works but `test tst_("Hi")` does not? Could you please elaborate? Thanks a lot in advance! – Milan Jul 15 '22 at 19:10
  • @Milan Sure. The problem is that in C++11 when using in-class initializers we can use `{}` but not `()`. The same is explained here in more detail: [Why C++11 in-class initializer cannot use parentheses?](https://stackoverflow.com/questions/24836526/why-c11-in-class-initializer-cannot-use-parentheses). Note also that you can find many other SO posts explaining the same. – Jason Jul 16 '22 at 05:16