如何 return c++ 中函数的结构向量

How to return a vector of structs of a function in c++

我正在尝试 return 来自我的 class 的结构向量,但我遇到了一些错误。这是我到目前为止的设置:

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... 说坐标未定义。有什么想法吗?

你是说,在out-of-class定义中?应该是(前提是你using std::vector;

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

在之前的in-class声明中,不需要资格trial::

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

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