表达式必须具有C++类错误

表达式必须具有C++类错误,c++,C++,所以我定义了: static char randomstring[128]; 现在每当我在这样的地方提到它: char *x = randomstring; 它很好,但每当我尝试对其内容做一些事情时: char *x = ranomstring.front(); 它根本不起作用,说表达式必须有类类型。。 这个问题经常发生在我身上。 我还应该提到我是C++的一个完全NoB。C++中的数组< < /P> 不是类。它们是聚合体。所以他们没有办法。 改为使用标准容器std::string或

所以我定义了:

static  char    randomstring[128];
现在每当我在这样的地方提到它:

char *x = randomstring;
它很好,但每当我尝试对其内容做一些事情时:

char *x = ranomstring.front();
它根本不起作用,说表达式必须有类类型。。 这个问题经常发生在我身上。
我还应该提到我是C++的一个完全NoB。C++中的数组< < /P>

不是类。它们是聚合体。所以他们没有办法。 改为使用标准容器std::string或标准容器std::vector,它们可以动态更改大小并具有方法前端

比如说

#include <string>

//...

std::string randomstring;

//filling the string

char x = randomstring.front();

您可能应该了解类与char*或char[]-数组之间的区别


更改char*x=ranomstring.front; 至char*x=stringranomstring.front

char数组不是类的实例。它没有可调用的方法。读C++书如果你是初学者,不要浪费我们自己的时间。
//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