¿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 ;
6 answers
Debe incluir
#include <string>
#include <iostream>
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.
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:
-
WordList
no se define en ninguna parte. Debes definirlo antes de usarlo. - No puedes simplemente escribir código fuera de una función como esta. Tienes que ponerlo en una función.
- Necesitas
#include <string>
antes de poder usar la clase string y iostream antes de usarcout
oendl
. -
string
,cout
yendl
vive en el espacio de nombresstd
, por lo que no puede acceder a ellos sin prefijarlos constd::
a menos que utilice la directivausing
para introducirlas primero en el ámbito de aplicación.
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;
}
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;
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;
}
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