【问题标题】:Trying to print information stored in an array of type object in C++尝试打印存储在 C++ 中类型对象数组中的信息
【发布时间】:2013-12-12 07:46:14
【问题描述】:

我刚刚开始学习 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;
}

主要:

#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 命名空间污染他们的代码。
  • 你尝试过重载
  • 哪一行或多行给你错误?
  • @Nik 他可能不知道。已经有答案了
  • 一旦你实现了 Alf 的答案,另外 you can used std::copy to print the array 的员工,而不是使用 for 遍历他们。

标签: c++ arrays class object


【解决方案1】:

添加这个:

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

根据需要修改。


一般cmets:

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

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

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

【讨论】:

    【解决方案2】:

    employees[i] 的类型为 Employee。所以要么你必须像这样打印

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

    或者你必须重载

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

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

    string name = nm;
    

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

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

    或者,

     this->name = nm;
    

    【讨论】:

    • 第一个有访问级别冲突。
    • @deeiip 我尝试了第一种方法并收到错误Error 1 error C3867: 'Employee::getName': function call missing argument list; use '&amp;Employee::getName' to create a pointer to member
    【解决方案3】:

    您的代码的一些更改:

    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 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 2013-03-29
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多