0

why there is an error

#include <iostream> using namespace std; int main(int argc, const char * argv[]) { // insert code here... string values[]={"hello", "world", "int",0}; for(int i = 0;string c = values[i];i++,cout<<c){} return 0; } No viable conversion from 'std::__1::string' (aka 'basic_string<char>') to 'bool'. // why there is a error and what does the "No viable..." mean

31st May 2021, 10:31 AM
Zhengrong Yan
Zhengrong Yan - avatar
2 ответов
+ 4
The compiler is complaining about how confusing and basically impossible it is to interpret "string c = values[i]" as true or false. That compiler problem is also related to a weird thing before it explained below. The 0 in this line is very weird: string values[]={"hello", "world", "int",0}; A null-terminated char array or c-style string is common where a '\0' char marks the end but if you want a null-terminated array of strings, you should write that differently. An array of string* could work but use nullptr or NULL. Don't treat 0 like it is a type of pointer. If you want to represent length of an array in c++, you probably want a vector instead. Another technique is what c conventionally does and the main function's command-line arguments are passed this way. In c, you'd pass the length as an int along with the array. argc and argv paramaters to main work like that.
31st May 2021, 11:10 AM
Josh Greig
Josh Greig - avatar
+ 2
The error simply says that your condition of *for* loop ( i.e. "string c = values[i]" ) can't be converted to Boolean type ( means can't be answered in YES or NO ) so your compiler didn't knew what to do there and threw an error complaining you the same.
31st May 2021, 10:44 AM
Arsenic
Arsenic - avatar