-1

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

halfelf
  • 9,737
  • 13
  • 54
  • 63
  • 1
    First please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) (and probably [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) as well). – Some programmer dude Dec 06 '18 at 09:29
  • @AlgirdasPreidžius [Not necessarily.](https://stackoverflow.com/questions/11683959/can-a-copy-constructor-take-a-non-const-parameter) – asu Dec 06 '18 at 09:29
  • Also, please try to create a [**Minimal**, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) to show us, and copy-paste the *full* and *complete* build output into the question (as text). – Some programmer dude Dec 06 '18 at 09:31
  • 2
    @AlgirdasPreidžius - `RNA::RNA(RNA &rhs)` is a copy constructor. However, it cannot be used to copy a `const` object. – Peter Dec 06 '18 at 09:38
  • 1
    @Peter Or temporary objects. – Some programmer dude Dec 06 '18 at 09:38
  • @Someprogrammerdude - yep – Peter Dec 06 '18 at 09:39
  • i am still study c++ so i cannot know every thing – Saeed Muhammed Dec 06 '18 at 09:39
  • 1
    To add another hint to the comment by @CinCout, the error message should actually contain exactly what is happening, and what is being ***called***. – Some programmer dude Dec 06 '18 at 09:39
  • 1
    @Asu Today I learned.. What I learned in addition to that, is that copy-constructor might have multiple arguments (if other arguments have default values). – Algirdas Preidžius Dec 06 '18 at 09:41

3 Answers3

1

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)
{}
Caleth
  • 52,200
  • 2
  • 44
  • 75
0

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.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

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; 
Walter
  • 44,150
  • 20
  • 113
  • 196