¿Por qué no puedo cortar una cuerda?


Por qué no puedo cout string así:

string text ;
text = WordList[i].substr(0,20) ;
cout << "String is  : " << text << endl ;

Cuando hago esto, obtengo el siguiente error:

Error 2 error C2679: binary'

Es asombroso, que incluso esto no está funcionando:

string text ;
text = "hello"  ;
cout << "String is  : " << text << endl ;
Author: Kiril Kirov, 2011-06-12

6 answers

Debe incluir

#include <string>
#include <iostream>
 200
Author: Kiril Kirov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-12 08:42:49

Necesita hacer referencia al espacio de nombres del cout std de alguna manera. Por ejemplo, insértese

using std::cout;
using std::endl;

En la parte superior de la definición de la función, o el archivo.

 9
Author: progo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-12 08:45:24

Hay varios problemas con su código:

  1. WordList no se define en ninguna parte. Debes definirlo antes de usarlo.
  2. No puedes simplemente escribir código fuera de una función como esta. Tienes que ponerlo en una función.
  3. Necesitas #include <string> antes de poder usar la clase string y iostream antes de usar cout o endl.
  4. string, cout y endl vive en el espacio de nombres std, por lo que no puede acceder a ellos sin prefijarlos con std:: a menos que utilice la directiva using para introducirlas primero en el ámbito de aplicación.
 6
Author: sepp2k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-12 08:46:41

Las respuestas anteriores son buenas, pero si no desea agregar la cadena include, puede usar lo siguiente

ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();

return os;
}
 0
Author: Maheswar Reddy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-06 11:16:58

No tiene que hacer referencia a std::cout o std::endl explícitamente.
Ambos están incluidos en el namespace std. using namespace std en lugar de usar el operador de resolución de alcance :: cada vez hace que sea más fácil y más limpio.

#include<iostream>
#include<string>
using namespace std;
 -1
Author: Ash Ghal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-25 10:41:37

Si está utilizando el sistema linux, debe agregar

using namespace std;

Debajo de los encabezados

Si Windows, asegúrese de colocar los encabezados correctamente #include<iostream.h>

#include<string.h>

Refiérase a esto funciona perfectamente.

#include <iostream>
#include <string>

int main ()
{
std::string str="We think in generalities, but we live in details.";
                                       // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

   std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     
// get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}
 -2
Author: pratikpchpr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-26 01:25:32