表达式必须有类类型错误c++
expression must have class type error c++
我已经定义了这个:
static char randomstring[128];
现在每当我在这样的地方提到它:
char *x = randomstring;
它运行良好,但每当我试图对其内容做一些事情时:
char *x = ranomstring.front();
它根本不起作用,并且说表达式必须具有类类型。这个问题经常发生在我身上。
您应该了解std::string(一个类)和c风格字符串(char*
或char[]
- array)之间的区别。
//this calls std::string constructor to convert c-style string to std::string:
string mystring = "hello world!";
//std::string has front()
char* front = mystring.front();
//this is old-style string
char oldstring[] = "hello again!";
//you can access it as pointer, not as class
char* first = oldstring;
//but you can iterate both
for(char c : mystring) cout << c;
for(char c : oldstring) cout << c;
//...because it uses std::begin which is specialized for arrays
c++中的数组不是类。它们是聚合体。所以他们没有办法。请使用标准容器std::string
或标准容器std::vector
,它们可以动态改变大小,并具有front
方法。
#include <string>
//...
std::string randomstring;
//filling the string
char x = randomstring.front();
改变char *x = ranomstring.front();char * x =((字符串)ranomstring) .front ();
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误