如何迭代每个对象并从c ++中的coustom类数组访问其成员函数?

时间:2021-05-02 14:28:19

Please have a look at below code: I have class facultyType and in main created a array of 250 object of class. in print method passing the Faculty(array of facultyType) but when calling its program getting stopped and no compile error. i want to iterate all facultyType in print method and want to access its member functions. In java doing same working fine but i am afraid why its not working here.

请看下面的代码:我有类facultyType,并在main中创建了一个250对象的类数组。在print方法中传递Faculty(facultyType数组),但在调用其程序时停止并且没有编译错误。我想在print方法中迭代所有facultyType并想要访问其成员函数。在java做同样的工作正常,但我担心为什么它不在这里工作。

#include<iostream>
#include<string>
using namespace std;
class facultyType
{
private:
    string firstname;
    string lastname;
    string department;
    double salary;
    int serviceyears;
public:
    facultyType(){}
    void print(facultyType* faculty,int count);
    void setAll(facultyType faculty[],float percent,int count);
   //setter and getter
};
void facultyType::print(facultyType* faculty,int count)//need help here
{
    int i;
    for(i=0;i<count;i++)
    {
    //want to iterate all facultyType one by one
        facultyType f=faculty[i];//here the problem

        int serviceYear;
        serviceYear =f.getServiceYears();//not getting the exact values
        cout<<"service year "<<serviceYear<<endl;
        cout<<"dfhh"<<endl;
        if(serviceYear>=15) {
            cout<<f.getFirstName()<<"\n"<<f.getLastName()<<"\n"
                <<f.getDepartment();
            cout<<f.getSalary()<<"\n"<<f.getServiceYears()<<endl;
            cout<<"-------------------------------------------------\n";
        }
    }
}

int main()
{
    facultyType Faculty[250];
    facultyType f;
    int count=0;
    int status=0;
    string fname,lastname,depart;
    double sal;
    int serviceyears;
    while(status!=4)
    {
        cout<<"1. Add new Faculty member"<<endl;
        cout<<"2. increase all faculty member salary"<<endl;
        cout<<"3. Print Employee"<<endl;
        cout<<"4. Exit"<<endl;

        cin>>status;

        switch(status)
        {
            case 1:
            {
              cout<<"Enter first name : ";
                    cin>>fname;
                   //..other code for setter and getter values
                   Faculty[count]=newFaculty;
                   count++;
                    break;
                }

            case 3:{
                f.print(Faculty,count);//calling print method
                break;
            }
             case 4: break;
        }

    }

    return 0;
}

I know its dumbest questions , but i am new in c++. Please explain why its differ than Java. Thanks in advance.

我知道它最愚蠢的问题,但我是c ++的新手。请解释为什么它与Java不同。提前致谢。

1 个解决方案

#1


3  

The problem is that your "getter" functions do not actually return anything. Before your edit facultyType::setServiceYears was implemented like this:

问题是你的“getter”函数实际上并没有返回任何东西。编辑之前,facultyType :: setServiceYears是这样实现的:

int facultyType::getServiceYears()
{
    serviceyears;
}

It should have been (adding the return keyword):

应该是(添加return关键字):

int facultyType::getServiceYears()
{
    return serviceyears;
}

This is a type of error that get caught by the compiler when warnings are enabled.

这是一种在启用警告时被编译器捕获的错误。

I compiled it on GNU/Linux using: g++ test.cpp -ggdb -O0 -Wall -Wextra -pedantic

我使用以下命令在GNU / Linux上编译它:g ++ test.cpp -ggdb -O0 -Wall -Wextra -pedantic

#1


3  

The problem is that your "getter" functions do not actually return anything. Before your edit facultyType::setServiceYears was implemented like this:

问题是你的“getter”函数实际上并没有返回任何东西。编辑之前,facultyType :: setServiceYears是这样实现的:

int facultyType::getServiceYears()
{
    serviceyears;
}

It should have been (adding the return keyword):

应该是(添加return关键字):

int facultyType::getServiceYears()
{
    return serviceyears;
}

This is a type of error that get caught by the compiler when warnings are enabled.

这是一种在启用警告时被编译器捕获的错误。

I compiled it on GNU/Linux using: g++ test.cpp -ggdb -O0 -Wall -Wextra -pedantic

我使用以下命令在GNU / Linux上编译它:g ++ test.cpp -ggdb -O0 -Wall -Wextra -pedantic