C++ 错误与“运算符不匹配:

0 c++

我正在学习如何使用向量,并正在编写一个简单的程序,该程序获取一些信息并将其放在向量上,然后将其迭代回来。我的来源是

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;
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

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'

谁能帮我找到问题的根源吗?

Sho*_*hoe 5

这:

*i->size
Run Code Online (Sandbox Code Playgroud)

应该:

i->size
Run Code Online (Sandbox Code Playgroud)

运算->符(在您的情况下等于(*i).size)将自动遵循i