Error: error C2664: "size_t strlen (const char *)": unable to convert argument 1 from "std :: string" to "const char *" There is no user-defined conversion operator available or the operator can not be called to perform this conversion error C2664 : "char * strcat (char *, const char *)": it is not possible to convert argument 1 from "std :: string" to "char *" There is no available user-defined conversion operator, or the operator cannot be called to perform this conversion
#include <iostream> #include <string.h> // для функций strcpy, strcat #include <stdlib.h> // для функции exit using namespace std; class String { private: enum { SZ = 80 }; char str[SZ]; public: String() { strcpy_s(str, ""); } String(char s[]) { strcpy_s(str, s); } void display() const { cout << str; } String operator += (String ss) const { String temp; if (strlen(str) + strlen(ss.str) < SZ) { strcpy_s(temp.str, str); // копируем содержимое первой строки strcat_s(temp.str, ss.str);// добавляем содержимое второй строки } else { cout << "\nПереполнение!"; exit(1); } return temp; // возвращаем результат } }; int main() { system("chcp 1251 > nul"); String s1 = "\nHappy New Year! "; /* Ошибка: не существует подходящего конструктора для переобразования из "const char[19]" в "String" */ String s2 = "Merry Christmas!"; String s3; s1.display(); s2.display(); s3.display(); s3 = s1 += s2; s3.display(); cout << endl; cin.get(); return 0; }
_s). But the text of the error about which you write in the question does not match the code. - KoVadimconst char s[]as a constructor parameter, rather thanchar s[]. In general, the error text hints that you somewhere accidentally wrote astringinstead of aString. - wololo