我已经在网上搜索过,找不到任何解决我问题的方法,希望你能帮我。
因此,我构建了一个抽象基类,并有两个派生类来表示不同的实验。(一个实际上是我的基类的派生类)我在一个单独的头文件中创建了这样的映射,以存储不同类型的实验。
//Map class template to store name of experiment and the experiment within a project
typedef map <string, baseData <double>*> ExpContainer;
void search(string searchName, ExpContainer exps) {
ExpContainer::iterator Iter;
Iter = exps.find(searchName); //finds the entry corresponding to searchName and returns the iterator
if (Iter != exps.end()) { //need this as if entry is not found, the return will be end iter.
cout << "Found the experiment " << Iter->first << "." << endl;
Iter->second->print();
}
else {
cout << "Sorry, experiment " << searchName << " is not found" << endl;
}
}
对于每个实验类型,print()函数都是不同的,我知道存在一个称为切片的问题,所以我已经确保print()在基类中是虚拟的。这是我的基本类:
namespace mynmsp {
//base class of data can be measurements or errors
template < class T> class baseData {
public:
virtual void print() =0;
virtual ~baseData() {
cout << "Destructor called for the base class." << endl;
}
};
}
然后在我的主要。我构建了不同类型的实验,我想打印出来。每个实验类都有不同的打印函数实现,该函数覆盖了基类中的打印函数,如:
void print(){ //do something };
在我的主要。cpp I将映射定义为:
ExpContainer project;
在构建完每个实验之后,我向用户询问实验的名称(expName),并将其插入到项目中,如下所示:
project[expName] = &expC;
我认为插入是好的,因为我测试了项目的大小,它是正确的。
但是,当我的搜索函数按如下方式调用时,发生了运行时错误:
search(name, project);
我不知道切片还是指针有问题?
我试图在每个派生类中使print()成为一个虚拟函数,但这似乎也不起作用。
很抱歉问了这么长的问题,请帮帮我!
编辑:我在do-while循环中构造了实验,而在外部声明了项目。整个代码很长,但其基本内容如下:
string repeatstr; //user input whether to repeat do while loop or not
bool repeat = true; //condition for staying inside do while loop
ExpContainer project; //declared project to store experiments
do {
string expName;
string ans1; //character to store user input
cout << "What is the name of your experiment? " << endl;
cin >> expName;
cout << "Is this a experiment C ? (y/n)" << endl;
cin >> ans1;
if(ans1 =="y"){
//call the constructor for expC
project[expName] = &expC;
}else {
//call the constructor for expB
project[expName] = &expB;
}
cout << "Do you want to enter another experiment? (y/n)" << endl;
cin >> repeatstr;
if (repeatstr == "n") { repeat = false; }
}while (repeat); //loop over this do-while loop while repeat is true
cout << "There are " << project.size() << " in this database." << endl;
//to retrieve info from a certain experiment
string input, name;
cout << "Would you like to retrieve any experiments (y/n)? " << endl;
input = EitherInput("y", "n");
if (input == "y") {
cout << "Please enter the name of the experiment you want to retrieve: " << endl;
cin >> name;
search(name, project); //code breaks down here!
}