为什么我不cout string喜欢这个:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到以下错误:
错误2错误C2679:二进制'<<':找不到带有'std :: string'类型的右手操作数的运算符(或者没有可接受的转换)c:\ users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
令人惊讶的是,即使这不起作用:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
Run Code Online (Sandbox Code Playgroud) #include<iostream>
#include<string>
using namespace std;
void main(){
string str="abc";
cout<<str;
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
如果我不包含字符串头文件,那么<< in line cout <中 会出现错误
我认为错误将在定义str的行.
我有这个功能:
void strPointerTest(const string* const pStr)
{
cout << pStr;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样称呼它:
string animals[] = {"cat", "dog"};
strPointerTest(animals);
Run Code Online (Sandbox Code Playgroud)
它返回第一个元素的地址.所以我期待如果我取消引用它,我会得到数组的第一个元素但是这样做:
void strPointerTest(const string* const pStr)
{
cout << *(pStr);
}
Run Code Online (Sandbox Code Playgroud)
它甚至不会让我编译.我尝试使用int而不是字符串,它的工作原理.字符串有什么特别之处吗?如何在此函数中检索字符串数组的元素?
编辑:
这是一个完整的例子,它不会在我的结尾编译:
#include <iostream>
void strPointerTest(const std::string* const pStr);
void intPointerTest(const int* const pInt);
int main()
{
std::string animals[] = { "cat", "dog" };
strPointerTest(animals);
int numbers[] = { 9, 4 };
intPointerTest(numbers);
}
void strPointerTest(const std::string* const pStr)
{
std::cout << *(pStr);
}
void intPointerTest(const int* const pInt)
{ …Run Code Online (Sandbox Code Playgroud)