0
#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;
}

So, I was asked in my programming class to make a phone book application in C++ using only objects, so this is my attempt at it.

All functions are good, I did recompile the program after finishing each function at it gave me 0 errors, however whenever I try to call writeFile or readFile function that were previously working fine, now the compiler gave me an error of "error: use of deleted functions... "

I don't know what are deleted functions and why only functions that take file objects as an argument are treated as such.

Can anyone please help?

Thanks.

  • [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris Feb 19 '17 at 00:00

1 Answers1

3

Objects of type std::ifstream are not copyable -- indeed, the object represents the unique handle of an open file, and it would be difficult to conceptualize what it would mean to copy such unique responsibility.

Indeed, this inability to copy an object is encoded by making the copy constructor deleted, which causes the error that you see when you do attempt to copy it.

Your code should pass the original ifstream, not a copy (by taking a reference parameter):

void readFile(ifstream & contact_file)
//            ^^^^^^^^^^
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025