[英]Initializing a struct from a constructor
I'm getting back into writing some C++ and I'm honestly rusty. 我回到写一些C ++的过程中,老实说,我很生锈。 I get the feeling I'd find a quick answer to my question if I just knew how to phrase it properly, but still I'd appreciate your help.
如果我只是知道如何正确地表达它的话,我会很快找到我的问题的答案,但是我仍然感谢您的帮助。
sanitycheck.cpp: sanitycheck.cpp:
#include <string>
using namespace std;
typedef struct STR_1 {
int val_a, val_b;
STR_1 (int a, int b)
{ val_a = a; val_b = b; }
} STR_1;
typedef struct STR_2{
string name;
STR_1 myStr1;
STR_2 (string n, STR_1 s)
{ name=n; myStr1 = s; }
} STR_2;
int main(){
return 0;
} // end main
When I try to compile with g++ -o sanitycheck ./test/sanitycheck.cpp
I get the following, 当我尝试使用
g++ -o sanitycheck ./test/sanitycheck.cpp
进行编译时,得到以下信息:
./test/sanitytest.cpp: In constructor ‘STR_2::STR_2(std::string, STR_1)’:
./test/sanitytest.cpp:25:3: error: no matching function for call to ‘STR_1::STR_1()’
{ name=name; myStr1 = &s; }
^
./test/sanitytest.cpp:25:3: note: candidates are:
./test/sanitytest.cpp:11:3: note: STR_1::STR_1(int*, int*)
STR_1 (int *a, int *b)
^
./test/sanitytest.cpp:11:3: note: candidate expects 2 arguments, 0 provided
./test/sanitytest.cpp:7:16: note: STR_1::STR_1(const STR_1&)
typedef struct STR_1 {
^
./test/sanitytest.cpp:7:16: note: candidate expects 1 argument, 0 provided
./test/sanitytest.cpp:25:23: error: no match for ‘operator=’ (operand types are ‘STR_1’ and ‘STR_1*’)
{ name=name; myStr1 = &s; }
^
./test/sanitytest.cpp:25:23: note: candidate is:
./test/sanitytest.cpp:7:16: note: STR_1& STR_1::operator=(const STR_1&)
typedef struct STR_1 {
^
./test/sanitytest.cpp:7:16: note: no known conversion for argument 1 from ‘STR_1*’ to ‘const STR_1&’
One thing I'm not clear on is why would STR_1 myStr1;
我不清楚的一件事是为什么
STR_1 myStr1;
为什么STR_1 myStr1;
of STR_2
need to call the STR_1
constructor in the first place? 的
STR_2
需要调用STR_1
构造函数吗? Couldn't I initialize both types with, 我不能用这两种类型初始化
int main()
{
STR_1 bob = STR_1(5,6);
STR_2 tom = STR_2('Tom',bob);
return 0;
}
Thanks! 谢谢!
Unless it is not already clear from the link in the comments to the OP: this here, 除非从OP的注释链接中不清楚,否则:
typedef struct STR_2{
string name;
STR_1 myStr1;
STR_2 (string n, STR_1 s) // here the myStr1 default constructor is called
{ name=name; myStr1 = s; }
} STR_2;
requires STR_1
to be default constructible. 要求
STR_1
是默认可构造的。 In order to work around, you have to construct the member STR_1 myStr1;
为了变通,您必须构造成员
STR_1 myStr1;
in the constructor's initializer list: 在构造函数的初始化列表中:
STR_2 (string n, STR_1 s) : name(n), myStr1(s) {}
This calls the compiler generated copy-constructor of STR_1
instead of the default constructor (the automatic generation of which is suppressed by providing a custom constructor). 这将调用编译器生成的
STR_1
副本构造函数,而不是默认的构造函数(通过提供自定义构造函数来抑制其自动生成)。
Another option would be to use a pointer to STR_1
: 另一种选择是使用指向
STR_1
的指针:
typedef struct STR_2{
string name;
std::unique_ptr<STR_1> myStr1;
STR_2 (string n, STR_1 s)
{ name=name; myStr1 = std::make_unique<STR_1>(s); } //just for the sake of explanation
//again, this would be better
//done in the initializer list
} STR_2;
Yet, I would depart from the first alternative only for a good reason. 然而,仅出于充分的理由,我才选择第一种选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.