Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试在c+中使用类和定义变量+;头文件_C++_Visual Studio - Fatal编程技术网

C++ 尝试在c+中使用类和定义变量+;头文件

C++ 尝试在c+中使用类和定义变量+;头文件,c++,visual-studio,C++,Visual Studio,我试图创建一个类,并在头文件中定义它的变量。所有成员的数据必须是私有的。我想我让这个类工作起来了,我只是无法定义变量。 这是我的密码: class myMember { public: myMember(); private: myMember(char name, double height, double weight, int salary); }; myMember::myMember(); { name = "Joe"; height = 5.10;

我试图创建一个类,并在头文件中定义它的变量。所有成员的数据必须是私有的。我想我让这个类工作起来了,我只是无法定义变量。 这是我的密码:

class myMember
{
public:
    myMember();
private:
    myMember(char name, double height, double weight, int salary);
};

myMember::myMember();
{
    name = "Joe";
    height = 5.10;
    weight = 170;
    salary = 100;
}

您需要在类中声明类的字段:

class myMember
{
public:
    myMember();
private:
    std::string name;
    double height;
    double weight;
    int salary;
};
您添加的私有成员是一个构造函数,而不是一组字段。如果要将其用作构造函数,请将其公开,并将名称更改为字符串

public:
    myMember( std::string name, double height, double weight, int salary);

如果希望类
myMember
具有在构造函数中设置的成员变量,则需要这样声明:

class myMember
{
public:
    myMember();
private:
    std::string name ;
    double height, weight ;
    int salary ;
};

因为这是C++,我会使用<代码> STD::String 为<代码> No.<代码>变量,否则我将类型保持不变。

所以你想<代码> MyMys<代码>有成员变量<代码>名称<代码>,<代码>高度> /代码>……?你的问题是什么?您发布的类定义不包含任何数据成员,因此您试图初始化什么?@Shafik Yaghmour是的,很抱歉,这是我第一次使用类,所以idk使用了很多术语