【问题标题】:Use Member functions of array which contains struct使用包含结构的数组的成员函数
【发布时间】:2017-07-14 11:12:18
【问题描述】:

例如,我用结构制作了一个数组:

#include <opencv2/opencv.hpp>
#include "utils/Configuration.h"
#include <time.h>
using namespace std;
using namespace cv;    
struct plausibilitylinestruct{
        int x0;
        int y0;
        int x1;
        int y1;
        float p;
    } plausibilitylines[10];

如何在创建的数组上使用成员函数? C++ Refernce for array

我试过例如:

void addPlausibilitylines(vector<Vec4i> line) {
    if(plausibilitylines.empty()) {
        cout << "Test"<< endl;
    }
}

编辑:我添加了更多代码

【问题讨论】:

  • 你拥有的是一个原始的 C 数组。它没有任何成员函数。
  • plausibilitylines.empty() 无效,因为 plausibilitylines 不是对象。您链接到的参考是std::array,您在这里没有使用。
  • 您链接到的参考是 类模板 std::array。您定义的是一个简单的标准 C 样式数组。这两个是不同的。我建议你find some good beginners books阅读。

标签: c++ arrays struct member-functions


【解决方案1】:

您只是混淆了 C 样式的数组和自 C++11 以来添加的 std::array

C 风格的数组显然没有方法。相反,std::array 是一个拥有自己的方法集的容器。

要使用std::array,您必须将您的解决方案转换为:

std::array<plausibilitylinestruct, 10> plausibilitylines;

然后使用empty()之类的方法:

plausibilitylines.empty();

【讨论】:

  • 一个 std::array 是空的当且仅当它是用大小 0 构造的。我不认为这就是 OP 想要归档的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 2019-05-06
  • 2017-08-07
相关资源
最近更新 更多