[英]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.