【问题标题】:is implicitly deleted because the default definition would be ill-formed:被隐式删除,因为默认定义格式不正确:
【发布时间】:2018-04-13 23:35:52
【问题描述】:

我正在创建一个项目,它读取文件并使用文件中的数据做一些事情:

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:在函数“int main()”中: main.cpp:19:67:错误:使用已删除的函数‘NameSurferDataBase::NameSurferDataBase(NameSurferDataBase&&)’ NameSurferDataBase namesdb = NameSurferDataBase("NamesData.txt"); ^ 在 main.cpp:5:0 包含的文件中: NameSurferDataBase.h:11:7: 注意:‘NameSurferDataBase::NameSurferDataBase(NameSurferDataBase&&)’被隐式删除,因为默认定义格式不正确: 类 NameSurferDataBase { ^~~~~~~~~~~~~~~~~~ NameSurferDataBase.h:11:7: 错误:从“linked_list”类型的右值初始化“linked_list&”类型的非常量引用无效

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

【问题讨论】:

  • 附带说明,.cpp 文件不需要标头保护,只有 .h 文件需要。如果Search() 返回false,则findEntry() 的返回值未定义。如果在找不到name的情况下不想返回NameSurferEntry对象,请将返回类型更改为NameSurferEntry*并返回nullptr,或者抛出异常。

标签: c++ c++11


【解决方案1】:

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

直接构造namesdb,它应该可以工作,例如:

NameSurferDataBase namesdb("NamesData.txt");

【讨论】:

  • 当我这样做时,它消除了两个错误,但现在完全抛出了一个不同的错误。
  • 那么您的代码中有另一个错误,应该发布一个关于该错误的单独问题。关于格式错误的构造函数的问题已经得到解答。请每个帖子一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 2022-01-22
相关资源
最近更新 更多