【问题标题】:Using class in c++ [closed]在 C++ 中使用类 [关闭]
【发布时间】:2013-05-13 15:24:37
【问题描述】:

我正在尝试使用类,但程序无法运行。如何修复?请记住,我是 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 ​​错误 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 错误 2 错误 C2660: 'Person::setOccupation' : function does not take 0 arguments c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 138 错误 3 error C2660: 'Person::setLocation' : function does not take 0 arguments c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 140 错误 4 error C2660: 'Person::setReferences' : function does not take 0 arguments c:\users\toshiba\documents\visual studio 2010\projects\trial and error\trial and error\bel class.cpp 142

【问题讨论】:

  • 好吧,您可以先阅读编译器错误并修复它们。在第 13 行等处添加“存储类”或“类型说明符”等。
  • IntelliSense 错误通常不会阻止程序的构建。
  • @StoryTeller "IntelliSense" 仅表示发现错误的原因。列出的那些使代码几乎无法编译。

标签: c++ class


【解决方案1】:

关键字privatepublic 是这样拼写的,而不是大写首字母。

此外,拥有一个有成员 int Person; 的 Person 类似乎是消除混乱的好方法。

【讨论】:

  • 谢谢我解决了这个问题,但我没有收到一个新错误“函数调用中的参数太少”。这是什么意思?.....我已经编辑了上面的代码
  • @Sara:错误消息说您正在尝试调用一些需要 n 个参数的函数,但您提供了 CG->setPersonName ();。这意味着您尝试在 Person 类的对象上调用方法 setPersonName。但是,在 Person 类中,您只提供了一个方法“setPersonName”,它需要一个参数(人的新名称)。您可能希望传递程序用户输入的文本:CG-&gt;setPersonName (TEMP); Regards, Stuart
猜你喜欢
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多