创建类的空实例
Creating an empty instance of a class
好的,我有一个类Set
,他保留vector<int>
作为其数据有效载荷。它有一个构造函数,它接受string
作为参数,例如Set test = Set("1 2 3 4 5 6");
我有一个读取行的函数,并将其解析为vector<int>
从那里我可以对Set
执行操作。当调用Set test = Set("");
时,问题就来了,我的构造函数无法进行Set
,因为它没有什么可解析的。我的程序无处可去。我尝试在构造函数中放置 if else 语句,但如何声明空set
?
现在我遇到了一个分段错误。
#include "Set.h"
using namespace std;
/***************************************************************************************
* Constructors and Destructors
**/
Set::Set(){
}
Set::Set(string inputString) {
if(inputString != ""){
readLine(inputString);
}
else{
//I've tried several things here, none of which work.
}
}
Set::~Set(){
}
/***************************************************************************************
* readLine function
*
* This function takes in a string, takes the next int, and stores in in a vector.
*
* Parameters: string, the string to be parsed.
*
**/
void Set::readLine(string inString){
ScanLine scanLine;
scanLine.openString(inString);
while(scanLine.hasMoreData()){
addToSet(scanLine.nextInt());
}
}
/***************************************************************************************
* addToSet function
*
* This function takes in a int that is an element and adds it to "this" Set.
*
* Parameters: int, the element to be added.
* Return: int, the value that was added.
*
**/
int Set::addToSet(int element){
int returnValue = -1;
if(!containsElement(element)){
this->theSet.push_back(element);
returnValue = element;
}
return returnValue;
}
我找到了解决方案。在我的等式函数中,我if(!set1.size() == set2.size())
我应该有if(set1.size() != set2.size())
,由于某种原因导致了分割错误。
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 在C++中为链表类创建实例
- 如何为类的每个实例创建特定实例的方法
- 基于现有实例创建类的新实例
- 从模板类的另一个实例创建模板类的实例时省略模板参数
- Vulkan-加载扩展时实例创建失败
- 通知B1或B2或B3类的特定实例创建Pointer A类
- 使用std::shared_ptr对象实例创建boost::线程
- 如何在C++中仅包含一个实例创建类
- 如何从旧实例创建新实例
- 在V8 javascript引擎中,如何为每个实例创建一个重新使用ObjectTemplate的构造函数
- 有没有一种方法可以从基本实例创建派生实例
- 如何在修改设计时停止现有代码基类的实例创建
- 使用基类实例创建派生类实例
- 如何从另一个仅静态选择满足特定类型规则的索引的元组实例创建元组实例