向一个向量索引添加多个int? 'Cout'无法正常工作

时间:2016-05-06 05:52:34

标签: c++ vector

所以我在这里找到了我正在使用的代码:

=IF(A3="X",SUM(A:A)-A2,IF(B3="X",SUM(B:B)-B2,IF(C3="X",SUM(C:C)-C2,IF(D3="X",SUM(D:D)
-D2,IF(E3="X",SUM(E:E)-E2,IF(F3="X",SUM(F:F)-F2,IF(G3="X",SUM(G:G)-G2,"")))))))

基本上,我试图将多个int整合到一个向量索引中。 当我尝试打印矢量的内容时,onClick="doSomething();doSomethingElse();" 无效。

首先,我是否正在做这个多元的东西吧?我对C ++比较陌生。

如果我这样做,那么为什么#include <iostream> #include <cstdlib> #include <vector> using namespace std; struct Something{ int x; int y; }; int main() { vector <Something> v; int x, y; cin >> x >> y; Something temp; temp.x = x; temp.y = y; v.push_back(temp); for (size_t i = 0; i < v.size(); i++) { cout << v[i] << endl; // Error: No operator "<<" matches these operands. Operand types are std::ostream << Something } } 无效的任何想法? 我也试过了cout但这没效果。知道cout发生了什么事吗?非常感谢。

修改: 非常感谢你到目前为止。我还有一个问题。如果我要修改我的代码以获取多个输入,然后希望向量中的所有内容按照“y”从最大到最小排序。

示例(原始矢量内容(x,y))

v.push_back({x,y})

根据'y'(从大到小)进行排序

cout

我知道如何根据第二个数字(y)进行常规排序而不是常规排序。我怎么做?非常感谢。

4 个答案:

答案 0 :(得分:2)

就像错误所说的那样,没有为你的结构声明重载的std::cout << v[i].x << ' ' << v[i].y << '\n'; 函数。

有三种可能的解决方案:第一种是输出结构的每个成员,比如

operator<<

另一种方法是创建一个执行上述操作的函数,作为成员函数或非成员函数。

第三个解决方案是为结构创建一个重载的std::ostream& operator<<(std::ostream& os, Something const& something) { return os << something.x << ' ' something.y; } 函数:

    //Simple Dropzonejs 
         $("#dZUpload").dropzone({
             url: "hn_SimpeFileUploader.ashx",
             maxFiles: 100,
             maxFilesize: 2,
             addRemoveLinks: false,
             acceptedFiles: "application/pdf",
             autoProcessQueue: false,                  
             success: function(file, response) {
                  var submit2 = document.getElementById('<%= UploadButton.ClientID %>');
                  submit2.disabled = false;     
                 var imgName = response;
                 file.previewElement.classList.add("dz-success");
                 console.log("Successfully uploaded :" + imgName);

                 var totalfilesize = 0;
                 var obj = {};
                 obj.name = file.name;
                 $.ajax({
                     type: "POST",
                     url: "DUpload.aspx/SendP",
                     data: JSON.stringify(obj),
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function(r) {
                         //alert(r.d);
                     }
                 });

             },
             error: function(file, response) {
                 file.previewElement.classList.add("dz-error");
             }
         });


     });

我建议您find a good beginners book on C++阅读有关输出,结构和类以及运算符和运算符重载的章节。

答案 1 :(得分:0)

cout对你的结构不起作用,因为它没有被定义为这样做。 我想你可以在这里找到答案。 Overloading operators in typedef structs (c++)

答案 2 :(得分:0)

friend std::ostream& operator<<(std::ostream& out, Something const& s); 与用户定义类的对象之间不会自动提供

cout << v[i] << endl; 。在使用之前需要对它们进行定义。

在您的情况下,您需要在结构定义的主体

中定义友元函数
std::ostream& operator<<(std::ostream& out, Something const& s)
{
    return out << s.x << " " << s.y;
}

能够使用

Something s;
std::cin >> s;

该功能的实施并不太难。

{{1}}

如果您希望能够使用:

{{1}}

你必须定义一个类似的功能。

答案 3 :(得分:0)

问题是<<是一个运营商。对于许多类型,它在std::cout(对于std::ostream)上定义。例如,它是为int定义的,这就是为什么这样做的原因:

std::cout << 3;

但是,您尝试将operator<<应用于您自己的Something类,但它没有此类运算符定义。您可以这样添加:

struct Something {
    // Other stuff here
    friend std::ostream& operator<<(std::ostream& os, Something const& smth) ;
};

std::ostream& operator<<(std::ostream& os, Something const& smth) {
    os << "(" << smth.x << ", " << smth.y << ")";
    return os;
}

那么你的代码应该可行。重载此运算符实际上比重载其他运算符更困难,因为您需要了解friend的作用。

修改

在您的情况下,实际上不需要

friend,因为xy是公开的。但在一般情况下,您需要它才能打印您班级的私人成员。

编辑2:

要清除上一个编辑,可以省略运算符的整个结构内声明,因为它将在全局范围内查找。如果操作员需要阅读私人成员,您只需要它(然后它需要friend)。