【问题标题】:Error with Class in C++ for my project我的项目的 C++ 中的类错误
【发布时间】:2017-12-25 09:14:40
【问题描述】:

我是新手。基本上我刚刚学会了如何在 C++ 中使用类。当我尝试打印时,值似乎为 0。有人可以帮我吗?它应该打印出来:

Susan Myers 47899 会计副总裁 马克·琼斯 39119 IT 职位 乔伊·罗杰斯 81774 制造工程师

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Employee
{
    private:
        string name; 
        int idNumber; 
        string department; 
        string position; 

    public:
        Employee()
        {
            name=" "; 
            idNumber=0; 
            department=" "; 
            position=" "; 
        }

        Employee(string, int, string, string)
        {
            int id; 
            string n,d,p; 

            name=n; 
            idNumber=id; 
            department=d; 
            position=p; 
        }

        Employee(string, int)
        {
            string n; 
            int id; 

            name=n; 
            idNumber=id; 
        }
    void setName(string)
    {
        string n; 
        name=n; 
    }
    void setId(int)
    {
        int id;
        idNumber=id; 
    }
    void setDepartment(string)
    {
        string d; 
        department=d; 
    }
    void setPosition(string)
    {
        string p; 
        position=p; 
    }
    string getName() const
    {
        return name; 
    }
    int getId() const
    {
        return idNumber; 
    }
    string getDepartment() const
    {
        return department; 
    }
    string getPosition() const
    {
        return position; 
    }

}; 


int main()
{

    Employee e1; 
    Employee e2; 
    Employee e3; 

    e1.setName("Susan Meyers"); 
    e2.setName("Mark Jones"); 
    e3.setName("Joy Rogers"); 


    e1.setId(47899); 
    e2.setId(39119); 
    e3.setId(81744); 

    e1.setDepartment("Accounting"); 
    e2.setDepartment("IT"); 
    e3.setDepartment("Manufacturing"); 

    e1.setPosition("Vice President"); 
    e2.setPosition("Programmer"); 
    e3.setPosition("Engineer"); 

    cout<<"---------------------------------------"<<endl; 
    cout<<"Name"<<setw(6)<<"ID Number"<<setw(10)<<"Department"<<setw(12)<<"Position"<<endl; 

    cout<<e1.getName()<<setw(6)<<e1.getId()<<setw(10)<<e1.getDepartment()<<setw(12)<<e1.getDepartment()<<endl; 
    cout<<e2.getName()<<setw(6)<<e2.getId()<<setw(10)<<e2.getDepartment()<<setw(12)<<e2.getDepartment()<<endl; 
    cout<<e3.getName()<<setw(6)<<e3.getId()<<setw(10)<<e3.getDepartment()<<setw(12)<<e3.getDepartment()<<endl; 


    return 0; 
}

【问题讨论】:

  • 为什么不重读这本书呢?参数通常有名字
  • 我认为你应该把你拥有的东西扔进垃圾箱,然后阅读a good C++ book

标签: c++ class


【解决方案1】:

当您依赖猜测而不是正确阅读 C++ 入门教科书时,您会得到这样的结果

Employee 类的构造函数(除了我删除的空白行)您定义为

Employee(string, int, string, string)
  {
        int id; 
        string n,d,p; 
        name=n; 
        idNumber=id; 
        department=d; 
        position=p; 
  }

具有以下效果。

  • 调用者传递的四个参数被忽略,因为它们没有被命名。
  • 四个默认初始化变量(idndp)在构造函数主体的本地定义。 id 将未初始化。其他的,因为它们是std::string,默认初始化(为空字符串)
  • 接下来的四个语句将这些变量复制到类成员中。结果是初始化 idNumber 具有未定义的行为(因为 id 未初始化)并且三个字符串被初始化为空字符串。

要获得(我假设)您想要的效果,请将其更改为;

Employee(std::string n, int id, std::string d, std::string p)
{
    name=n; 
    idNumber=id; 
    department=d; 
    position=p; 
}

请注意,我用它的全名std::string 调用string。这允许删除 using namespace std 这(除其他外)在头文件中是不好的做法。

更好的是,将其更改为

Employee(const std::string &n, int id, const std::string &d, const std::string &p) : 
     name(n), idNumber(id), department(d), position(p)
{
}

它通过const 引用传递字符串(避免std::strings 的额外副本)并使用初始化列表而不是分配给构造函数中的成员。

类似的 cmets 适用于 Employee 的所有成员函数,除了只有构造函数可以有初始化列表。

【讨论】:

    【解决方案2】:

    发生的错误

    1. 演示文稿

    你的代码非常混乱,而且有很多不相关的东西。

    1. 语法

    void setPosition(string){ 这里你的函数没有参数!什么是字符串?

    代码

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    class Employee{
        public:
            string name; 
            int idNumber; 
            string department; 
            string position; 
    
        void setName(string n){ 
            name=n; 
        }
        void setId(int k){
            int id;
            idNumber=id; 
        }
        void setDepartment(string d){
            department=d; 
        }
        void setPosition(string p){
            position=p; 
        }
        string getName(){
            return name; 
        }
        int getId(){
            return idNumber; 
        }
        string getDepartment(){
            return department; 
        }
        string getPosition(){
            return position; 
        }
    }; 
    int main(){
        Employee e1; 
        Employee e2; 
        Employee e3; 
        e1.setName("Susan Meyers"); 
        e2.setName("Mark Jones"); 
        e3.setName("Joy Rogers"); 
        e1.setId(47899); 
        e2.setId(39119); 
        e3.setId(81744); 
        e1.setDepartment("Accounting"); 
        e2.setDepartment("IT"); 
        e3.setDepartment("Manufacturing"); 
        e1.setPosition("Vice President"); 
        e2.setPosition("Programmer"); 
        e3.setPosition("Engineer"); 
        cout<<"---------------------------------------"<<endl; 
        cout<<"Name"<<" "<<"ID Number"<<" "<<"Department"<<" "<<"Position"<<endl; 
        cout<<e1.getName()<<" "<<e1.getId()<<" "<<e1.getDepartment()<<" "<<e1.getPosition()<<endl; 
        cout<<e2.getName()<<" "<<e2.getId()<<" "<<e2.getDepartment()<<" "<<e2.getPosition()<<endl; 
        cout<<e3.getName()<<" "<<e3.getId()<<" "<<e3.getDepartment()<<" "<<e3.getPosition()<<endl; 
    }
    

    输出

    ---------------------------------------
    Name ID Number Department Position
    Susan Meyers 32767 Accounting Vice President
    Mark Jones 32767 IT Programmer
    Joy Rogers 32767 Manufacturing Engineer
    

    说明

    我已将您的代码缩短了 50%(显示您有多少多余的东西),这是一个有效的代码。

    void setDepartment(string d){
         department=d; 
    }
    

    这里,字符串 d 在函数中被定义为参数。请注意,您的代码也 cout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-23
      • 2016-08-25
      • 2010-11-02
      • 2012-10-25
      • 1970-01-01
      • 2020-01-29
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多