Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++中访问另一个类中的向量对象 我是C++的新手。我创建了两个类。一个类A将把结构数据推到向量,而其他类B将从向量中弹出数据。_C++_C++11 - Fatal编程技术网

如何在C++中访问另一个类中的向量对象 我是C++的新手。我创建了两个类。一个类A将把结构数据推到向量,而其他类B将从向量中弹出数据。

如何在C++中访问另一个类中的向量对象 我是C++的新手。我创建了两个类。一个类A将把结构数据推到向量,而其他类B将从向量中弹出数据。,c++,c++11,C++,C++11,如何将向量的引用从类A传递到类B,以便类B可以指向同一个向量对象以弹出数据,从而进行一些操作 谁能帮我解决这个问题 到目前为止,我的努力是, A.h文件: struct strctOfA { int x; int y; int z; }; class A { public: A(); private: std::vector<strctOfA> t2; }; B.h文件 clas

如何将向量的引用从类A传递到类B,以便类B可以指向同一个向量对象以弹出数据,从而进行一些操作

谁能帮我解决这个问题

到目前为止,我的努力是, A.h文件:

struct strctOfA {
       int x;
       int y;
       int z;
    };  


class A {
public:

A();        
private:
     std::vector<strctOfA> t2;
};
B.h文件

         class B {

        public:
             B();
             functionOfB(&t2);
        };
B.cpp:

 B::functionOfB(A &t) {
    t2.pop_front(); 
    }

使用一个friend类,这是一个在另一个类中用关键字friend声明为friend的类。它可以访问其他类的私有和受保护成员。允许特定类访问另一个类的私有成员非常有用。 例如:

a、 h

b、 h

b、 cpp

main.cpp

int main() {
    A instanceOfA;
    B *instanceOfB = new B();
    instanceOfB->functionOfB(instanceOfA);
    return 0;
}

我发现typedef很有用:即typedef std::vector strucOfAVec\t;现在可以轻松地传递向量:用于声明t2:strucoffec_t t2,b用来通过ref传递到func init?或ctor:…,strucOfAVec_t&x。。。
typedef struct strctOfA {
   int x;
   int y;
   int z;
}positions;
class A {
public:
    A();
    friend class B;
private:
    strctOfA player;
    std::vector<positions> t2;
};
    A::A() {
    player.x=1;
    player.y=2;
    player.z=3;
    t2.push_back(player);
}
class B {
public:
    B();
    void functionOfB(A &x);
};
B::B() {
}
void B::functionOfB(A &x) {
    x.t2.pop_back();
}
int main() {
    A instanceOfA;
    B *instanceOfB = new B();
    instanceOfB->functionOfB(instanceOfA);
    return 0;
}