为什么我不能取消引用并在C++中引用此字符串
Why can't I dereference and cout this string in C++
我具有此功能:
void strPointerTest(const string* const pStr)
{
cout << pStr;
}
如果我这样称呼:
string animals[] = {"cat", "dog"};
strPointerTest(animals);
它返回第一个元素的地址。所以我期望如果我放弃它,我会得到数组的第一个元素,但这样做:
void strPointerTest(const string* const pStr)
{
cout << *(pStr);
}
它甚至不允许我编译。我尝试使用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)
{
std::cout << *(pInt);
}
我不知道为什么要裁员。我要寻求帮助,因为它不会编译我的目的。如果它在您的末端起作用,并不意味着它也适用于我的。我要帮助,因为我不知道发生了什么。
汇编错误是:
No operator "<<" matches these operands - operand types are: std::ostream << const std::string
在某些编译器中,<iostream>
恰好还包括<string>
标头。在其他编译器(特别是Microsoft编译器)上,显然没有。<string>
标题中声明了用于字符串的I/O操作员。
即使代码有时碰巧起作用,包括所有所需的标头是您的责任。
因此,修复程序就是添加
#include <string>
在文件的顶部。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 如何引用基类的派生类?