简体   繁体   English

尝试在main中调用函数时出错。

[英]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++. 我试图在我试图从C转换为C ++的程序中的main中调用一个函数。 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])' 我在其他函数中进行的所有函数调用均编译无误,但是当它到达主函数中的一个函数调用时,我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. 我是C ++的新手,所以我不确定是否需要添加一些特殊的东西才能在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** 当您将其传递给衰减到contacts**的函数时,您已经声明了一个指向 contacts指针数组contacts**

To use your function with its current signature, you either need to declare your friends array as 要将函数与当前签名一起使用,您需要将friends数组声明为

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. 在后一种情况下,将数组传递给函数将起作用,因为它将衰减到函数期望的contacts* The latter case is preferable as you will not have to worry about releasing the memory you create in the first case using new 后一种情况比较可取,因为您不必担心在第一种情况下使用new释放创建的内存

Don't use pointers 不要使用指针

int main()
{
    contacts friends[5];

and your code will compile. 然后您的代码就会编译。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在main中访问类的模板朋友功能。 - Accessing template friend function of class in main. 析因显示主答案错误。 功能正确计算 - Factorial showing wrong answer in main. Computed correctly in function 打印出向量中的输入,并将 function 包含在 main 中。 c++ - Printing out the input in a vector, and include the function into main. c++ C++:将向量传递给 function 然后在 main 中调用 function。 遗漏了什么 - C++: Passing vector to function and then calling function in main. Missing something 我终于在链表中添加了add函数,但不能在main中使用。 :( - I finally made my add function in linked list but cant use in main. :( 尝试调用将向矢量添加数据的函数时出错 - Error when trying to call a function that will add data to vector 尝试访问 function 调用中的向量元素时出现“错误:太多 arguments 无法运行” - "error: too many arguments to function" when trying to access vector elements in a function call 我正在尝试将我的 header 文件用于双向链表,但是当我在主程序中调用任何函数时,它会给出相同的错误代码 - I am trying to use my header files for a doubly linked list but when I call any functions in the main it gives same error code 尝试在数据库管理系统中调用 function 时出现未定义的引用错误以及退出状态错误 - Undefined reference error along with exit status error when trying to call function in database management system 尝试 urldownloadtofile 函数时出错 - Error When trying the urldownloadtofile function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM