[英]The overloaded member function is not found, the class is in header
我在user.cpp文件中得到函數的錯誤消息:未找到重載的成員函數,在main.cpp中得到:聲明與函數不兼容。 該程序是一個電話簿,可以寫入數據並從文件中提取。 這些功能是單獨工作的,但是當我將它們放在項目中時,調用它們是行不通的。
編輯:程序編輯后,它可以工作,但是我做了3個新功能,它們不能正常工作。 第一個是Prefix,它在文件中搜索國家/地區,並返回電話號碼的前綴,返回值不錯,但是沒有保存在文件中。 在文件中部分起作用的SearchUser和DeleteUser函數保持不變的用戶。
用戶名
#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;
}
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 ,這最后一個更加實用
在主要
本地var people必須是一個數組/向量,如果考慮User::ReadAllUser
限制為100的數組,則它可以是User people[100];
,但向量又好得多
您重新定義變量人 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){
但是,如何使人們成為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
必須在文件中添加新用戶,而不是僅用新用戶替換所有文件,而是以追加模式打開文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.