C++错误与"运算符"不匹配:
C++ error no match for 'operator:
我正在学习如何使用向量,并正在编写一个简单的程序,该程序获取一些信息并将其放在向量上,然后将其迭代回来。我的来源是
int main ()
{
int answer= 1;
int decide;
int vectCount = 0;
vector<animal> pet;
while(answer > 0)
{
pet.push_back(animal());
cout << "enter the name of the pet" << endl;
getline(cin,pet[vectCount].name);
cout << "Please enter the age of the pet" << endl;
cin >> pet[vectCount].age;
cout << "enter the weight of the pet" << endl;
cin >> pet[vectCount].weight;
do
{
cout << "Please enter the size of the pet S/M/L" << endl;
cin >> pet[vectCount].size;
}while(pet[vectCount].size != 'L'
&& pet[vectCount].size != 'M'
&& pet[vectCount].size != 'S');
answer = question(decide);
}
vector<animal>::iterator i;
for(i = pet.begin(); i != pet.end(); ++i)
{
cout << "The name of the pet is " << *i->name << endl;
cout << "The age of the pet is " << *i->age << endl;
cout << "The weight if the pet is " << *i->weight << endl;
cout << "The size of your pet is " << *i->size;
if(*i->size == 'S')
cout << "(-): meow" <<endl;
if(*i->size == 'M')
cout << "(---): woof" <<endl;
if(*i->size == 'L')
cout << "(------): moooo" <<endl;
}
cout << "Exiting the program" << endl;
cin.get();
return 0;
}
得到的错误是:
谁能帮我找出问题的根源吗?
no match for 'operator*' in '*(&i)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = animal*, _Container = std::vector<animal, std::allocator<animal> >]()->animal::name'
This:
*i->size
应:i->size
->
运算符(在您的示例中等于(*i).size
)将自动忽略i
你会得到这个错误,因为编译器正在尝试做:
*(i->name)
试图解引用i->name
,由于i
是指向name
对象的指针,它将失败。
而你想要的是:
(*i).name
或
i->name
这将取消对i
的引用,然后再尝试从结构中取出name
您应该使用:
i -> size
或
(*i).size
但不是你用的方式
尝试通过更改
来删除"*" cout << "The name of the pet is " << *i->name << endl;
cout << "The name of the pet is " << i->name << endl;
或
cout << "The name of the pet is " << (*i).name << endl;
相关文章:
- 在使用累加时,C++中的运算符+不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- 不显示匹配运算符
- 映射迭代器与运算符不匹配
- 与"运算符<<"C++不匹配
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与标准中的"运算符<<"不匹配
- 运算符不匹配*=
- 不断收到错误" 与"运算符>>不匹配";
- 编译我的 3 个文件时,我收到错误,说"运算符="不匹配
- C++ 模板与运算符<不匹配
- 与"运算符="不匹配
- 代码块编译错误与运算符<<不匹配
- 错误:运算符 [] 不匹配。在比较列表中的 int 和 int 时<int>,
- 解决错误:运算符 [] 不匹配
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与“运算符 []”不匹配
- C++ 设置:不匹配 - 运算符