Visual C++: No default constructor

▼魔方 西西 提交于 2019-12-02 00:44:18
Jarod42

Parsing issue:

Learn(x);

is parsed as

Learn x;

You should use

Learn{x};

to build your temporary or

Learn some_name{x};
//or
Learn some_name(x);

if you want an actual object.

Okay, I figured out the problem I was having. I didn't realize that the call is done as part of an object assignment. The notation in C++ seems to be a bit different that way.

So Learn(x) should be replaced with Learn obj(x) apparently.

This notation is a little off from other programming languages where you can usually write className(inputs) variableName.

I had similar code and came up with a different errors because I was compiling with Linux in a terminal. My errors were:

error: conflicting declaration 'myObject str' 
error: 'str' has a previous declaration as 'const string  str'

when I tried myObject(str);

Sure Learn{x}; (and in my case myObject{str};) will successfully compile, but it creates a temporary object, then destroys it in the same line.

This is not what I wanted to do. So an alternative solution is to create a new object like:

Learn var_name = new Learn(x);

This way you can reference it for future use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!