先週、クラスとベクトルを使用して文字列のセットを管理するプログラムを作成しました。私はこれを100%完成させることができました。今週は、クラス内の文字列を格納するために使用したベクトルを、単純な単一リンクリストに置き換える必要があります。単一リンクリストの非常に基本的な実装のためのコピーコンストラクタに関する助けが必要
この関数は、基本的にユーザーが空の文字列のセットを宣言することを可能にし、1つの要素のみで設定します。メインファイルには、要素がsetNameとstrSet(クラス)を含む構造体であるベクトルがあります。
私の問題です:クラスのコピーコンストラクタを扱います。コピーコンストラクタを削除/コメントアウトするとき、空のセットまたは単一のセットを必要なだけ宣言し、問題なく値を出力できます。しかし、私は明らかに私がプログラムの残りの部分を実装するときのコピーコンストラクタが必要になることは知っています。コピーコンストラクタを残しておくと、1つまたは複数のセットを宣言してその値を出力できます。しかし、2番目のセットを宣言し、最初の2つのセットのどちらかを出力しようとすると、セグメンテーションフォルトが発生します。さらに、2セット以上宣言しようとすると、セグメンテーションフォルトが発生します。どんな助けもありがとう!ここで
は、すべての非常に基本的な実装のための私のコードです:ここでは
はsetcalc.cppです:(メインファイル)ここで
#include <iostream>
#include <cctype>
#include <cstring>
#include <string>
#include "strset2.h"
using namespace std;
// Declares of structure to hold all the sets defined
struct setsOfStr {
string nameOfSet;
strSet stringSet;
};
// Checks if the set name inputted is unique
bool isSetNameUnique(vector<setsOfStr> strSetArr, string setName) {
for(unsigned int i = 0; i < strSetArr.size(); i++) {
if(strSetArr[i].nameOfSet == setName) {
return false;
}
}
return true;
}
int main() {
char commandChoice;
// Declares a vector with our declared structure as the type
vector<setsOfStr> strSetVec;
string setName;
string singleEle;
// Sets a loop that will constantly ask for a command until 'q' is typed
while (1) {
cin >> commandChoice;
// declaring a set to be empty
if(commandChoice == 'd') {
cin >> setName;
// Check that the set name inputted is unique
if (isSetNameUnique(strSetVec, setName) == true) {
strSet emptyStrSet;
setsOfStr set1;
set1.nameOfSet = setName;
set1.stringSet = emptyStrSet;
strSetVec.push_back(set1);
}
else {
cerr << "ERROR: Re-declaration of set '" << setName << "'\n";
}
}
// declaring a set to be a singleton
else if(commandChoice == 's') {
cin >> setName;
cin >> singleEle;
// Check that the set name inputted is unique
if (isSetNameUnique(strSetVec, setName) == true) {
strSet singleStrSet(singleEle);
setsOfStr set2;
set2.nameOfSet = setName;
set2.stringSet = singleStrSet;
strSetVec.push_back(set2);
}
else {
cerr << "ERROR: Re-declaration of set '" << setName << "'\n";
}
}
// using the output function
else if(commandChoice == 'o') {
cin >> setName;
if(isSetNameUnique(strSetVec, setName) == false) {
// loop through until the set name is matched and call output on its strSet
for(unsigned int k = 0; k < strSetVec.size(); k++) {
if(strSetVec[k].nameOfSet == setName) {
(strSetVec[k].stringSet).output();
}
}
}
else {
cerr << "ERROR: No such set '" << setName << "'\n";
}
}
// quitting
else if(commandChoice == 'q') {
break;
}
else {
cerr << "ERROR: Ignoring bad command: '" << commandChoice << "'\n";
}
}
return 0;
}
strSet2.hさ:
#ifndef _STRSET_
#define _STRSET_
#include <iostream>
#include <vector>
#include <string>
struct node {
std::string s1;
node * next;
};
class strSet {
private:
node * first;
public:
strSet(); // Create empty set
strSet (std::string s); // Create singleton set
strSet (const strSet ©); // Copy constructor
// will implement destructor and overloaded assignment operator later
void output() const;
}; // End of strSet class
#endif // _STRSET_
ここにstrSet2.cpp(claの実装SS)
#include <iostream>
#include <vector>
#include <string>
#include "strset2.h"
using namespace std;
strSet::strSet() {
first = NULL;
}
strSet::strSet(string s) {
node *temp;
temp = new node;
temp->s1 = s;
temp->next = NULL;
first = temp;
}
strSet::strSet(const strSet& copy) {
cout << "copy-cst\n";
node *n = copy.first;
node *prev = NULL;
while (n) {
node *newNode = new node;
newNode->s1 = n->s1;
newNode->next = NULL;
if (prev) {
prev->next = newNode;
}
else {
first = newNode;
}
prev = newNode;
n = n->next;
}
}
void strSet::output() const {
if(first == NULL) {
cout << "Empty set\n";
}
else {
node *temp;
temp = first;
while(1) {
cout << temp->s1 << endl;
if(temp->next == NULL) break;
temp = temp->next;
}
}
}
関数がコンストラクタであるため、 'first'はこの関数が呼び出される前に既に何かを指していません。 – aschepler
はい、そのコンストラクタです。 – Tesla