C++ extern function error: too many arguments to function

瘦欲@ 提交于 2019-12-24 09:27:34

问题


I have a cw.h file with a bunch of extern functions in it that I want to call from my cw.cpp file.

They are expressed like this in the .h. file along with the declarations of the Type struct (just example functions, not the actual names of the functions):

extern Type* new_type(), match(), sharetype();

But their definitions and implementations are in the cw.cpp file.
Each of the functions has 1 or more parameters passed into it.

When I try compiling, I keep getting this error message for each of the functions:

cw.h:11: error: too many arguments to function Type new_type()
cw.cpp:575: error: at this point in file

I have no idea how to fix it. And I've been searching for the past hour (-_-)

EDIT[SOLVED]:

I changed my code in the .h file to match the types of the parameters being passed into the functions when they're being called. No more errors.


回答1:


In C++, a function declared with () is a prototype and means that the function takes no arguments. In C++ it is equivalent to using (void). It doesn't have the same meaning as in C (i.e. that the function takes an unspecified number of arguments).




回答2:


Extending CharlesBailey's answer:

In C++, Type* new_type() is a different function than Type* new_type(int) due to overloading.

Your parameters need to match their definition:

//hpp:
extern Type* new_type(int), match(float), sharetype(char);

//cpp:
Type* new_type(int x) {
  // ...
}

Type* match(float x) {
  // ...
}


来源:https://stackoverflow.com/questions/8349050/c-extern-function-error-too-many-arguments-to-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!