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; } 
  • Your code is compiled (of course, after removing unnecessary _s ). But the text of the error about which you write in the question does not match the code. - KoVadim
  • This code is taken as an example. The whole snag is that when initialized to String. In any story, he writes: Error: there is no suitable constructor to transform from "const char []" to "String" - Egor Malinovsky
  • one
    Your code works fine (if you replace strcpy_s with strcpy) - ideone.com/xZLFPZ How can we help you if you are unable to reproduce the error? Yes, and in the question you declare some errors, in the code - others, and as a result there are no? ... - Harry
  • one
    Error or compiler warning? All the same, it is better to use const char s[] as a constructor parameter, rather than char s[] . In general, the error text hints that you somewhere accidentally wrote a string instead of a String . - wololo
  • Everything earned with const char s []. Thanks for the help! - Egor Malinovsky

0