【问题标题】:How do I access variables output of a scope in C++?如何在 C++ 中访问范围的变量输出?
【发布时间】:2020-06-10 15:31:12
【问题描述】:

例如,

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?

}

我想在 main() 中访问 getDepth() 输出的向量 positionX 和向量 positionY 的值,并且我想避免使用全局变量。有什么解决办法吗?

谢谢

【问题讨论】:

  • int depth = Obj.getDepth(); 有什么问题?
  • 定义“访问值”。哪些元素?所有两个向量?然后,您可以提供访问器来返回 const 引用或成员变量的副本。如果您想从一个函数中全部返回它们,那么只需返回包含所有感兴趣字段的std::tuplestruct。或者从getDepth() 返回它们,并以其他方式发出错误信号,例如通过抛出异常,而不是返回错误代码。
  • 喜欢所有会员访问; Obj.positionXObj.positionY。我认为您需要good book
  • 感谢您的回复。我想我错过了一些观点。我不确定我是否以正确的方式表达它。但是当我在 int main() 函数中使用 Obj.positionX 时,它不会访问 GetDepth_Class::getDepth() 内部的 positionX 输出,它只能访问类 GetDepth_Class 中定义的 positionX。我要做的就是使用 getDepth() 输出的 positionX 和 positionY 的值在 main() 中进行一些计算。谢谢。
  • 你想从getDepth()函数中获取这两个值并在main()中访问它吗?

标签: c++ scope


【解决方案1】:

由于您已将所有班级成员声明为公开,您可以直接通过Obj.positionX 等方式访问它们。这不是一个好的做法,但给出的示例代码是最简单的方法。

【讨论】:

  • 感谢您的回复。当我在 int main() 函数中使用 Obj.positionX 时,它不会访问 GetDepth_Class::getDepth() 内部的 positionX 输出,它只能访问类 GetDepth_Class 中定义的 positionX。
  • 当我在main()中点赞GetDepth_Class Obj; Obj.getDepth(); for (int i = 0; i &lt; Obj.positionX.size(); i++) { cout &lt;&lt; Obj.positionX[i] &lt;&lt; "," &lt;&lt; Obj.positionY[i] &lt;&lt; endl; } //swap (clear) the elements of vector and clear the memory vector &lt;int&gt;().swap(Obj.positionX); vector &lt;int&gt;().swap(Obj.positionY);时,它没有输出任何值。
【解决方案2】:

您也可以使用struct 来返回多个值:

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

我知道这有点混乱但可以理解。

我设置了{positionX, positionY} 并返回它并使用循环在main() 中读取它。

样本输出:

80,74 // printed from class function
80    // positionX from main()
74    // positionY from main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多