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

2019-08-14 21:49发布

问题:

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) {
  // ...
}