我在user.cpp文件中得到了函数的错误消息:找不到重载的成员函数,在main.cpp中,我得到了:声明与函数不兼容。 该程序是一个电话簿,可以写入数据并从文件中提取。这些功能可以单独使用,但是当我将它们放在项目中时,调用它们将无法正常工作。
编辑: 程序编辑后,它可以正常工作,但是我做了3个无法正常工作的新功能。第一个是Prefix,它在文件中搜索国家/地区,并返回电话号码的前缀,返回值不错,但是没有保存在文件中。在文件中部分起作用的SearchUser和DeleteUser函数保持不变的用户。
user.h
#pragma once
#include <string>
using namespace std;
class User
{private:
string firstname, lastname, country, city, street;
string phone, prefix;
public:
void ReadAllUsers(User[], int&);
void SaveUser(User, int&);
void SaveToFile(const User[], int);
void AddName(User[], int&);
void ListAllUsers(const User[], int&);
void Prefix(User, int);
void ChangePhone(User[], int&);
void Help();
void DeleteUser(User[], int&);
bool Search(string x)
{
return (phone.find(x) != string::npos);
}
};
user.cpp
#include "User.h"
#include <iostream>
#include <fstream>
#include <string>
#pragma warning(disable:4996)
using namespace std;
const string PHONEBOOK_FILENAME = "phonebook.txt";
void User::Help()
{cout<<"\nWELCOME TO THE APPLICATION!\n";
cout<<"Press 0 to display on the screen all records that are saved in the file(phonebook.txt)\n";
cout<<"Press 1 to add 1 or more new record(s) in file(phonebook.txt)\n";
cout<<"Press 2 to delete permanently a record from file(phonebook.txt)\n";
cout<<"Press 3 to sort users from file(phonebook.txt) by name and display them on the screen\n";
cout<<"Press 4 to edit a user phone number and save it after in file(phonebook.txt)\n";
cout<<"Press 5 for help\n";
cout<<"Press 6 to exit the application\n";
}
void User::ReadAllUsers(User people[], int &num_people)
{
ifstream f;
f.open(PHONEBOOK_FILENAME.c_str());
if (f.fail())
{
cout << "Unable to open file " << endl;
return ;
}
int i = 0;
while (!f.eof() && i < 100)
{ getline(f, people[i].firstname);
getline(f, people[i].lastname);
getline(f, people[i].phone);
getline(f, people[i].country);
getline(f, people[i].city);
getline(f, people[i].street);
i++;
}
num_people = i;
f.close();
}
//Add country prefix to the phone number
void User::Prefix(User person, int num_people)
{string filecountry;
ifstream f;
f.open("prefix.txt");
{
while (getline(f, filecountry))
{
if (person.country == filecountry )
{
f.ignore();//next line
f >> person.prefix;
}
}
cout << "The prefix is " << person.prefix;
f.close();
}
}
void User::SaveUser(User person, int &num_people)
{
ofstream f(PHONEBOOK_FILENAME.c_str(), ios::app ) ;
if (f.fail())
cout << "Unable to open file " << endl;
else
Prefix(person, num_people);
f << person.firstname << " " << person.lastname << " " << person.country << " " << person.city << " " << person.street << " " << person.prefix << "-" << person.phone << endl;
cout << "\nThe user was added\n";
}
//Save data after a modification or after a user delete
void User::SaveToFile(const User people[], int num_people)
{ofstream f;
f.open(PHONEBOOK_FILENAME.c_str());
for(int i = 0; i < num_people; i++)
{
f << people[i].firstname << " " << people[i].lastname << " " << people[i].country << " " << people[i].city << " " << people[i].street << " " << people[i].prefix << " " << people[i].phone << endl;
}
}
// Read user data from the keyboard, add a new contact to the array
void User::AddName(User people[],int &num_people)
{User person;
cout <<"Enter the user's first name: ";
cin >> person.firstname;
cout <<"Enter the user's last name: ";
cin >> person.lastname;
cout <<"Enter the user's country: ";
cin >> person.country;
cout <<"Enter the user's city: ";
cin >> person.city;
cout <<"Enter the user's street: ";
cin >> person.street;
cout <<"Enter the user's phone number: ";
cin >> person.phone;
for(int i = 0; i < num_people; i++)
{
if( i + 1 == num_people)
people[num_people] = person;
}
SaveUser(person, num_people);
num_people++;
}
// Ask the for person's name to change, find the person in the array and
// change it to the new phone number. Then save the new data to file by
// calling SaveToFile.
void User::ChangePhone(User people[], int &num_people)
{
User person;
int count;
cout <<"Enter name to change: ";
cin >> person.firstname;
for(count = 0; count < num_people; count++)
{
if(people[count].Search(person.firstname))
{ cout <<endl<< people[count].firstname<<endl;
cout <<"Current number"<<people[count].phone;
cout << "\nNew number: ";
cin >> people[count].phone;
SaveToFile(people,num_people);
cout <<"\n\nNew number Saved.";
return;
}
}
if(count = num_people)
cout <<"\nName not found.\n";
}
void User::DeleteUser(User people[], int &num_people)
{string phone;
int count = 0;
ifstream f;
f.open("phonebook.txt");
cout << "Input the phone of user that you want to delete ";
cin >> phone;
for(count = 0; count < num_people; count++)
{
if(people[count].Search(phone))
{ cout <<endl<< people[count].phone<<endl;
people[count].firstname = people[count].lastname = people[count].phone = people[count].country = people[count].city = people[count].street = " ";
}
SaveToFile(people,num_people);
cout <<"\n\nUser deleted.";
return;}
f.close();
}
// Ask the user for a name to find and show all occurrences of the name
// in the given array.
/*void SortUsers(User people[], int num_people)
{
}
*/
// Display all user data.
void User::ListAllUsers(const User people[], int &num_people)
{string line;
ifstream f;
f.open("phonebook.txt");
if (f)
{
while (getline(f, line)) {
cout << line.c_str() << endl;
}
}
else
cout << " Can't open the file!\n";
f.close();
}
main.cpp
#include <iostream>
#pragma warning(disable:4996)
#include <string>
#include <fstream>
#include <conio.h>
#include <stdlib.h>
#include "User.h"
using namespace std;
int main()
{
string mypassword; //file password
string password; //input password
char ch;
ifstream file;
file.open("mypassword.txt");
file >> mypassword;
file.close();
cout << "=====================================================================\n";
cout << " WELCOME !"<<endl;
cout << "=====================================================================\n";
cout << " Enter the password to access the program : ";
ch = _getch();
while(ch != 13){//character 13 is enter
password.push_back(ch);
cout << '#';
ch = _getch();
}
cout << "\n=====================================================================\n";
if(password==mypassword)
{cout<<"\n Correct password!"<<endl;
cout << "=====================================================================\n";
int choice;
bool menu = true;//menu
User people[100];
int num_people=0;
while (menu != false){
cout << "|===================================================================|\n";
cout << "| =MENU= |\n";
cout << "|===================================================================|\n";
cout << "| 0 - Phone book! |\n";
cout << "| 1 - Add user! |\n";
cout << "| 2 - Delete user! |\n";
cout << "| 3 - Sort users! |\n";
cout << "| 4 - Edit user! |\n";
cout << "| 5 - Help! |\n";
cout << "| 6 - Exit! |\n";
cout << "|===================================================================|\n";
cout << " Enter your choice and press enter: ";
cin >> choice;
User user;
fstream file;
file.open("phonebook.txt", ios::ate | ios::in | ios::out | ios::binary );
// Get records from file
user.ReadAllUsers(people, num_people);
switch (choice)
{
case 0://Read all contacts from file
{cout << "==============================PHONEBOOK==============================\n" << endl;
user.ListAllUsers(people, num_people);
}
break;
case 1:
{
user.AddName(people, num_people);
break;}
case 2:
{
user.DeleteUser(people, num_people);
break;}
case 3:
cout << "nothing\n";
break;
case 4:
{
user.ChangePhone(people, num_people);
break;}
case 5:
{
user.Help();
break;}
case 6:
{cout << " Session ended!\n";
file.close();
menu = false;
break;}
default:
cout << " Not a Valid Choice. \n";
cout << "======================================================================\n";
cout << " Choose again.\n";
cin >> choice;
break;
}
}
}
else {
cout<<"\n"<<" Wrong password!"<<endl;
cout << "===================================================================\n";
}
return 0;
}
答案 0 :(得分:0)
User.h和User.cpp中的声明不一致,假设实现正确,该类的声明可以替换为:
class User
{
private:
string firstname, lastname, country, city, street, streetnumber;
string phone;
public:
void ReadAllUsers(User[], int&);
void SaveUser();
void SaveToFile(const User[], int);
void AddName(User[], int&);
void ListAllUsers(const User[], int&);
void Read();
void Print();
};
但是对User向量执行的操作必须是静态的,它们不会影响实例。
class User
{
private:
string firstname, lastname, country, city, street, streetnumber;
string phone;
public:
static void ReadAllUsers(User[], int&);
void SaveUser();
static void SaveToFile(const User[], int);
static void AddName(User[], int&);
static void ListAllUsers(const User[], int&);
void Read();
void Print();
};
为什么要使用数组而不是 std :: vector ,这最后一个更加实用
在主要
局部变量 people 必须是一个数组/向量,如果考虑User::ReadAllUser
中限制为100的数组可以是User people[100];
,但同样向量要好得多
您在 switch 的某些情况下重新定义了变量 people 和num_people
,因为毫无意义,必须删除这些重新定义。使用向量变量num_people
变得无用
变量people
和num_people
的唯一定义(如果是数组而不是向量)必须移到 while 之前,而不是内部,并且num_people
如果存在,则必须将其初始化为0,所以
...
int choice;
bool menu = true;//menu
User people[100];
int num_people = 0;
while (menu != false){
或
...
int choice;
bool menu = true;//menu
vector<User> people
while (menu != false){
但是如何将 people 设置为User的静态变量,并保存所有实例呢?
在User::ReadAllUsers
if (f.fail()) { cout << "Unable to open file " << endl; } else { return ; }
没有意义,必须在无法打开文件文件的情况下返回,而不是在可能的情况下返回,所以:
if (f.fail())
{
cout << "Unable to open file " << endl;
return ;
}
您错过了检查file.open("mypassword.in");
成功的机会
请不要使用文字数字,而要使用字符名称,因此请使用'\ r'而不是13。注意'\ r'假设您在Windows下,例如在Linux下,您之前将读为'\ n',而不会使用'\ r'
User :: AddName 必须是静态的,并在 people 中添加用户,而不仅仅是设置本地变量。 User::SaveUser()
的调用必须在新实例上完成,甚至在当前操作不是静态的情况下也不是
User::SaveUser
必须添加文件中的新用户,而不是仅用新用户替换所有文件,而是以追加模式打开文件。