【问题标题】:How to return a vector of structs of a function in c++如何在c ++中返回函数的结构向量
【发布时间】:2019-07-14 06:29:26
【问题描述】:

我正在尝试从我的班级返回一个结构向量,但遇到了一些错误。这是我到目前为止的设置:

class trial{

   public:
    struct coords{
      double x,y,z;
    };
    vector<coords> trial::function(vector <double> x1, vector <double> x2);
};

std:vector<coords> function(vector <double> x1, vector <double> x2){

    some math.....
    vector <coords> test;
    return test;
}

错误出现在 st::vector.... 说 coords 没有定义。有什么想法吗?

【问题讨论】:

    标签: c++ function c++11 function-prototypes


    【解决方案1】:

    你的意思是,在课外定义?应该是(前提是你是using std::vector;

    vector<trial::coords> trial::function(vector <double> x1, vector <double> x2){
    

    在前面的课堂声明中,不需要资格trial::

    【讨论】:

      【解决方案2】:

      一个可以用来避免在返回类型中完全命名trial::coords 的技巧是尾随返回值:

      // still need the first `trial::`, but the one on `coords` is inferred
      auto trial::function(vector <double> x1, vector <double> x2) -> vector<coords> {
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-06
        • 1970-01-01
        • 2013-10-12
        • 2019-08-21
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 2020-05-31
        相关资源
        最近更新 更多