简体   繁体   中英

Error when trying to call a function in main.

I'm trying to call a function within my main in a program in which I am trying to convert from C to C++. All my function calls within other functions compile without error, but when it gets to the one function call in the main I get no matching function for call to contacts::menu(contacts*[5], int*, int&, char[50])'

Here is the main:

int main() {


 contacts *friends[5];
 char buffer[BUFFSIZE];
 int counter=0;
 int i=0;

 contacts::menu(friends, &counter,i,buffer);

 getch();
 return 0;
}

here is the class with the function declaration:

class contacts
{
  private:
          char *First_Name;
          char *Last_Name;
          char *home;
          char *cell;
  public:
  //constructor
         contacts()
         {
         }       

//Function declarations 
static void menu(contacts*friends ,int* counter,int i,char buffer[]);
};

Here is the beginning part of the menu function, just so you can get an idea what It was labelled:

void contacts::menu(contacts*friends,int* counter, int i,char buffer[]) 
{
  int user_entry=0;
  int user_entry1=0;
  int user_entry2=0;
  char user_entry3[50]={'\0'};
  FILE *read;
  printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
  scanf("%d",&user_entry1);
  if(user_entry1==1)
    {
     printf("Please enter a file name");
     scanf("%s",user_entry3); 
     read=fopen(user_entry3,"r+");

Like I said, the other functions within my program don't receive any errors, but this one does. I'm new to C++ so I wasn't sure if there was something special I needed to add to call a function within the main.

Here is the problem

 contacts *friends[5];

which you pass to

void contacts::menu(contacts*friends,int* counter, int i,char buffer[]) 

You have declared an array of pointers to contacts when you pass this to the function it decays to a contacts**

To use your function with its current signature, you either need to declare your friends array as

contacts* friends = new contacts[5];

or

contacts friends[5];

In the latter case passing the array to the function will work since that will decay to contacts* which your function is expecting. The latter case is preferable as you will not have to worry about releasing the memory you create in the first case using new

Don't use pointers

int main()
{
    contacts friends[5];

and your code will compile.

菜单功能预计contacts*作为第一个参数,而你正在试图通过contacts*[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM