Question

I'm relatively new to C++ and am learning by going through questions. I'm attempting to use getopt to manage my input and I've created functions to help make the program clearer. Here's what I'm doing (snippets):

void processArgs(int,char *,char **,long int *);

void processArgs(int argc, char ** argv, long int * res)
{
  globalArgs.startTerm = NULL;
  globalArgs.endTerm = NULL;
  globalArgs.outFileName = NULL;
  globalArgs.verbosity = 0;
  int minimumArgs = 1;

  //Check for minimum number of parameters
  if (argc<minimumArgs){
    displayUsage();
  }
}

int main(int argc,char * argv[]){
  int * results=NULL;
  processArgs(argc,argv,results);
  return 0;
}

When I run the following compile, I get:

g++ -std=c++0x fibonacci.cpp -o fibonacci
fibonacci.cpp: In function âint main(int, char**)â:
fibonacci.cpp:68: error: no matching function for call to âprocessArgs(int&, char**&, int*&)â
fibonacci.h:3: note: candidates are: void processArgs(int, char*, char**, long int*)
fibonacci.cpp:46: note:                 void processArgs(int, char**, long int*)

Can someone please help me to understand the problem? I'm sure its related to my reduced understanding to how the pointers related to argv are working.

Thanks Dan

Was it helpful?

Solution

your function signatures are not matching

void processArgs(int,char *,char **,long int *);

void processArgs(int argc, char ** argv, long int * res)

check for char *

you can remove char * from void processArgs(int,char *,char **,long int *); or you can remove the complete line.

here because you are already defining function above main() so no need to use void processArgs(int,char *,char **,long int *); before function, as compiler is already getting the function signature before compiling main().

OTHER TIPS

Take into account that these two function declarations declare different functions because they have different number of parameters

void processArgs(int,char *,char **,long int *);

void processArgs(int argc, char ** argv, long int * res);

However it is not the reason of the error.

The error message of your compiler is confusing. The problem is that in the second function declaration (that in the same time is a definition) the third parameter is declared as having type

long int *

However you call the function passing to it as an argument an object (variable results) of type

int *

Here is your definition of the passed to function object and the function call

  int * results=NULL;
  processArgs(argc,argv,results);

There is no implicit conversion from int * to long int *

So the compiler issues the error.

Firstly, as rajenpandit already said, your declaration and definition signatures are different. Secondly, I suppose that it fails to call the function due to the last argument: the function expects long int *, while you provide it with a int *.

The first obvious problem (for programmers!) in your code - rajenpandit already stated it - is a signature mismatch - but - as Vlad from Moscow found - the compiler did not stop there. The call itself did not match any of the two declarations you gave. This is what the compiler says.


BTW: The easiest way to declare a function in C/C++: use the clipboard (add the semicolon manually):

void processArgs(int argc, char ** argv, long int * res);

void processArgs(int argc, char ** argv, long int * res)
{
    // your function body
}

The copy&paste method has also another advantage: the function declaration then contains also parameter names, which is especially helpful if there is more than one parameter. Many IDEs also display these names as a hint when you type the call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top