You are here:

C++/Error C2064: term does not evaluate to a function taking 4 arguments

Advertisement


Question
In my code I have an array being initialized before entering a for loop and being populated in the for loop after questions have been answered by the user.  From what I have found on the web and in my books it "looks like" I typed everything correctly.  Any help would be appreciated, thanks.

Jim

void main()
{
 string nm;
 int ag;
 double wgt;
 char gen;
  Dog dg[3]; //initialize array here
  bool flag = false;
  for(int i = 0; i > 3; i++)
  {
     cout << "Enter a dog's name" << endl;
     cin >>  nm;
     cout << "Enter the dog's age" << endl;
     cin >> ag;
     while (!flag)
     {
        if(ag<=0)
        {
         cout<<"You must enter a postive number greater than zero!";
         cin >> ag;
        }
        else
        {
         flag = true;
        }
     }
     cout << "Enter the dog's weight" << endl;
     cin >> wgt;
     while (!flag)
     {
        if(wgt<=0.0)
        {
         cout<<"You must enter a postive number greater than zero!";
         cin >> wgt;
        }
        else
        {
         flag = true;
        }
     }
     cout << "Enter 'M' if the dog is Male and 'F' if the dog is female" << endl;
     cin >> gen;
     while (!flag)
     {
        if((gen = 'm')||(gen = 'M')||(gen = 'f')||(gen = 'F'))
        {
         flag = true;
        }
        else
        {
         cout<<"You must 'M' if the dog is Male and 'F' if the dog is female!";
         cin >> gen;
        }
     }
     dg[i](nm, ag, wgt, gen);//C2064 Error here
  }
  
  for(int i = 0; i > 3; i++)
  {
     dg[i].print();
  }
  for(int i = 0; i > 3; i++)
  {
    happyBirthday(dg[i]);
  }
};

Answer
Hello Jim, thank you for the question.

The reason you are getting a compile error, is that it appears you are trying to invoke the constructor of your Dog array after it has been constructed. The compiler is trying to resolve your parameters to a method on the class, but it can't.

When you allocate your array on the stack: Dog dg[3], the default constructor is called for each Dog object at that instant. The way to fix this is to allocate your Dog array on the heap by using a pointer instead.

Dog* dg[3];

// In your for loop after you have collected data
cout<<"You must 'M' if the dog is Male and 'F' if the dog is female!";
cin >> gen;
}
}
//dg[i](nm, ag, wgt, gen);//C2064 Error here
dg[i] = new Dog(nm, ag, wgt, gen);
}

After you are finished operating on your Dog array you have to delete it though.
for(i = 0; i < 3; i++)
{
delete dg;
}

This should help fix your compile problems. Please let me know if you have any other questions.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.