ghostrider
ghostrider

Reputation: 5259

C++ : use of deleted function

I am doing circles around trying to fix this but still no luck. I am sure I am confusing basic C++ stuff so need your help.

class TypeA {
  TypeA( const int id ) ;
  TypeA() ;
  private :
     int n_id ;
}

then on my class B**.h**

class TypeB :
   TypeB( const int x , const int y ) ;
   TypeB( const int x , const int y , const TypeA& a) ;
   private :
       int _x ;
       int _y ;
       TypeA _a ; 

I am having issues on my second constructor.

.cpp

TypeB( const int x , const int y , const TypeA& a) : _x( x) , _y(y) {
   _a = a ;
}

and I am getting this error :

use of deleted function TypeA::operator=(TypeA&)
note : TypeA::operator=(TypeA&) is implicity deleted because the default definition would be ill-formed
class TypeA

Any ideas on why this is happening?

Edit : I tried this :

TypeB( const int x , const int y , const TypeA& a) : _x( x) , _y(y) , _a(a) { }

and now error becomes :

use of deleted function TypeA&  _a(a)
note : TypeA is implicity deleted because the default definition would be ill-formed. 
class TypeA

does that mean that the problem then lies into my default constructor of my typeA ?

Upvotes: 0

Views: 476

Answers (2)

Antonio
Antonio

Reputation: 651

You have to define 'public' your constructors!!! If you don't write 'public', by default, everything is private in a class:

class TypeA {
  public:  // <--- you need this!!!
  TypeA( const int id ) ;
  TypeA() ;
  private :
 int n_id ;
};

And don't pass 'const int id', only 'int id'. 'id' is passing by value, so why do you need it to be const?

Upvotes: 0

rak007
rak007

Reputation: 1011

Give your TypeA class a constructor (even default if you want) and change your type B constructor. Remember that class attributes and functions are private by default

Full answer :

class TypeA {
public :
  TypeA() = default;
  ~TypeA() = default;
private :
  int n_id ;
};

class TypeB {
public :
    TypeB(const int x ,const int y);
    TypeB(const int x ,const int y ,const TypeA& a);
    ~TypeB() = default;
private :
    int _x;
    int _y;
    TypeA _a; 
};

TypeB::TypeB(const int x ,const int y ,const TypeA& a) : _x( x) , _y(y), _a(a) {
}

int main(void)
{
    TypeA test;
    TypeB hello(10, 10, test);
}

Upvotes: 2

Related Questions