1

For example,

class GetDepth_Class {
    public:
        vector<int> positionX, positionY;
        vector<int>::iterator it;
        int getDepth();
    };

int GetDepth_Class::getDepth(){
                ......
                ......
                if (scalar[0] == 0 && scalar[1] == 0 && scalar[2] == 0) {

                    it = positionX.begin();
                    positionX.insert(it, i + 80);

                    it = positionY.begin();
                    positionY.insert(it, j + 80);
                }

                for (int i = 0; i < positionX.size(); i++) {
                    cout << positionX[i] << "," << positionY[i] << endl;
                }


return EXIT_SUCCESS;//realsense depth camera module
}

    int main() {

    GetDepth_Class Obj;
    Obj.getDepth();

    //Here I would like to access the values of vector positionX and vector positionY output from GetDepth_Class::getDepth(), 
    //how should I do if I want to avoid using global variable?

}

I would like to access the values of vector positionX and vector positionY output from getDepth() in main(), and I want to avoid using global varible. Is there any solution for it?

Thanks

TED_EE
  • 13
  • 3
  • 3
    What's wrong with `int depth = Obj.getDepth();`? – NathanOliver Jun 10 '20 at 15:32
  • Define "access the values". Which elements? All of both vectors? Then you can provide accessors to return const-references or copies of the member variables. If you want to return them all from one function, then just return a `std::tuple` or `struct` containing all the fields of interest. Or return them from `getDepth()`, and signal error some other way, e.g. by throwing an exception, instead of returning an error code. – underscore_d Jun 10 '20 at 15:37
  • Like all member accesses; `Obj.positionX` and `Obj.positionY`. I think you're in need of a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jun 10 '20 at 15:44
  • Thanks for your replying. I think I've missed some points. I am not sure if I express it in a right way. But when I used Obj.positionX in int main() function, it does not access the positionX output inside GetDepth_Class::getDepth(), it can only access the positionX defined in class GetDepth_Class. All I want to do is use the values of positionX and positionY output by getDepth() to do some calculation in main(). Thanks. – TED_EE Jun 10 '20 at 16:32
  • Do you want to get the two values from `getDepth()` function and access it in `main()`? – Rohan Bari Jun 10 '20 at 16:35
  • When I did like `GetDepth_Class Obj; Obj.getDepth(); for (int i = 0; i < Obj.positionX.size(); i++) { cout << Obj.positionX[i] << "," << Obj.positionY[i] << endl; } //swap (clear) the elements of vector and clear the memory vector ().swap(Obj.positionX); vector ().swap(Obj.positionY);` in the main(), it does not output any value. – TED_EE Jun 10 '20 at 16:35
  • Hi, Rohan Bari. Definitely right. – TED_EE Jun 10 '20 at 16:36

2 Answers2

2

Since you have declared all your class members as public, you can just access them directly via Obj.positionX etc. Not good practice, but given your example code that is the simplest way to do it.

James Telfer
  • 106
  • 1
  • 11
  • Thanks for your replying. When I used Obj.positionX in int main() function, it does not access the positionX output inside GetDepth_Class::getDepth(), it can only access the positionX defined in class GetDepth_Class. – TED_EE Jun 10 '20 at 16:24
  • When I did like `GetDepth_Class Obj; Obj.getDepth(); for (int i = 0; i < Obj.positionX.size(); i++) { cout << Obj.positionX[i] << "," << Obj.positionY[i] << endl; } //swap (clear) the elements of vector and clear the memory vector ().swap(Obj.positionX); vector ().swap(Obj.positionY);` in the main(), it does not output any value. – TED_EE Jun 10 '20 at 16:42
0

You can use a struct for returning multiple values too:

#include <iostream>
#include <vector>

using namespace std;

// using a struct for returning multiple variables
struct Decl
{
    vector<int> posX;
    vector<int> posY;
};

class GetDepth_Class
{
    // these are private members now
    vector<int> positionX, positionY;
    vector<int>::iterator it;

public:
    Decl getDepth();
};

Decl GetDepth_Class::getDepth()
{
    Decl d;

    it = positionX.begin();
    positionX.insert(it, 80);

    it = positionY.begin();
    positionY.insert(it, 74);

    d = {positionX, positionY};

    for (int i = 0; i < positionX.size(); i++)
        cout << positionX[i] << "," << positionY[i] << endl;

    return d;
}

int main()
{

    GetDepth_Class Obj;
    Decl x = Obj.getDepth();

    for (int i = 0; i < x.posX.size(); i++)
        cout << x.posX[i] << ' ';
    cout << endl;

    for (int i = 0; i < x.posY.size(); i++)
        cout << x.posY[i] << endl;

    return 0;
}

I know it's a little bit messy but understandable.

I had set {positionX, positionY} and returned it and read it in main() using a loop.

Sample Output:

80,74 // printed from class function
80    // positionX from main()
74    // positionY from main()
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34