-1

A class from a library that takes string needs to be initialized in my own class. Trying to do something like this, but gives me the error: expected identifier before string constant

Class MyClass{
 public:

  ClassA obj("some string");

}

How can I properly do this?

Tim
  • 47
  • 7
  • 2
    Did you try initializing it in a constructor of your class? – Algirdas Preidžius Jul 20 '20 at 19:29
  • 1
    [This question](https://stackoverflow.com/questions/1272680/what-does-a-colon-following-a-c-constructor-name-do) and [this question](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) will probably be informative. – WhozCraig Jul 20 '20 at 19:38

1 Answers1

2

Simplest thing is to do the initialization in the MyClass constructor, e.g.

class MyClass {
public:
    MyClass() : obj("some string") {}
    ClassA obj;
};
john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks for the example. However, I got the following error: error: template argument 1 is invalid. The error only happens when I initialize it in the class header file, and no error if I initialize it in the source file. – Tim Jul 20 '20 at 20:24
  • There are no templates the code you posted. So I have no idea how you got that error. Please post real code. It's risky to say 'something like this' as you may miss out important information (as it seems you have). – john Jul 20 '20 at 20:30