Practical solution to: "non-static reference member .... can't use default assignment operator|"

Hello, I have the following constructor:

1
2
Bbox (float min_x,float min_y,float min_z,float max_x,float max_y,float max_z,vector <Triangle>& tri_ref): min_X (min_x), min_Y (min_y), min_Z (min_z)
, max_X (max_x), max_Y (max_y), max_Z (max_z), tri_vec_ref (tri_ref) {}


where the last variable, tri_vec_ref, is a reference to a vector of another type, of Triangle:

vector <Triangle>& tri_vec_ref;

As I understand every time a call is made to the constructor, I receive this error message:

/media/34GB/demos/asmfrt-master/Bbox.h|6|error: non-static reference member ‘std::vector<Triangle, std::allocator<Triangle> >& Bbox::tri_vec_ref’, can't use default assignment operator|

I read that a solution is changing the variable type from a reference to a pointer, and it does work. Nonetheless, it would require changes in several other code pieces, so I was wondering is there is a more practical solution, one that does not require me to change code in many different classes.
you don't have to change something to a pointer to get a pointer.

int x;
foo(&x);//pass some dumb function foo a pointer to x because it wants int*

That may or may not help here?
Last edited on
Do you have a move assignment operator overload for Bbox?

1
2
3
4
5
6
Bbox& operator=( Bbox&& other )
{
    tri_vec_ref = std::move(other.tri_vec_ref);
    // assign other varibles
    return *this;
}

BTW, you might try passing max and min as 3d points instead of separate coords.

1
2
Bbox( Point min, Point max, vector< Triangle >& tri_ref )
    : min( min ), max( max ), tri_vec_ref( tri_ref ) { }

It would be really helpful if you posted some of Bbox.h and also show more of class Bbox, including the definition of the members. Also the full error message would help.

Without seeing the code, it's just speculation. And this is particularly unhelpful because the error message says exactly what's wrong.
Are you sure you're calling the constructor and not hte assignment operator? As kbw said, show the rest of the code.
It might be worth looking into Triangle. Your error message said that the default operator= cannot be used on std::vector<Triangle>. Did you define Triangle in such a way that the vector<Triangle>::operator= doesn't work?
This compiles:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>


class Triangle {};


class Bbox {
public:
    Bbox ( double, double, double, double, double, double, std::vector<Triangle>& );

private:
    double min_x {};
    double min_y {};
    double min_z {};
    double max_x {};
    double max_y {};
    double max_z {};
    std::vector<Triangle> & tri_ref;

// friends:
    friend std::ostream& operator << (std::ostream&, const Bbox&);
};


Bbox::Bbox ( double min_x_arg,
             double min_y_arg,
             double min_z_arg,
             double max_x_arg,
             double max_y_arg,
             double max_z_arg,
             std::vector<Triangle>& tri_ref_arg)
    : min_x { min_x_arg }
    , min_y { min_y_arg }
    , min_z { min_z_arg }
    , max_x { max_x_arg }
    , max_y { max_y_arg }
    , max_z { max_z_arg }
    , tri_ref { tri_ref_arg }
{
}


std::ostream& operator <<(std::ostream& os, const Bbox& rhs)
{
    os << "min_x: " << rhs.min_x <<  "; min_y: " << rhs.min_y <<  "; min_z: " << rhs.min_z
       <<  "\nmax_x: " << rhs.max_x <<  "; max_y: " << rhs.max_y <<  "; max_z: " << rhs.max_z
       << "\ntri_ref.size(): " << rhs.tri_ref.size();
    return os;
}


int main()
{
    std::vector<Triangle> v( 13 );
    Bbox bbox( 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, v);
    std::cout << bbox << '\n';
}


Output:
min_x: 1.1; min_y: 2.2; min_z: 3.3
max_x: 4.4; max_y: 5.5; max_z: 6.6
tri_ref.size(): 13

Strange. My code is similar to what Enoizat posted.

Here is the Bbox code:

1
2
3
4
5
6
7
8
9
10
11
12
class Bbox {
   public:
      Bbox (float min_x,float min_y,float min_z,float max_x,float max_y,float max_z,vector <Triangle>& tri_ref): min_X (min_x), min_Y (min_y), min_Z (min_z)
, max_X (max_x), max_Y (max_y), max_Z (max_z), tri_vec_ref (tri_ref) {}
         bool intersect (Vec3f& camera_dir, Vec3f& camera_ori, Vec3f& dist, Vec3f& s2, Bbox& s, float& v, int x, int y);
         Bbox* operator = (vector <Triangle>& tri_ref ) {
            tri_vec_ref = tri_ref;
         }
   private:
      float min_X,min_Y,min_Z,max_X,max_Y,max_Z;
      vector <Triangle>& tri_vec_ref;
};


I added the operator code to see if it would change something, but it didn't.

The error message:

1
2
3
/media/34GB/demos/asmfrt-master/Bbox.h|6|error: non-static reference member ‘std::vector<Triangle, std::allocator<Triangle> >& Bbox::tri_vec_ref’, can't use default assignment operator|
/usr/include/c++/4.4/bits/vector.tcc||In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Bbox, _Alloc = std::allocator<Bbox>]’:|
/usr/include/c++/4.4/bits/vector.tcc|312|note: synthesized method ‘Bbox& Bbox::operator=(const Bbox&)’ first required here | 


EDITED:

Tried Dutch's assignment operator code. It generated this error:
/media/34GB/demos/asmfrt-master/Bbox.h|11|error: expected ‘,’ or ‘...’ before ‘&&’ token|

So I think it was just a mistype. Removed one of the '&' and tried again. This time:

/media/34GB/demos/asmfrt-master/Bbox.h|12|error: ‘move’ is not a member of ‘std’|
Last edited on
1
2
3
Bbox* operator = (vector <Triangle>& tri_ref ) {
    tri_vec_ref = tri_ref;
}

If you’re trying to implement an overloaded copy assignment operator, that’s wrong.

1) Your method should return a Bbox and it returns nothing.

2) https://en.cppreference.com/w/cpp/language/copy_assignment
A copy assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T, T&, const T&, volatile T&, or const volatile T&.

Your method doesn’t comply.

Please post a compilable (or nearly compilable) code snippet which reproduces your error.
I'm not sure if he's trying to implement a copy assignment operator. He's trying to assign from a vector<Triangle>. There's nothing wrong with that.
dhayden wrote:
I'm not sure if he's trying to implement a copy assignment operator.

Me too, it was an if.
I’d not go with pointers that way, anyway; I’d prefer returning a Bbox& and add return *this at the end, but this is secondary.
> In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux
> [with _Tp = Bbox, _Alloc = std::allocator<Bbox>]
> note: synthesized method ‘Bbox& Bbox::operator=(const Bbox&)’ first required here
my stuffed turtle says that you have a vector<Bbox> v
`vector' expects its elements to be assignable, your Bbox has a reference so the default assignment operator does not work
you need a proper assignment operator for Bbox.
Topic archived. No new replies allowed.