fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. std::string a = "a";
  6. char b = 'b';
  7. std::string ab = a + b;
  8. std::cout << ab << std::endl;
  9.  
  10. char d = 'd';
  11. char e = 'e';
  12. // this works though, using the std::string(const char* c, const int len) constructor
  13. std::string de2 = std::string(&d, 1) + e;
  14. std::cout << de2 << std::endl;
  15.  
  16. // for some reason, which I should look into
  17. // std::string won't let us directly assign this value like
  18. // probably because it tries to use the constructor, which doesn't exist.
  19. //std::string de = static_cast<char>(d + e);
  20. std::string de;
  21. de = char(d + e);
  22. //std::cout << de << std::endl; // uncomment this line and everything breaks, really bad!
  23.  
  24. return 0;
  25. }
Compilation error #stdin compilation error #stdout 0s 3412KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:18:43: error: conversion from 'char' to non-scalar type 'std::string {aka std::basic_string<char>}' requested
   std::string de = static_cast<char>(d + e);
                                           ^
stdout
Standard output is empty