C++ C+中的类出错+;对于我的项目

C++ C+中的类出错+;对于我的项目,c++,class,C++,Class,我是新来的。基本上我只是学会了如何在C++中使用类。当我尝试打印时,值似乎是0。有人能帮我吗?它应该打印出来: Susan Myers 47899会计副总裁 马克·琼斯39119 IT位置 Joy Rogers 81774制造工程师 #include <iostream> #include <string> #include <iomanip> using namespace std; class Employee { private:

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

Susan Myers 47899会计副总裁 马克·琼斯39119 IT位置 Joy Rogers 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; 
}
#包括
#包括
#包括
使用名称空间std;
班级员工
{
私人:
字符串名;
国际电话号码;
弦乐部;
串位置;
公众:
雇员()
{
name=“”;
idNumber=0;
部门=”;
位置=”;
}
雇员(字符串,整型,字符串,字符串)
{
int-id;
字符串n,d,p;
name=n;
idNumber=id;
部门=d;
位置=p;
}
雇员(字符串,int)
{
字符串n;
int-id;
name=n;
idNumber=id;
}
void setName(字符串)
{
字符串n;
name=n;
}
void setId(int)
{
int-id;
idNumber=id;
}
无效设置部门(字符串)
{
字符串d;
部门=d;
}
无效设置位置(字符串)
{
字符串p;
位置=p;
}
字符串getName()常量
{
返回名称;
}
int getId()常量
{
返回idNumber;
}
字符串getDepartment()常量
{
退货部;
}
字符串getPosition()常量
{
返回位置;
}
}; 
int main()
{
雇员e1;
雇员e2;
员工e3;
e1.设置名称(“Susan Meyers”);
e2.设定名称(“马克·琼斯”);
e3.设置名称(“Joy Rogers”);
e1.setId(47899);
e2.setId(39119);
e3.setId(81744);
e1.会计部(“会计”);
e2.教育部(“IT”);
e3.教育部(“制造业”);
e1.职位(“副总裁”);
e2.设置位置(“编程器”);
e3.设置位置(“工程师”);

cout这就是当你依靠猜测而不是正确阅读C语言的入门教材时得到的结果++

Employee
类的构造函数,您将其定义为

具有以下效果

  • 调用方传递的四个参数将被忽略,因为它们没有命名
  • 构造函数主体本地定义了四个默认初始化变量(
    id
    n
    d
    p
    )。
    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; 
}
请注意,我正在调用
string
的全名
std::string
。这允许使用命名空间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::string
s副本),并使用初始化器列表,而不是分配给构造函数中的成员

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

出错
  • 介绍
  • 你的代码非常混乱,有很多不相关的东西

  • 语法
  • 无效设置位置(字符串){ 这里你的函数没有参数!什么是字符串

    代码 解释 我把你的代码缩短了50%(显示了你有多少多余的东西),这是一个有效的代码

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

    这里,字符串d在函数中定义为一个参数。请注意,您的代码为什么也不能重新阅读这本书?参数通常有名称。我认为您应该将您拥有的内容扔进垃圾箱并读取
    #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
    
    void setDepartment(string d){
         department=d; 
    }