从构造函数初始化结构
Initializing a struct from a constructor
我正在重新开始编写一些 C++,老实说,我有点生疏了。我觉得如果我知道如何正确地表达我的问题,我会找到一个快速的答案,但我仍然会感谢你的帮助。
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
当我尝试使用 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&’
我不清楚的一件事是为什么 STR_2
的 STR_1 myStr1;
需要首先调用 STR_1
构造函数?我不能用
初始化这两种类型
int main()
{
STR_1 bob = STR_1(5,6);
STR_2 tom = STR_2('Tom',bob);
return 0;
}
谢谢!
除非在对 OP 的评论中 link 还不清楚:这里,
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;
要求STR_1
是默认可构造的。为了解决这个问题,您必须在构造函数的初始化列表中构造成员 STR_1 myStr1;
:
STR_2 (string n, STR_1 s) : name(n), myStr1(s) {}
这会调用编译器生成的 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;
但是,我会出于充分的理由放弃第一个选择。
我正在重新开始编写一些 C++,老实说,我有点生疏了。我觉得如果我知道如何正确地表达我的问题,我会找到一个快速的答案,但我仍然会感谢你的帮助。
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
当我尝试使用 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&’
我不清楚的一件事是为什么 STR_2
的 STR_1 myStr1;
需要首先调用 STR_1
构造函数?我不能用
int main()
{
STR_1 bob = STR_1(5,6);
STR_2 tom = STR_2('Tom',bob);
return 0;
}
谢谢!
除非在对 OP 的评论中 link 还不清楚:这里,
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;
要求STR_1
是默认可构造的。为了解决这个问题,您必须在构造函数的初始化列表中构造成员 STR_1 myStr1;
:
STR_2 (string n, STR_1 s) : name(n), myStr1(s) {}
这会调用编译器生成的 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;
但是,我会出于充分的理由放弃第一个选择。