【发布时间】:2020-12-21 06:24:45
【问题描述】:
error: incompatible types in assignment of 'std::__cxx11::string' {aka'std::__cxx11::basic_string<char>'} to 'char [100]'
error: cannot convert 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'const char*'
这些代码行出现上述两个错误
string s1=asctime(localtime(&timetoday));;
string r=s1.substr(4,6);
ch=r; //char ch[100];(defined aleady)
//1st error was in line just above this comment
if(strcmp(r,"Dec 20")==0)
//2nd error was in line just above this comment
{
cout<<"made my first try for creating this program";
}
else if(strcmp(r,"Dec 21")==0)
{
cout<<"Shortest day of the year";
}
我正在尝试使用代码块在 C++ 中创建一个简单的余数程序。
【问题讨论】:
-
可能来自
ch = r;这一行,看起来你正在混合C和C++。r是string和ch是char -
strings 有自己的内置比较运算符。在可能的情况下,全力以赴string并使用strings 设施。 -
请不要添加无关的语言标签。这个问题是关于 C++ 和 C++ 代码的,所以不要标记 C。
-
至于您的问题,请联系some good books 并参加一些课程正确地学习C++。
-
简单示例:ideone.com/WSSLGF
标签: c++ compiler-errors codeblocks