相关疑难解决方法(0)

为什么我不能唱一个字符串?

为什么我不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)

c++ string cout

130
推荐指数
3
解决办法
21万
查看次数

我在c ++中不包含字符串头文件时出错

#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的行.

c++ string std

0
推荐指数
1
解决办法
3955
查看次数

为什么我不能在C++中取消引用和cout这个字符串

我有这个功能:

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)

c++

-4
推荐指数
1
解决办法
305
查看次数

标签 统计

c++ ×3

string ×2

cout ×1

std ×1