How to make a copy constructor? - c++

I want to make a copy constructor for this class RNA to copy the details form another object RNA
#include "RNA.h"
#include"Sequence.h"
#include<bits/stdc++.h>
using namespace std;
RNA::RNA()
{
set_sequence();
}
RNA::RNA(char * seq, RNA_Type atype)
{
int x;
int i=0;
while(1)
{
if(seq[i] != 'C'&&seq[i] != 'G'&&seq[i] != 'A'&&seq[i] != 'U')break;
x++;
i++;
}
x--;
length = x;
this->seq = new char[length];
for(int i=0;i<length;i++)
{
this->seq[i] = seq[i];
}
type = atype;
}
this is the copy constructor
RNA::RNA( RNA& rhs)
{
seq = new char[length];
for(int i=0;i<length;i++)
{
seq[i] = rhs.seq[i];
}
type = rhs.type;
}
in the main I try to do it and it make error
int l;
cin>>l;
char* arr = new char[l];
for(int i=0;i<l;i++)
{
cin>>arr[i];
}
cin>>l;
RNA anas(arr,(RNA_Type)l);
int s;
cin>>s;
char* arr2 = new char[s];
for(int i=0;i<s;i++)
{
cin>>arr2[i];
}
cin>>s;
RNA saeed(arr2,(RNA_Type)s);
saeed(anas); error is here
saeed.Print();
The error is " No match for call to '(RNA) (RNA&)'
so what can i do to solve this error

The simplest way is to let the compiler do it for you.
class RNA
{
RNA_Type type;
std::string seq;
public:
RNA(std::string = /* default seq */, RNA_Type = /* default type */);
/* implicitly generated correctly
~RNA();
RNA(const RNA &);
RNA & operator = (const RNA &);
RNA(RNA &&);
RNA & operator = (RNA &&);
*/
// other members
};
RNA::RNA(std::string aseq, RNA_Type atype)
: seq(aseq.begin(), aseq.find_first_not_of("ACGT")), type(atype)
{}

saeed already exists at that point, and you're trying to use it as if it were a function.
You can't copy-construct except when you are constructing, and it looks like any other initialisation:
RNA saeed(anas);
or:
RNA saeed{anas};
If you want to replace the value of an already existing object, you use assignment (and you need to implement that assignment; read about The Rule Of Three.)

The line
saeed(anas);
is not a call to the constructor RNA::RNA(RNA const&), but to RNA::operator()(RNA const&), which does not exist.
What you presumably want is a copy assignement operator
RNA& RNA::operator=(RNA const&);
which for simple enough classes will be auto-generated by the compiler (as explained in Caleth's answer). To call it, replace your line with
saeed = anas;

Related

Issues with dynamic arrays, pointers and copy-constructors

I'm having a lot of issues with creating a dynamic array containing objects.
As I've understood it, because my array is handling objects, the class stored in the array must have a copy constructor or an assignment operator so that all will be copied properly.
I've successfully created this program with a normal array of defined size. Now I have a lot of problems creating the same program with a dynamic array.
Class 1 The objects to be stored:
class objToBeStored{
private:
string dataToBeStored;
int sizeOfArray;
string *storedArray;
public:
objToBeStored(); //empty constructor
objToBeStored& operator =(const objToBeStored& o); // assignment operator
~objToBeStored(); //destructor (no code inside);
bool getData(istream &stream);
//.....other methods to do stuff
};
objToBeStored::objToBeStored(){
//empty
}
objToBeStored& objToBeStored::operator=(const objToBeStored& o){
if(this != o){
dataToBeStored = o.dataToBeStored;
for (int i = 0; i < sizeOfArray; i++){
storedArray[i] = o.storedArray[i];
}
}
return *this;
}
void objToBeStored::getData(istream &stream){
stream >> dataToBeStored >> sizeOfArray;
storedArray = new string[sizeOfArray];
for(int i = 0; i < sizeOfArray; i++){
stream >> storedArray[i];
}
return !stream.eof();
}
//.....other methods to do stuff
Class 2 contains the dynamic array that stores the above objects. Everything is working,except how I declared my dynamic array and the functions handling it. Therefore I will write this code below:
class storageArrayClass{
private:
storageArrayClass *store;
storageArrayClass *storptr;
int numberOfstored;
public:
storageArrayClass(); //empty constructor
~storageArrayClass();
void addElm(objToBeStored & o);
//other functions to do stuff
};
storageArrayClass::storageArrayClass(){ //constructor
numberOfstored = 0;
}
storageArrayClass::~storageArrayClass(){
}
void storageArrayClass(istream &stream) {
objToBeStored o;
o.getData(stream);
if(numberOfstored == 0){ //check it this is the first element
store = new objToBeStored[1]; //create a new array with length 1
store[(numberOfstored] = o; //store object
}else{
objToBeStored tmpStore = new objToBeStored[(numberOfstored+1]; //create a temp. array with 1 more position
for(int i=0; i < numberOfstored; i++){
tmpStore[i] = store[i]; //copy original array to the temp. array
storptr = &tmpStore[i]; // increment a point
}
storptr++; //increment pointer to last position
*storptr = o; //store object in last position
delete[] store; //delete the original array
store = new objToBeStored[(numberOfstored+1]; //create a new original array
store = tmpStore;//copy temp. array
}
}
I manage to add 3 objects to my dynamic array before I get the following error:
Process returned -1073741819 (0xC0000005) execution time : 5.059 s
Please help. I've read countless threads here, but I cannot get it to work.

What is wrong with my park_car function?

I'm again doing a task for school and I'm implementing it slowly, I don't know why my park_car function is not working, I just wanted to make a test and the program crashes ... here is my code.
PS: I can't change the ***p2parkboxes because it is given in the starter file like most other variables. I just want to see the first element of Floor 0 as : HH-AB 1234. Your help is most appreciated.
PS2: I can't use the std::string as well it isn't allowed for the task.
#include <iostream>
#include <cstring>
using namespace std;
#define EMPTY "----------"
class Parkbox{
char *license_plate; // car's license plate
public:
Parkbox(char *s = EMPTY); // CTOR
~Parkbox(); // DTOR
char *get_plate(){return license_plate;}
};
class ParkingGarage{
Parkbox ***p2parkboxes;
//int dimensions_of_parkhouse[3]; // better with rows,columns,floors
int rows,columns,floors; // dimensions of park house
int total_num_of_cars_currently_parked;
int next_free_parking_position[3];
// PRIVATE MEMBER FUNCTION
void find_next_free_parking_position();
public:
ParkingGarage(int row, int col, int flr);// CTOR,[rows][columns][floors]
~ParkingGarage(); // DTOR
bool park_car(char*); // park car with license plate
bool fetch_car(char*); // fetch car with license plate
void show(); // show content of garage floor
// by floor
};
Parkbox::Parkbox(char *s ) { // CTOR
license_plate = new char[strlen(s)+1];
strcpy(license_plate, s);
//cout << "ParkBox CTOR" << endl;
}
Parkbox::~Parkbox() { // DTOR
delete [] license_plate;
//cout << "ParkBox DTOR" << endl;
}
ParkingGarage::ParkingGarage(int row, int col, int flr){
rows = row; columns = col; floors = flr;
p2parkboxes = new Parkbox**[row];
for (int i = 0; i < row; ++i) {
p2parkboxes[i] = new Parkbox*[col];
for (int j = 0; j < col; ++j)
p2parkboxes[i][j] = new Parkbox[flr];
}
}
ParkingGarage::~ParkingGarage(){
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j)
delete [] p2parkboxes[i][j];
delete [] p2parkboxes[i];
}
delete [] p2parkboxes;
}
void ParkingGarage::show(){
int i,j,k;
for (i = 0 ; i < floors; i++){
cout << "Floor" << i << endl;
for (j=0;j<rows;j++){
for (k=0;k<columns;k++){
cout << p2parkboxes[j][k][i].get_plate() << " ";
}
cout << endl;
}
}
}
bool ParkingGarage::park_car(char*s){
p2parkboxes[0][0][0] = Parkbox(s); //test
//p2parkboxes[0][0][0] = s; //test
return true;
}
int main(void) {
// a parking garage with 2 rows, 3 columns and 4 floors
ParkingGarage pg1(2, 3, 4);
pg1.park_car("HH-AB 1234");
/*pg1.park_car("HH-CD 5678");
pg1.park_car("HH-EF 1010");
pg1.park_car("HH-GH 1235");
pg1.park_car("HH-IJ 5676");
pg1.park_car("HH-LM 1017");
pg1.park_car("HH-MN 1111"); */
pg1.show();
/*pg1.fetch_car("HH-CD 5678");
pg1.show();
pg1.fetch_car("HH-IJ 5676");
pg1.show();
pg1.park_car("HH-SK 1087");
pg1.show();
pg1.park_car("SE-AB 1000");
pg1.show();
pg1.park_car("PI-XY 9999");
pg1.show(); */
return 0;
}
You did not declare the copy constructor for the Parkbox class. So, the line
p2parboxes[0][0][0] = Parkbox(s)
creates something (instance of Parkbox with a char* pointer) on the stack (and deletes it almost immediately). To correct this you might define the
Parkbox& operator = Parkbox(const Parkbox& other)
{
license_plate = new char[strlen(other.get_plate())+1];
strcpy(license_plate, other.get_plate());
return *this;
}
Let's see the workflow for the
p2parboxes[0][0][0] = Parkbox(s)
line.
First, the constructor is called and an instance of Parkbox is created on stack (we will call this tmp_Parkbox).
Inside this constructor the license_plate is allocated and let's say it points to 0xDEADBEEF location.
The copying happens (this is obvious because this is the thing that is written in code) and the p2parboxes[0][0][0] now contains the exact copy of tmp_Parkbox.
The scope for tmp_Parkbox now ends and the destructor for tmp_Parkbox is called, where the tmp_Parkbox.license_plate (0xDEADBEEF ptr) is deallocated.
p2parboxes[0][0][0] still contains a "valid" instance of Parkbox and the p2parboxes[0][0][0].license_plate is still 0xDEADBEEF which leads to the undefined behaviour, if any allocation occurs before you call the
cout << p2parboxes[0][0][0].license_plate;
Bottom line: there is nothing wrong with the line itself, the problem is hidden within the implementation details of the '=' operator.
At this point it is really better for you to use the std::string for strings and not the razor-sharp, tricky and explicit C-style direct memory management mixed with the implicit C++ copy/construction semantics. The code would also be better if you use the std::vector for dynamic arrays.
The problem here is that you do not have deep copy assignment semantics. When you assign a temporary Parkbox to the Parkbox in the parking garage, the compiler generated assignment operator makes a shallow copy of the pointer license_plate, leaving both Parkboxes pointing at the same memory location. Then the temporary Parkbox goes out of scope and deletes license_plate. Since the other Parkbox is pointing at the same spot its license_plate gets deleted, too.
There are a couple solutions. One way to solve the problem is to define an assignment operator and a copy constructor that provide proper semantics, i.e. that perform deep copies of the license plate string. The better option, and the one that makes better use of C++, is to use std::strings instead of manually allocated C-strings. I strongly suggest the second approach, though working through the first might be instructive.
From the OP:
I solved the Problem with :
void Parkbox::change_plate(char *s){
delete [] license_plate;
license_plate = new char[strlen(s)+1];
strcpy(license_plate, s);
}

overloaded assignment operator - char pointers not copying correctly

I have a class User that looks like this:
class User
{
private:
char* p_username;
int nProcesses;
struct time
{
int mins;
int secs;
} totalTime;
int longestPID;
char* p_longestPath;
public:
User();
User(const char[],int,int,int,const char[]);
~User();
User operator=(const User&);
// Other functions
};
And the overloaded assignment operator function is:
User User::operator=(const User &u)
{
if (this != &u)
{
delete [] p_username;
delete [] p_longestPath;
p_username = new char[strlen(u.p_username)+1];
strcpy(p_username,u.p_username);
nProcesses = u.nProcesses;
totalTime.mins = u.totalTime.mins;
totalTime.secs = u.totalTime.secs;
longestPID = u.longestPID;
p_longestPath = new char[strlen(u.p_longestPath)+1];
strcpy(p_longestPath,u.p_longestPath);
}
return *this;
}
A sample main program using the assignment operator:
int main()
{
cout << "\n\nProgram\n\n";
User u("Username",20,30,112233,"Pathname"),u2;
u2 = u;
}
When I try to use the assignment operator in the line u2 = u, everything is assigned properly except the dynamic char arrays.
Test output from the end of the operator= function shows that at the end of the assignment itself everything has works perfectly (the usernames and pathnames are correct), however test output from the main function directly after the assignment shows that all of a sudden the char arrays have changed. Suddenly the username of u2 is empty, and the first half of the pathname is garbage.
If at the end of the assignment operator function the username and pathname are perfect, how can they be wrong back in the calling function?
This really has me stumped...
Edit: Here are the constructors
User::User()
{
p_username = 0;
nProcesses = 0;
totalTime.mins = 0;
totalTime.secs = 0;
longestPID = -1;
p_longestPath = 0;
}
User::User(const char UID[],int minutes,int seconds,int PID,const char path[])
{
p_username = new char[strlen(UID)+1];
strcpy(p_username,UID);
nProcesses = 1;
totalTime.mins = minutes;
totalTime.secs = seconds;
longestPID = PID;
p_longestPath = new char[strlen(path)+1];
strcpy(p_longestPath,path);
}
You are returning by value from the assignment function. It is possible that your copy constructor is flawed.
You might want to check out this tutorial here:
http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html
Here is an example from there:
MyClass& MyClass::operator=(const MyClass &rhs) {
// Only do assignment if RHS is a different object from this.
if (this != &rhs) {
... // Deallocate, allocate new space, copy values...
}
return *this;
}

Creating a new object and storing that object in a new class' vector, attributes disappear

Bit of a long title so I am sorry with that. But I do have a bit of a problem with my code at the moment. It should be pretty general and there is quite a bit going on in the code so I won't post it all but I do have this one problem. Here it is:
Sentence newSentence(currentSentence, this, sentenceCount);
this->sentencesNonP.push_back(newSentence);
Now, newSentence has an attribute called words which is of type std::vector<Word *>, Word is also another class within the project.
When I am debugging and checking the attributes of newSentence it shows that words is populated with a length of 4, however when I check sentencesNonP, which is a std::vector<Sentence>, the words vector length is 0. I am checking the first point at sentencesNonP because it is the first the first value being pushed in so it's not that I'm looking at the wrong location of the sentencesNonP vector.
Any reason why my data is being lost in the conversion process?
EDIT: I have implemented both a = operator overload and a copy operator. However, words is still empty in sentencesNonP.
EDIT2:
Sentence.h (excluding include's)
class Word;
class Document;
class Sentence {
public:
//Take out Document * document
Sentence();
Sentence(std::string value, Document * document = NULL, int senNum = 0);
Sentence(const Sentence& newSent);
//Sentence(std::string value);
~Sentence(void);
Sentence & operator=(const Sentence newSent);
Document * getDocument(void);
void setDocument(Document * document);
//__declspec(property(get = getDocument, put = setDocument)) Document * document;
std::string getSentence(void);
void setSentence(std::string word);
//__declspec(property(get = getSentence, put = setSentence)) std::string str;
void setSentenceNumber(unsigned int i);
Word * operator[] (unsigned int i);
unsigned int wordCount(void);
unsigned int charCount(void);
unsigned int sentenceNumber(void);
std::vector<Word *> getWordsVector(void);
private:
std::string sentence;
std::vector<Word *> words;
std::vector<Word> wordNonP;
Document * myd;
unsigned int senNum;
};
Ignore the commented out declspec
EDIT3: Here is my copy constructor:
Sentence::Sentence(const Sentence& newSent) {
this->sentence = newSent.sentence;
this->myd = newSent.myd;
this->senNum = newSent.senNum;
for (int i = 0; i < newSent.wordNonP.size(); i++) {
this->wordNonP.push_back(newSent.wordNonP[i]);
this->words.push_back(newSent.words[i]);
}
}
for (int i = 0; i < newSent.wordNonP.size(); i++) {
this->wordNonP.push_back(newSent.wordNonP[i]);
this->words.push_back(newSent.words[i]);
}
If wordNonP is empty you won't copy any words at all. Write either:
for (int i = 0; i < newSent.wordNonP.size(); i++)
this->wordNonP.push_back(newSent.wordNonP[i]);
for (int i = 0; i < newSent.words.size(); i++)
this->words.push_back(newSent.words[i]);
Or even simpler:
this->wordNonP = newSent.wordNonP;
this->words = newSent.words;

initializing a vector of custom class in c++

Hey basically Im trying to store a "solution" and create a vector of these. The problem I'm having is with initialization. Heres my class for reference
class Solution
{
private:
// boost::thread m_Thread;
int itt_found;
int dim;
pfn_fitness f;
double value;
std::vector<double> x;
public:
Solution(size_t size, int funcNo) : itt_found(0), x(size, 0.0), value(0.0), dim(30), f(Eval_Functions[funcNo])
{
for (int i = 1; i < (int) size; i++) {
x[i] = ((double)rand()/((double)RAND_MAX))*maxs[funcNo];
}
}
Solution() : itt_found(0), x(31, 0.0), value(0.0), dim(30), f(Eval_Functions[1])
{
for (int i = 1; i < 31; i++) {
x[i] = ((double)rand()/((double)RAND_MAX))*maxs[1];
}
}
Solution operator= (Solution S)
{
x = S.GetX();
itt_found = S.GetIttFound();
dim = S.GetDim();
f = S.GetFunc();
value = S.GetValue();
return *this;
}
void start()
{
value = f (dim, x);
}
/* plus additional getter/setter methods*/
}
Solution S(30, 1) or Solution(2, 5) work and initalizes everything, but I need X of these solution objects. std::vector<Solution> Parents(X) will create X solutions with the default constructor and i want to construct using the (int, int) constructor. Is there any easy(one liner?) way to do this? Or would i have to do something like:
size_t numparents = 10;
vector<Solution> Parents;
Parents.reserve(numparents);
for (int i = 0; i<(int)numparents; i++) {
Solution S(31, 0);
Parents.push_back(S);
}
the example I gave as a comment uses copy constructor to create new objects.
You can do the following:
// override copy constructor
Solution(const Solution &solution) {
... copy from another solution
}
however be careful, as you no longer going to have exact object copy/construct if you introduce random generation in your copy constructor, i.e. Solution y = x; y != x
your best solution is something like you already have in my opinion
I have used the Boost assignment library for tasks like this. You may find it useful....