C++ - 类问题

Sim*_*ity 1 c++ pointers class

在:http://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/

有以下程序(我做了一些小修改):

#include <iostream>

class Employee
{
public:
    char m_strName[25];
    int m_id;
    double m_wage;

    //set the employee information
    void setInfo(char *strName,int id,double wage)
    {
        strncpy(m_strName,strName,25);
        m_id=id;
        m_wage=wage;
    }

    //print employee information to the screen
    void print()
    {
        std::cout<<"Name: "<<m_strName<<"id: "<<m_id<<"wage: $"<<wage<<std::endl;
    }
};

int main()
{
    //declare employee
    Employee abder;
    abder.setInfo("Abder-Rahman",123,400);
    abder.print();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我得到以下内容:

替代文字

而且,为什么这里使用指针? void setInfo(char *strName,int id,double wage)

谢谢.

cod*_*ict 5

您必须包含声明该strncpy函数的标头.所以加

#include <cstring> 
Run Code Online (Sandbox Code Playgroud)

在开始.

成员名称是m_wagewageprint成员函数中使用它.

更改

std::cout<<"Name: "<<m_strName<<"id: "<<m_id<<"wage: $"<<wage<<std::endl;
Run Code Online (Sandbox Code Playgroud)

std::cout<<"Name: "<<m_strName<<"id: "<<m_id<<"wage: $"<<m_wage<<std::endl;
                                                         ^^^^^^
Run Code Online (Sandbox Code Playgroud)