i want to add a overloaded assignment operator to my object class in c but when I do this
Cabinet& Cabinet::operator=( const Cabinet& right ) {
if(&right != this){
for (int i = 0; i < rows; i )
{
for (int j = 0; j < columns; j )
{
this->chemicals[i][j] = right.chemicals[i][j];
}
}
}
return *this;
}
and with a header file like this
using namespace std;
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Chemical.h"
class Cabinet{
private:
int rows;
int id_cabinet;
int columns;
Chemical*** chemicals;
string alphabet [9];
public:
Cabinet(int id = 0, int rows = 0, int columns = 0);
~Cabinet();
int getRow();
int getColumn();
int plusCount();
};
when compiling I get a compile error that says:
Cabinet.cpp:146:19: error: definition of implicitly declared copy assignment operator
CodePudding user response:
You need to declare the function in your header file so you can define it later on.
using namespace std;
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Chemical.h"
class Cabinet{
private:
int rows;
int id_cabinet;
int columns;
Chemical*** chemicals;
string alphabet [9];
public:
Cabinet(int id = 0, int rows = 0, int columns = 0);
Cabinet& operator=( const Cabinet& right );
~Cabinet();
int getRow();
int getColumn();
int plusCount();
};