Error when iterating through template function with char

The for loop I'm using in the header file of a school project doesn't seem to be working. There seems to be an issue with using a char variable but I don't understand how to fix it. I also am having trouble making sense of the error message when I try to compile.

As a side question, is there anywhere I can go to find documentation on error messages?

template <typename DnaSequence>
std::tuple<int, int, int, int> nucleotide_counts(const DnaSequence &dna) {
int a = 0, g = 0, c = 0, t = 0;

for (char nucl : dna) {
if(nucl == 'A'){ // A or 65
++a;
}else if(nucl == 'C'){ // C or 67
++c;
}else if(nucl == 'G'){ // G or 71
++g;
}else if(nucl == 'T'){ // T or 84
++t;
}
}
std::tuple<int, int, int, int> nucleotides = std::make_tuple(a, c, g, t);
return nucleotides;
}


Error message:

rosalind.h: In instantiation of ‘std::tuple<int, int, int, int> cs19::nucleotide_counts(const DnaSequence&) [with DnaSequence = std::vector<std::__cxx11::basic_string<char> >]’:
test.cpp:15:73: required from here
rosalind.h:31:3: error: cannot convert ‘const std::__cxx11::basic_string<char>’ to ‘char’ in initialization
31 | for (char nucl : dna) {
| ^~~
Last edited on
Show the code that called it.

Your error message is clear as it stands - you called nucleotide_counts as
nucleotide_counts( something ), where something is a vector<string>. The basic elements of this are strings, not chars as implied by your for ( char nucl : dna )

I can't see any good reason for this being a template function in the first place - the only sensible entity to hold a sequence of nucleic acids is a std::string.
Last edited on
Topic archived. No new replies allowed.