未使用默认参数
default argument not being used
我调用这个函数
void Board::insertWord(const string& s, const Clue& clue,
bool recordLetters=true) {
...
}
这里
insertWord( newWord, curClue );
其中 newWord
和 curClue
分别是字符串和线索。我不明白为什么第三个参数没有使用默认值。
g++ -c -std=c++11 -Wall -g board.cpp -o board.o
board.cpp: In member function ‘void Board::processUntilValid()’:
board.cpp:78:38: error: no matching function for call to ‘Board::insertWord(std::string&, const Clue&)’
insertWord( newWord, curClue );
^
board.cpp:78:38: note: candidate is:
In file included from board.cpp:1:0:
board.h:42:10: note: void Board::insertWord(const string&, const Clue&, bool)
void insertWord(const string& s, const Clue& c, bool recordLetters);
^
board.h:42:10: note: candidate expects 3 arguments, 2 provided
我无法重现该问题。有什么想法吗?
默认参数属于声明 (.h
) 而不是定义 (.cpp
)。
这个:
void Board::insertWord(const string& s, const Clue& clue,
bool recordLetters=true) { /* ... */ }
...是Board::insertWord
的定义。在 C++ 中,您在定义方法时不放置默认参数,而是在声明它们时放置默认参数,因此您的代码可能应该是:
class Board {
// Declaration:
void insertWord(const string& s, const Clue& clue,
bool recordLetters = true);
};
// Definition:
void Board::insertWord(const string& s, const Clue& clue,
bool recordLetters) { /* ... */ } // No default argument
默认参数是函数特定声明的属性,而不是函数本身。每个调用者根据它所看到的函数的声明评估默认参数。
完全有可能(如果不明智的话)进行这样的设置:
a.cpp
void foo(int i = 42);
void a()
{
foo();
}
b.cpp
void foo(int i = -42);
void b()
{
foo();
}
main.cpp
#include <iostream>
void a();
void b();
void foo(int i)
{
std::cout << i << '\n';
}
int main()
{
a();
b();
}
这个程序将愉快地在一行上输出 42
并在下一行输出 -42
,同时完全定义良好并符合要求。
因此,在您的情况下,您必须确保调用方有权访问定义默认参数的声明。该声明恰好是您案例中的函数定义。如果它隐藏在 .cpp 文件中,您可能希望将默认参数定义移动到头文件中的函数声明中。
我调用这个函数
void Board::insertWord(const string& s, const Clue& clue,
bool recordLetters=true) {
...
}
这里
insertWord( newWord, curClue );
其中 newWord
和 curClue
分别是字符串和线索。我不明白为什么第三个参数没有使用默认值。
g++ -c -std=c++11 -Wall -g board.cpp -o board.o
board.cpp: In member function ‘void Board::processUntilValid()’:
board.cpp:78:38: error: no matching function for call to ‘Board::insertWord(std::string&, const Clue&)’
insertWord( newWord, curClue );
^
board.cpp:78:38: note: candidate is:
In file included from board.cpp:1:0:
board.h:42:10: note: void Board::insertWord(const string&, const Clue&, bool)
void insertWord(const string& s, const Clue& c, bool recordLetters);
^
board.h:42:10: note: candidate expects 3 arguments, 2 provided
我无法重现该问题。有什么想法吗?
默认参数属于声明 (.h
) 而不是定义 (.cpp
)。
这个:
void Board::insertWord(const string& s, const Clue& clue,
bool recordLetters=true) { /* ... */ }
...是Board::insertWord
的定义。在 C++ 中,您在定义方法时不放置默认参数,而是在声明它们时放置默认参数,因此您的代码可能应该是:
class Board {
// Declaration:
void insertWord(const string& s, const Clue& clue,
bool recordLetters = true);
};
// Definition:
void Board::insertWord(const string& s, const Clue& clue,
bool recordLetters) { /* ... */ } // No default argument
默认参数是函数特定声明的属性,而不是函数本身。每个调用者根据它所看到的函数的声明评估默认参数。
完全有可能(如果不明智的话)进行这样的设置:
a.cpp
void foo(int i = 42);
void a()
{
foo();
}
b.cpp
void foo(int i = -42);
void b()
{
foo();
}
main.cpp
#include <iostream>
void a();
void b();
void foo(int i)
{
std::cout << i << '\n';
}
int main()
{
a();
b();
}
这个程序将愉快地在一行上输出 42
并在下一行输出 -42
,同时完全定义良好并符合要求。
因此,在您的情况下,您必须确保调用方有权访问定义默认参数的声明。该声明恰好是您案例中的函数定义。如果它隐藏在 .cpp 文件中,您可能希望将默认参数定义移动到头文件中的函数声明中。