被隐式删除,因为默认定义不正确:

时间:2018-04-13 23:35:53

标签: c++ c++11

我正在创建一个项目,该项目读取文件并对文件中的数据执行一些操作:

NameSurferDataBase.cpp

#ifndef NAMESURFERDATABASE_CPP
#define NAMESURFERDATABASE_CPP
#include "NameSurferDataBase.h"
#include "linked_list.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <ostream>
#include <sstream>
#include <iomanip>
#include <istream>
#include <cstdio>

using namespace std;

NameSurferDataBase::NameSurferDataBase(string filename){
    getNameData(filename);
}

void NameSurferDataBase::getNameData(string filename){
    ifstream input;
    input.open(filename);
    if(!input.is_open()){
        cout << "Not Open";
    }else{
        string temp;
        while(input.is_good()){
            getline(input,temp);
            if(!input.eof()){
                NameSurferEntry entry(temp);
                database.InsertInOrder(entry);
            }
        }
    }
}

NameSurferEntry NameSurferDataBase::findEntry(string name){
    NameSurferEntry temp(name);
    if(database.Search(temp) == true){
        return temp;
   } else{
       cout << "Name not found" << endl;
   }
}
#endif

NameSurferDataBase.h

#ifndef NAMESURFERDATABASE_H
#define NAMESURFERDATABASE_H
#include "NameSurferEntry.h"
#include "linked_list.h"
#include <iostream>
#include <list>
#include <string>

class NameSurferDataBase {
public:
    NameSurferDataBase(string filename);
    void getNameData(string filename);
    NameSurferEntry findEntry(string name);
private:
    linked_list<NameSurferEntry> database;
};
#endif

每当我尝试编译时,我在main代码中都会在此行中收到以下错误:

NameSurferDataBase namesdb = NameSurferDataBase("NamesData.txt");
main.cpp: In function ‘int main()’:
main.cpp:19:67: error: use of deleted function ‘NameSurferDataBase::NameSurferDataBase(NameSurferDataBase&&)’
    NameSurferDataBase namesdb = NameSurferDataBase("NamesData.txt");
                                                                       ^
In file included from main.cpp:5:0:
NameSurferDataBase.h:11:7: note: ‘NameSurferDataBase::NameSurferDataBase(NameSurferDataBase&&)’ is implicitly deleted because the default definition would be ill-formed:
 class NameSurferDataBase {
       ^~~~~~~~~~~~~~~~~~
NameSurferDataBase.h:11:7: error: invalid initialization of non-const reference of type ‘linked_list<NameSurferEntry>&’ from an rvalue of type ‘linked_list<NameSurferEntry>’

为什么说我的构造函数不正确?

1 个答案:

答案 0 :(得分:3)

您正构建一个临时NameSurferDataBase并将其移至namesdb。由于您使用了linked_list,因此无法生成默认移动构造函数,这可能会禁止移动。

直接构造namesdb,它应该有用,例如:

NameSurferDataBase namesdb("NamesData.txt");