Steven
Steven

Reputation: 105

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

I am trying to return a vector of structs from my class, but am getting a few errors. This is what i have setup so far:

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;
}

The error comes at the st::vector.... saying coords was not defined. Any thoughts?

Upvotes: 0

Views: 373

Answers (2)

Eric
Eric

Reputation: 97601

One trick you can use to avoid needing to name trial::coords fully in the return type is a trailing return value:

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

Upvotes: 0

bipll
bipll

Reputation: 11940

You mean, in out-of-class definition? It should be (provided you're using std::vector;)

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

In in-class declaration earlier, qualification trial:: is not needed.

Upvotes: 1

Related Questions