Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++中设置一些简单的类。我正在尝试创建一个订单类型,它接受价格(double)、数量(int)和样式(std::string)_C++_String_Class_Cout - Fatal编程技术网

C++;传递给构造函数的变量未正确传递 我只是想在C++中设置一些简单的类。我正在尝试创建一个订单类型,它接受价格(double)、数量(int)和样式(std::string)

C++;传递给构造函数的变量未正确传递 我只是想在C++中设置一些简单的类。我正在尝试创建一个订单类型,它接受价格(double)、数量(int)和样式(std::string),c++,string,class,cout,C++,String,Class,Cout,这是我点的菜 #ifndef ORDER_H #define ORDER_H #include <string> #include <iostream> class Order { private: double limit_price; int quantity; std::string style; public: Order(); Order(double pric

这是我点的菜

#ifndef ORDER_H
#define ORDER_H

#include <string>
#include <iostream>


class Order {
    private:
        double limit_price;
        int quantity;
        std::string style;
    public:
        Order();
        Order(double price, int quantity, std::string style);

        void print_price();
        void print();
};
#endif
正如我所料,我得到了如下结果

bid 0@0
 -1722935952@12.3

每次运行时,较大的负数都会发生变化。

在构造函数中,参数的名称会影响成员:

Order::Order(double price, int quantity, std::string style){
    limit_price = price;
    quantity = quantity;
    style = style;
}
Order::Order(double limit_price, int quantity, std::string style) : limit_price(limit_price),quantity(quantity),style(style)
{}
你认为第二行的数量是多少?为什么它应该在左手边和右手边有所不同

数量
指的是参数

您可以将
this->
预先发送给成员以消除歧义,尽管这很少见。在这种情况下,最好重命名其中一个

无论如何,您应该初始化成员,而不是在构造函数体中赋值。在执行构造函数主体之前初始化成员。初始化成员的一种方法是成员初始值设定项列表:

Order::Order(double p, int q, std::string s) : limit_price(p),quantity(q),style(s)
{ /* empty body */ }
由于在初始值设定项列表中没有歧义的危险,我们可以使用与成员相同的参数名称:

Order::Order(double price, int quantity, std::string style){
    limit_price = price;
    quantity = quantity;
    style = style;
}
Order::Order(double limit_price, int quantity, std::string style) : limit_price(limit_price),quantity(quantity),style(style)
{}

在构造函数中,参数的名称会影响成员:

Order::Order(double price, int quantity, std::string style){
    limit_price = price;
    quantity = quantity;
    style = style;
}
Order::Order(double limit_price, int quantity, std::string style) : limit_price(limit_price),quantity(quantity),style(style)
{}
你认为第二行的数量是多少?为什么它应该在左手边和右手边有所不同

数量
指的是参数

您可以将
this->
预先发送给成员以消除歧义,尽管这很少见。在这种情况下,最好重命名其中一个

无论如何,您应该初始化成员,而不是在构造函数体中赋值。在执行构造函数主体之前初始化成员。初始化成员的一种方法是成员初始值设定项列表:

Order::Order(double p, int q, std::string s) : limit_price(p),quantity(q),style(s)
{ /* empty body */ }
由于在初始值设定项列表中没有歧义的危险,我们可以使用与成员相同的参数名称:

Order::Order(double price, int quantity, std::string style){
    limit_price = price;
    quantity = quantity;
    style = style;
}
Order::Order(double limit_price, int quantity, std::string style) : limit_price(limit_price),quantity(quantity),style(style)
{}

为了编译,我只使用命令
g++tester.cpp-o tester
,其中tester.cpp是我的测试文件的名称
quantity=quantity你认为这能做什么?为什么您认为它会这样做而不是做其他事情呢?
#include“order.cpp”
是一个禁忌。include头文件(the.h)编译并链接cpp文件。阅读后,你会发现不幸很少教授的方法对解决这一混乱问题非常有帮助。要编译,我只需使用命令
g++tester.cpp-o tester
,其中tester.cpp是我的测试文件名
quantity=quantity你认为这能做什么?为什么您认为它会这样做而不是做其他事情呢?
#include“order.cpp”
是一个禁忌。include头文件(the.h)编译和链接CPP文件。也会发现,不幸的是很少有人教的帮助解决混乱。我想知道是否有任何建议,为初始化列表的参数与成员相同的名字。@ Barmar:我不认为有C++。在C#中有一个“记录类型”的建议,它将自动存储所有构造函数参数。它们被添加到编译器的技术预览版本中,在发布(我想是5.0?)之前再次删除,看起来将成为下一版本(9.0)的一部分@Barmar hm不确定是否值得增加复杂性。构造函数的一种特殊情况的新语法。另一方面,这是一条相当常见的三重重复线。这是一种特殊情况,但也是一种常见情况。我可以想象像
:()
@Barmar这样的语法实际上是一个有趣的想法。初始化的顺序总是正确的,错误的一个来源是不知道是否有任何建议,为初始化列表的参数与成员的名字相同。@ Barmar:我不认为有C++。在C#中有一个“记录类型”的建议,它将自动存储所有构造函数参数。它们被添加到编译器的技术预览版本中,在发布(我想是5.0?)之前再次删除,看起来将成为下一版本(9.0)的一部分@Barmar hm不确定是否值得增加复杂性。构造函数的一种特殊情况的新语法。另一方面,这是一条相当常见的三重重复线。这是一种特殊情况,但也是一种常见情况。我可以想象像
:()
@Barmar这样的语法实际上是一个有趣的想法。初始化的顺序总是正确的,消除了一个错误源