twerk_it_606
twerk_it_606

Reputation: 63

error: invalid conversion from char to const char*

I am getting the titular error in the following code.

rna_transcription_test.cpp

#include "rna_transcription.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(transcribes_cytidine_to_guanosine)
{
    BOOST_REQUIRE_EQUAL('G', transcription::to_rna('C'));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(transcribes_guanosine_to_cytidine)
{
BOOST_REQUIRE_EQUAL('C', transcription::to_rna('G'));
}

BOOST_AUTO_TEST_CASE(transcribes_adenosine_to_uracil)
{
    BOOST_REQUIRE_EQUAL('U', transcription::to_rna('A'));
}

BOOST_AUTO_TEST_CASE(transcribes_thymidine_to_adenosine)
{
    BOOST_REQUIRE_EQUAL('A', transcription::to_rna('T'));
}

BOOST_AUTO_TEST_CASE(transcribes_all_dna_nucleotides_to_their_rna_complements)
{
    BOOST_REQUIRE_EQUAL("UGCACCAGAAUU", transcription::to_rna("ACGTGGTCTTAA"));
}
#endif

rna_transcription.h

#include <string>

using namespace std;

class transcription
{
    public:
        static string to_rna(string input)
        {
            string returnValue;
            for(char& c:input)
            {
                if(c == 'A')
                {
                    returnValue.push_back('U');
                }
                else if(c == 'T')
                {
                    returnValue.push_back('A');
                }
                else if(c == 'G')
                {
                    returnValue.push_back('C');
                }
                else if(c == 'C')
                {
                    returnValue.push_back('G');
                }
            }
            return returnValue;
      };
};

I know I just posted this, but I have a feeling that the error is in rna_transcription_test.cpp because I got rna_transcription.h checked by stackoverflow and they said it was bug free. So can someone bugcheck both files, and make sure I got everything right?

Upvotes: 0

Views: 2844

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54325

std::string does NOT have a char constructor. If you want to build a string from a single char you'd have to say std::string(1, 'A')

It does not have a char constructor because char is an integer and integer arguments would auto-convert to characters, resulting in very strange bugs.

So change your calls from transcription::to_rna('A') to transcription::to_rna("A")

Also change your test comparisons to strings instead of characters.

Update:

According to legalize, the tests are perfect as they are. So the answer is not to change the tests but to change your code.

You will need to add another to_rna overload function that receives a single char. You would probably want to update your code so that your loop over the string calls the single char function for each character. That would reduce the duplication from writing the if/then series twice.

Upvotes: 2

Related Questions