[Error] D:\实验\888\main.cpp:137: error: base operand of `->' is not a pointer
时间: 2024-03-06 14:10:31 浏览: 16
这个错误通常是因为你在使用 `->` 运算符时,左侧的对象不是一个指针类型。在 C++ 中,`->` 运算符用于访问指针所指向的对象的成员。
检查一下你的代码中,左侧的对象是否为指针类型。如果不是,你可以使用 `.` 运算符来访问对象的成员。如果你想访问指针所指向的对象的成员,需要使用 `*` 运算符先解引用指针,然后再使用 `.` 或 `->` 运算符访问成员。例如,如果 `p` 是一个指向 `MyClass` 类型的指针,你可以使用 `(*p).member` 或者 `p->member` 来访问 `MyClass` 类的 `member` 成员。
相关问题
D:\data structure\homework(c)\main.cpp|183|error: base operand of '->' has non-pointer type 'NODE<int>'|
This error message suggests that you are trying to use the "->" operator on a variable that is not a pointer. The "->" operator is used to access a member of a pointed-to object.
For example, if you have a pointer to a struct called "person":
```c++
struct person {
string name;
int age;
};
person* ptr_person = new person;
```
You can access the "name" member using "->":
```c++
ptr_person->name = "John";
```
However, if you try to use "->" on a non-pointer variable, you will get the error you described:
```c++
NODE<int> myNode;
myNode->data = 5; // Error: myNode is not a pointer
```
To fix this error, you should use the "." operator instead of "->" when accessing members of non-pointer objects:
```c++
NODE<int> myNode;
myNode.data = 5; // This is correct
```
base operand of '->' has non-pointer type 'std::vector<Wares>
这个错误通常是因为你尝试在一个非指针对象上使用箭头运算符 "->"。箭头运算符只能用于指针对象,而std::vector<Wares>不是指针类型,它是一个容器类模板,因此不能使用箭头运算符。
如果你想访问std::vector<Wares>中的元素,可以使用下标运算符 "[]" 或者迭代器。例如,如果你想访问第一个元素,可以使用以下语句:
```
std::vector<Wares> myVector;
Wares firstElement = myVector[0]; // 使用下标运算符访问第一个元素
```
或者使用迭代器:
```
std::vector<Wares> myVector;
std::vector<Wares>::iterator it = myVector.begin(); // 获取迭代器指向第一个元素
Wares firstElement = *it; // 使用解引用运算符访问第一个元素
```
希望能帮到你!