user2284606
user2284606

Reputation: 1

C++ error no match for 'operator:

I'm learning how to uses vectors and was writing a simple program that takes some information and puts it on a vector and then iterates it back. my source is

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;
}

and the error I get is:

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'

can anyone help me locate the source of the problem please?

Upvotes: 0

Views: 103

Answers (4)

Angel Koh
Angel Koh

Reputation: 13505

try removing the "*" by changing

 cout << "The name of the pet is " << *i->name << endl;

to

 cout << "The name of the pet is " <<  i->name << endl;

or

 cout << "The name of the pet is " <<  (*i).name << endl;

Upvotes: 0

taocp
taocp

Reputation: 23634

You should use either:

 i -> size

or

 (*i).size

but not the way you used.

Upvotes: 0

Serdalis
Serdalis

Reputation: 10489

you are getting that error because the compiler is trying to do:

*(i->name)

Which is trying to dereference i->name, and since i is a pointer to the object with name, it will fail.

Wheras what you want is:

(*i).name

or

i->name

Which will dereference i before trying to take name out of the structure.

Upvotes: 0

Shoe
Shoe

Reputation: 76240

This:

*i->size

should be:

i->size

The -> operator (which in your case is equal to (*i).size) will automatically deference i.

Upvotes: 5

Related Questions