我正在尝试使用类,但程序无法运行。如何修复?请记住,我是 C++ 的初学者。
#include <iostream>
#include <string>
#include <fstream>// enable writing to and reading from files
#include <cstdlib>
#include <time.h>
using namespace std;
//Globals
//----------------------------------------------------------------------------------------------------------------------------
class Person
{
public:
Person() {cout<<"\n\tBuilding a Person.";}
~Person() {cout<<"\n\tDestroying a Person.";}
//Accessor Methods
void setPersonName(string X) {PersonName=X; }
void setOccupation(string X) {Occupation=X; }
void setLocation(string X) {Location=X; }
void setReferences(string X) {References=X; }
string GetPersonName() {return PersonName;}
string GetOccupation() {return Occupation;}
string GetLocation() {return Location;}
string GetReferences() {return References;}
private:
string PersonName ;
string Occupation ;
string Location;
string References;
};//----------------------------------------------------------------------------------------------------------------------------
//Function prototypes
void createperson();
void editperson();
void displayperson();
void saveperson();
void loadperson();
Person*CG;
int main (int argc, char *argv[])
{
char choose[10] = "";
CG = new Person ();
cout<<"\n\t Personnel database 1.0\n";
//system ("CLS");
while (choose [0] != 'q')
{
system ("CLS");
cout<<"\n\t---------------------Main Menu---------------------\n";
cout<<"\n\t| |\n";
cout<<"\n\t| (c) Create person |\n";
cout<<"\n\t| (E) edit information |\n";
cout<<"\n\t| (D) display person |\n";
cout<<"\n\t| (S) Save person |\n";
cout<<"\n\t| (L) Load person |\n";
cout<<"\n\t| (Q) quit |\n";
cout<<"\n\t| |\n";
cout<<"\n\t---------------------------------------------------\n\n\t";
cin>>choose;
//system ("CLS");
switch (choose [0])
{
//system ("CLS");
case 'c': createperson(); break;
case 'e': editperson(); break;
case 'd': displayperson(); system ("pause"); break;
case 's': saveperson(); break;
case 'l': loadperson(); break;
case 'q': cout<<"\n\tExiting main menu..."; break;
default :"\n\tInvalid input!";
}
}
system ("pause");
return 0;
}
void createPerson()
{
CG=new Person();
}
//----------------------------------------------------------------------------------------------------------------------------
void editPerson()
{
string TEMP;
system ("CLS");
cout<<"\n\n\t---------------------EDIT person ---------------------------";
cout<<"\n\t NAME: ";
cin.ignore();
getline (cin,TEMP);
CG->setPersonName(TEMP);
cout<<"\n\t Occupation: ";
getline (cin,TEMP);
CG->setOccupation(TEMP);
cout<<"\n\t Location: ";
getline (cin,TEMP);
CG->setLocation(TEMP);
cout<<"\n\t References: ";
getline (cin,TEMP);
CG->setReferences(TEMP);
}
//----------------------------------------------------------------------------------------------------------------------------
void displayPerson()
{
system("CLS");
cout<<"\n\n\t---------------------person information----------------------";
cout<< "\n\tName: "<<CG->GetPersonName();
cout<< "\n\tOccupation: "<<CG->GetOccupation();
cout<< "\n\tLocation: "<<CG->GetLocation();
cout<< "\n\tReferences: "<<CG->GetReferences();
cout<<"\n\n\t---------------------------------------------------------------\n\n";
}
//----------------------------------------------------------------------------------------------------------------------------
void savePerson()
{
try
{
ofstream DATAFILE;
DATAFILE.open("Data1.file",ios::out);
DATAFILE <<CG->GetPersonName ()<<"\n";
DATAFILE <<CG->GetOccupation ()<<"\n";
DATAFILE <<CG->GetLocation ()<<"\n";
DATAFILE <<CG->GetReferences ()<<"\n";
DATAFILE.close(); //Prevents data from getting corrupt incase of sudden shutdown
cout<<"\n\t Success! Data was saved to file.";
}
catch (exception x)
{
cout<<"\n\t File Error! Could not SAVE PERSON.";
}
}
//----------------------------------------------------------------------------------------------------------------------------
void loadperson()
{
try
{
string TEMP;
ifstream DATAFILE;
DATAFILE.open("Data1.file",ios::in);
getline (DATAFILE,TEMP);
CG->setPersonName ();
getline (DATAFILE,TEMP);
CG->setOccupation ();
getline (DATAFILE,TEMP);
CG->setLocation ();
getline (DATAFILE,TEMP);
CG->setReferences ();
DATAFILE.close();
}
catch (exception x)
{
cout<<"\n\t File Error! Unable to load data.";
}
}
//----------------------------------------------------------------------------------------------------------------------------
这些是错误:
5 IntelliSense: too few arguments in function call c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 136
6 IntelliSense: too few arguments in function call c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 138
7 IntelliSense: too few arguments in function call c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 140
8 IntelliSense: too few arguments in function call c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 142
错误 1 error C2660: 'Person::setPersonName' : function does not take 0 arguments c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 136 Error 2 error C2660 : 'Person::setOccupation' : 函数不接受 0 个参数 c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 138 Error 3 error C2660: 'Person: :setLocation' : 函数不接受 0 个参数 c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 140 Error 4 error C2660: 'Person::setReferences' :函数不接受 0 个参数 c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 142