Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中的SETter吸气剂方法 我是C++新手。我将以下类创建到MyWstring中,如下所示:我尝试将对象变量的setter创建为setInto方法。它抱怨没有这样的构造函数。我该怎么办?如何为此创建setter?基本上,我的期望是如何在C中实现类似Java的setter++_C++_Setter_Getter - Fatal编程技术网

C++中的SETter吸气剂方法 我是C++新手。我将以下类创建到MyWstring中,如下所示:我尝试将对象变量的setter创建为setInto方法。它抱怨没有这样的构造函数。我该怎么办?如何为此创建setter?基本上,我的期望是如何在C中实现类似Java的setter++

C++中的SETter吸气剂方法 我是C++新手。我将以下类创建到MyWstring中,如下所示:我尝试将对象变量的setter创建为setInto方法。它抱怨没有这样的构造函数。我该怎么办?如何为此创建setter?基本上,我的期望是如何在C中实现类似Java的setter++,c++,setter,getter,C++,Setter,Getter,进入.h #ifndef INTO_H_ #define INTO_H_ class Into { public: Into(int id1); virtual ~Into(); int id; }; #endif /* INTO_H_ */ Into.cpp #include "Into.h" Into::Into(int id1) { // TODO Auto-generated constructor stub id = id1; } I

进入.h

#ifndef INTO_H_
#define INTO_H_

class Into {
public:
    Into(int id1);
    virtual ~Into();

    int id;
};

#endif /* INTO_H_ */
Into.cpp

#include "Into.h"

Into::Into(int id1) {
    // TODO Auto-generated constructor stub
    id = id1;
}

Into::~Into() {
    // TODO Auto-generated destructor stub
}
MyWstring.h

#ifndef MYWSTRING_H_
#define MYWSTRING_H_

#include<iostream>
#include"Into.h"

using namespace std;

class MyWstring {
public:
    MyWstring(wstring test1);
    virtual ~MyWstring();
    void assign(MyWstring m);
    void setInto(Into into1);

    wstring test;
    Into into;
};

#endif /* MYWSTRING_H_ */

尝试将MyWstring c'tor定义更改为:

    MyWstring::MyWstring(wstring test1)
    :
    into( 0 ),
    test( test1 )
    {

    }
在类MyWstring中有一个类型为的变量,它没有默认的c'tor,因此编译器不能自己实例化它


另外,如果您的MyWSString类在c'tor中接受变量的值,这样您就可以设置一个实际的值,而不是设置一些默认值,这会更好。

您的类有一个实例变量,该实例变量没有默认构造函数,也没有参数

创建MyWstring时,它需要创建Into的实例,但无法创建,因为它不知道如何创建

解决方案1:输入默认构造函数

class Into {
    [...]
    Into() : id(0) { }
};
解决方案2:在MyWSString的初始值设定项列表中提到:

注意初始值设定项列表中分配测试的附加更改。否则,它会得到默认构造,然后分配副本,这可能不是您想要的

如果into没有合理的默认值,您可能需要重新考虑您的逻辑并使用指向into对象的指针,但请确保使用std::unique_ptr或类似的值。

当您构造MyWSString时,编译器将调用您没有的所有基类的构造函数和子对象。如果您不提供参数,它将调用没有参数的构造函数,而您没有参数。你的选择是:

提供默认构造函数:

....
Into(int id1);
Into();
...

Into::Into() : id(0) {}  // Always prefer to initialize rather than assign later
将MyWString::初始化为:


编译器抱怨您没有默认构造函数。 当构造对象而不传递任何参数时,将调用默认构造函数,例如

Into into;
如果您的类是另一个类的成员,也会自动调用默认构造函数

MyWstring::MyWstring(wstring test1): test(test1), into(0) 
{}
如果没有用户声明的构造函数,则自动生成默认构造函数。在您的例子中,构造函数intoid1阻止编译器自动生成默认构造函数

所以你需要的是下面三行中的一行

Into():id(0){};
Into() = default; // C++11 syntax
Into():Into(0){}; // call another constructor from your constructor.
或者,如果您有一个构造函数,其中所有参数都有默认值,编译器将使用该构造函数作为默认构造函数。所以你也可以这样做

Into(int _id = 0) : id(_id){};
如果您不想为您的类使用默认构造函数,那么您需要在另一个类的构造函数上调用非默认构造函数

MyWstring::MyWstring(wstring test1): test(test1), into(0) 
{}

现在进入下一部分,将一个类型为的对象指定给另一个类型相同的对象,这意味着此处正在使用复制指定操作符。您很幸运,因为在您的情况下,编译器会自动生成复制赋值运算符

这实际上不是setter/getter的问题。更好的标题是没有这样的构造函数
Into(int _id = 0) : id(_id){};
MyWstring::MyWstring(wstring test1): test(test1), into(0) 
{}