If you use
Label label = new Label ("name", "componentName");
the object is allocated on the heap, which you have to either delete
, or wrap a smart pointer around it, or else you get a memory leak.
If you do
Label label {"name", "componentName"};
the object is allocated on the stack. You don’t have to free it. It is a “classic” variable if you will. (Without going into too much technical details)
Furthermore a header file serves for declaring a function, a cpp file for implementing said function. You include the header file in other files, in order for the program to know the function signature.
Now if you were a compiler what would you expect from a line like this: String somtehing(...)
inside a header file? Yes a function declaration. That’s why you gen en error. If you want to allocate your member variable on the stack and do said thing inside a header file, you need to use braces {}
(which can also lead to some problems, because if the class defines an array initialization constructor, it is prefered instead of the standard constructor), or you have to do
Label label = Label ("name", "componentName");
In many cases it is “advised” to use braces for initialization over parantheses, but as I mentioned, this can also cause some problems.
Another question would be whether you really need to initialize these member variables inside the header file, or if it would be better to do so in the constructor of a class for example. But this is a question only you can answer.
EDIT 1:
Just a tip. use
```
code
````
for codeblocks and `code` for inline code fragments to make your posts well formatted.