找不到超载的成员功能,类位于标题中

The overloaded member function is not found, the class is in header

本文关键字:于标题 标题 超载 成员 功能 找不到      更新时间:2023-10-16

我在user.cpp文件中收到了函数的错误消息:未找到的Overloaded成员函数,并且在Main.CPP i获得:声明与函数不相容。该程序是一本电话簿,可以写入数据并从文件中获取。这些功能是单独工作的,但是当我将它们放在项目中时,它们呼叫它们不起作用。

编辑:在编辑程序后,它正在正常工作,但是我制作了3个新功能,这些功能无法正常工作。第一个是在文件中搜索一个国家的前缀,并返回了电话号码的前缀,返回很好,但没有保存文件。搜索器和删除器函数部分可行,在文件中保持相同的用户。

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 screenn";
 cout<<"Press 4 to edit a user phone number and save it after in file(phonebook.txt)n";
 cout<<"Press 5 for helpn";
 cout<<"Press 6 to exit the applicationn";
}
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 addedn";
}
//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 <<"nnNew 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 <<"nnUser 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 << "nothingn";
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 and 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();
};

但是在用户向量上工作的操作必须是静态的,它们不会影响实例。

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 ,最后一个更实用

in main

  • 本地var people 必须是数组/向量,如果考虑到 User::ReadAllUser中100的数组可以是 User people[100];,但是vector又更好

  • 在某些情况下,您重新定义了变量 People num_people,在 switch的某些情况下 毫无意义,必须删除这些重新定义。使用矢量可变num_people变得无用

变量peoplenum_people的唯一定义(如果数组而不是向量(必须在之前移动而不是而不是内部,并且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::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下,您将在Linux之前读取' n'' n' r'

用户:: addName 必须是静态的,并在 People 中添加用户,而不仅仅是设置本地var。User::SaveUser()的调用必须在新实例上完成,甚至当前操作不是静态的情况

User::SaveUser必须添加 a文件中的新用户,而不是只用新用户替换所有文件,以附加模式打开文件。