试图在C++中打印存储在类型对象数组中的信息

Trying to print information stored in an array of type object in C++

本文关键字:数组 对象 信息 类型 打印 C++ 存储      更新时间:2023-10-16

我刚开始学习C++中的面向对象编程,在弄清楚如何打印存储在数组中的对象时遇到了问题。据我所知,我只想尝试遍历数组并打印出每个员工对象,因为对象与int和double等变量不同,我确信这会造成问题。我的逻辑是错的,还是只是语法问题?这是我的代码:

标题:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee
{
private:
    string name;
    string idNumber;
    string department;
    string position;
    int yearsWorked;
public:
    Employee();
    Employee(string, string);
    Employee(string, string, string, string, int);
    void setName(string);
    void setIdNumber(string);
    void setDepartment(string);
    void setPosition(string);
    bool setYearsWorked(int);
    string getName()const;
    string getIdNumber()const;
    string getDepartment()const;
    string getPosition()const;
    int getYearsWorked()const;
};
#endif

实施:

#include "Employee.h"
using namespace std;
Employee::Employee()
{
    string name = "";
    string idNumber = "";
    string department = "";
    string position = "";
    int yearsWorked = 0;
}
Employee::Employee(string nm, string id)
{
    string name = nm;
    string idNumber = id;
    string department = "";
    string position = "";
    int yearsWorked = 0;
}
Employee::Employee(string nm, string id, string dpt, string pos, int years)
{
    string name = nm;
    string idNumber = id;
    string department = dpt;
    string position = pos;
    int yearsWorked = years;
}
void Employee::setName(string nm)
{
    name = nm;
}
void Employee::setIdNumber(string id)
{
    idNumber = id;
}
void Employee::setDepartment(string dpt)
{
    department = dpt;
}
void Employee::setPosition(string pos)
{
    position = pos;
}
bool Employee::setYearsWorked(int years)
{
    if (years >= 0)
    {
        yearsWorked = years;
        return true;
    }
    else
        return false;
}
string Employee::getName()const
{
    return name;
}
string Employee::getIdNumber()const
{
    return idNumber;
}
string Employee::getDepartment()const
{
    return department;
}
string Employee::getPosition()const
{
    return position;
}
int Employee::getYearsWorked()const
{
    return yearsWorked;
}

Main:

#include <iostream>
#include <iomanip>
#include "Employee.h"
using namespace std;
const int SIZE = 3;
int main()
{
    Employee emp1("Jenny Jacobs", "JJ8990", "Accounting", "President", 15);
    Employee emp2("Myron Smith", "MS7571", "IT", "Programmer", 5);
    Employee emp3("Chris Raines", "CR6873", "Manufacturing", "Engineer", 30);
    Employee employees[SIZE] = {emp1, emp2, emp3};
    for (int i = 0; i < SIZE; i++)
    {
        cout << employees[i] << endl;
    }
    system("PAUSE");
    return 0;
}

添加此项:

std::ostream& operator<<( std::ostream& stream, Employee const& emp )
{
    return (stream << emp.getName());
}

根据需要进行修改。


一般意见:

  • 不要将using namespace std;放在标头的全局命名空间中。请记住,标准库定义了非常常见的名称,如distance。这很容易导致名称冲突。

  • 为宏保留所有大写名称,以减少名称冲突和意外文本替换的可能性。

  • 优先通过引用传递潜在的"大"对象,如std::string,例如形式参数类型std::string const&,以避免过度复制。当目标是完美的代码时,这个规则也有一些例外,例如C++11移动语义,但这是一个很好的通用规则。

employees[i]属于Employee类型。所以要么你必须像一样打印

cout<<employees[i].getName(); // so on

或者你不得不超载<lt;员工类型的操作员:

ostream& operator<<(ostream& stream, Employee const& emp );

首先。在我的机器中,它编译得很好
其次,你正在做:

string name = nm;

所以name是一个自动变量,而不是类的成员。你应该这样做:

  name = nm; // if you delete int name; line

或者,

 this->name = nm;

您的代码的一些更改:

Employee::Employee() 
    : yearsWorked( 0 )
{
}
Employee::Employee(string nm, string id) 
    : name( nm ), idNumber( id ), yearsWorked( 0 )
{
}
Employee::Employee(string nm, string id) 
    : name( nm ), idNumber( id ), department( dpt ), position( pos ), yearsWorked( years ), 
{
}
std::ostream & operator <<( std::ostream &os, const Employee &emp )
{
    return ( os << "ID: " << emp.idNumber << ", name: " << emp.name 
                << ", department: " << emp.department << ", position: " << emp.position
                << ", years worked: " << emp.yearsWorked );
}