1

I'm getting errors when I call a function which uses a default argument.

The two files are cache.cpp and cache.h

Command I use to compile is

g++ -c cache.cpp

and the error is:

cache.cpp: In member function ‘bool mem::read(long unsigned int)’:
cache.cpp:205:88: error: no matching function for call to ‘vcache::swap(long unsigned int&, bool&)’
cache.h:97:23: note: candidate is: long unsigned int vcache::swap(long unsigned int, bool, int)
cache.cpp: In member function ‘void mem::write(long unsigned int)’:
cache.cpp:367:92: error: no matching function for call to ‘vcache::swap(long unsigned int&, bool&)’
cache.h:97:23: note: candidate is: long unsigned int vcache::swap(long unsigned int, bool, int)

As you can see on line #569 where the function vcache::swap has been defined, I've provided a default value to the 3rd argument. The problem arises when I don't specify the 3rd argument during the function call. If I run this by explicitly specifying a 3rd argument, it compiles properly.

Am unable to understand why this is happening.

1
  • 1
    That is way too much code to understand at once. Try to narrow down to the problem code snippet and post that here. You should start looking at the line numbers present in the compiler errors. In this case, take another look at lines 205 and 367 in cache.cpp and line 97 in cache.h. Commented Sep 26, 2011 at 0:49

1 Answer 1

11

That's not how default arguments work. The default argument has to go in the declaration, not the definition:

// foo.h
void foo(int, int, int = 5);   // default values here

// foo.cpp
void foo(int a, int b, int c)
{
  // ...
}

Think about it: Every TU that wants to use the function has to know the default value. This only makes sense in the declaration, which every user of the function must see.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.