本文介绍了递归push_back不适用于自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我确定它已被问过100次,但 push_back 是这样一个流行的问题,我找不到一个长的答案时间。 我的问题是在此页面它说,递归 push_back 应该在多个级别上完全没有任何问题。 vector< vector< int> > vI2Matrix; // Declare two dimensional array vector< int> A,B; A.push_back(10); A.push_back(20); A.push_back(30); B.push_back(100); B.push_back(200); B.push_back(300); vI2Matrix.push_back(A); vI2Matrix.push_back(B);但是在我的版本中,我试图使用一个自定义类 Vector3f code>而不是 int 。我自然认为以下代码将工作,但它不会。它在第一级,但不在第二级。 矢量< vector< Vector3f> > m; vector< Vector3f>一个; a.push_back(Vector3f(1,2,3)); //< - 第一级工程 m.push_back(a); // 返回的错误代码是: gobase.h:343:错误:在'* __result = * __first'中不匹配'operator =' demo1.h: 38:注意:候选人是:Vector3f& Vector3f :: operator =(Vector3f&) Vector3f 类在外部头文件中定义,我不应该修改。你认为给定的头文件中缺少什么使我无法使用 push_back ? 函数定义在头文件中: 类Vector3f { float _item [3]; public: float&运算符[](int i) Vector3f(float x,float y,float z) Vector3f() Vector3f& operator =(Vector3f& obj) Vector3f& operator + =(Vector3f& obj) bool operator ==(Vector3f& obj)} 更新 这里是& Vector3f& operator =(Vector3f& obj) { _item [0] = obj [0]; _item [1] = obj [1]; _item [2] = obj [2]; return * this; };你可以告诉我如何修改这个来使 push_back c $ c>工作? 查看在GCC中解决这个问题所需的完整步骤,我将其作为一个额外的答案。 解决方案 Vector3f类中缺少赋值运算符。向量的push_back的模板扩展需要一个赋值运算符将值复制到向量中,因此编译器会报告它找不到它。如果您要修改该标题,您还需要定义一个 Vector3f(cont Vector3f& other)复制构造函数。 I'm sure it has been asked 100 times, but push_back is such a popular question that I couldn't find an answer after searching for a long time.My problem is that on this page it says that that a recursive push_back should work perfectly without any problem on multiple levels.vector< vector<int> > vI2Matrix; // Declare two dimensional arrayvector<int> A, B;A.push_back(10);A.push_back(20);A.push_back(30);B.push_back(100);B.push_back(200);B.push_back(300);vI2Matrix.push_back(A);vI2Matrix.push_back(B);However in my version I am trying to use a custom class Vector3f instead of int. I would naturally think that the following code would work, but it doesn't. It works on the 1st level, but not on the 2nd.vector< vector<Vector3f> > m;vector<Vector3f> a;a.push_back(Vector3f(1,2,3)); // <- 1st level worksm.push_back(a); // <- 2nd level doesn'tThe error code returned is:gobase.h:343: error: no match for ‘operator=’ in ‘* __result = * __first’demo1.h:38: note: candidates are: Vector3f& Vector3f::operator=(Vector3f&)The Vector3f class is defined in an external header file, what I am not supposed to modify. Do you think there is something missing from the given header file what makes me unable to use push_back?The following functions are defined in the header file:class Vector3f { float _item[3];public: float & operator [] (int i) Vector3f(float x, float y, float z) Vector3f() Vector3f & operator = (Vector3f & obj) Vector3f & operator += (Vector3f & obj) bool operator ==(Vector3f & obj)}UpdateHere is the & operator = from the header file:Vector3f & operator = (Vector3f & obj) { _item[0] = obj[0]; _item[1] = obj[1]; _item[2] = obj[2]; return *this;};Can you tell me how should I modify this to make push_back work?Have a look at the complete steps required to solve this problem in GCC, I posted it as an additional answer. 解决方案 The assignment operator is missing from your Vector3f class. Template expansion of vector's push_back needs an assignment operator to copy the value into the vector, so the compiler complains that it cannot find it. If you get to modify that header, you will also need to define a Vector3f(cont Vector3f& other) copy constructor. 这篇关于递归push_back不适用于自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 09:04