【问题标题】:What is a deleted function, and why only my functions that I pass files into are considered deleted? [duplicate]什么是已删除函数,为什么只有我将文件传入的函数才被视为已删除? [复制]
【发布时间】:2017-02-18 23:46:42
【问题描述】:
#include <bits/stdc++.h>
using namespace std;

class contact {
private:
    vector< pair<string, int> > contact_info;
public:
    void add_contact(string contact_name, int contact_number) {
        contact_info.push_back(make_pair(contact_name, contact_number));
        sort(contact_info.begin(),contact_info.end());
    }
    void edit_contact(string contact_name) {
        int found_at;
        for (unsigned int i =0; i < contact_info.size(); i++) {
            if (contact_info[i].first == contact_name) {
                found_at = i;
            }
    }
    if (contact_info[found_at +1].first == contact_name) {
        int choice;
        int counter = found_at;
        int index = 1;
        while (contact_info[counter].first == contact_name) {
            cout << index << ". " << contact_info[counter].first << " " << contact_info[counter].second;
            counter++;
            index++;
        }
        cout << "Choose any please: ";
        cin >> choice;
        found_at = found_at - (choice - 1);
    }
    cout << "Enter the new number: ";
    cin >> contact_info[found_at].second;
}
void show_all() {
    for (unsigned int i =0; i < contact_info.size(); i++) {
        cout << contact_info[i].first << " " << contact_info[i].second << endl;
    }
}
void delete_contact(string contact_name) {
    int found_at;
    for (unsigned int i =0; i < contact_info.size(); i++) {
        if (contact_info[i].first == contact_name) {
            found_at = i;
        }
    }
    if (contact_info[found_at +1].first == contact_name) {
        int choice;
        int counter = found_at;
        int index = 1;
        while (contact_info[counter].first == contact_name) {
            cout << index << ". " << contact_info[counter].first << " " << contact_info[counter].second;
            counter++;
            index++;
        }
        cout << "Choose any please: ";
        cin >> choice;
        found_at = found_at - (choice - 1);
    }
    contact_info.erase(contact_info.begin()+found_at);
}
void writeFile(ofstream contact_file) {
    for (unsigned int i =0; i < contact_info.size(); i++) {
        contact_file << contact_info[i].first << " " << contact_info[i].second << endl;
    }

}
void readFile(ifstream contact_file) {
    string input;
    while (!contact_file.eof()) {
    contact_file >> input;
    size_t pos = input.find(" ");
    string name = input.substr(0,pos);
    string number_str = input.substr(pos);
    int number = stoi(number_str) ;
    contact_info.push_back(make_pair(name,number));
    }
}

};

int main()
{
    int choice;
    ifstream contacts_file_read;
    contacts_file_read.open("contacts.txt");
    ofstream contacts_file_write;
    contacts_file_write.open("contacts.txt");
    bool in_prog = true;
    contact contacts;
    string name;
    int number;
    while (in_prog) {
    cout << "1. Add contacts" << endl
        << "2. Edit contact" << endl
        << "3. Delete contact" << endl
        << "4. Show all" << endl
        << "5. exit" << endl;
   cout << "Your choice: ";
    cin >> choice;
    contacts.readFile(contacts_file_read);
    if (choice == 1) {
        cout << "Enter name & number separated by a space: ";
        cin >> name >> number;
        contacts.add_contact(name, number);
    } else if (choice == 2) {
       cout << "Enter name of contacts to be edited: ";
       cin >> name;
       contacts.edit_contact(name);
   }  else if (choice == 3) {
    cout << "Enter name of contact to be deleted: ";
    cin >> name;
    contacts.delete_contact(name);
} else if (choice == 4) {
    contacts.show_all();
} else if(choice == 5) {
    contacts.writeFile(contacts_file_write);
} else {
    cout << "Wrong choice" << endl;
}
}



return 0;
}

所以,我在我的编程课上被要求用 C++ 制作一个只使用对象的电话簿应用程序,所以这是我的尝试。

所有函数都很好,我在完成每个函数后重新编译程序给了我 0 个错误,但是每当我尝试调用以前工作正常的 writeFile 或 readFile 函数时,现在编译器给我一个错误“错误: 使用已删除的函数..."

我不知道什么是删除函数,以及为什么只有将文件对象作为参数的函数才被这样处理。

有人可以帮忙吗?

谢谢。

【问题讨论】:

标签: c++ class oop c++14 fstream


【解决方案1】:

std::ifstream 类型的对象是不可复制的——事实上,该对象代表了打开文件的唯一句柄,并且很难概念化复制这种独特的责任意味着什么。

确实,无法复制对象是通过删除复制构造函数来编码的,这会导致您在尝试复制它时看到的错误。

您的代码应该传递原始的ifstream,而不是副本(通过引用参数):

void readFile(ifstream & contact_file)
//            ^^^^^^^^^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-03
    • 2016-11-11
    • 2016-02-20
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多