web-dev-qa-db-ja.com

関数のデフォルトの引数とヘッダー

私はC++の初心者です。ヘッダーの設定に問題があります。これはfunctions.hからのものです

extern void apply_surface(int, int, SDL_Surface *, SDL_Surface *,SDL_Rect *);

そして、これはfunctions.cppの関数定義です

void
apply_surface(int x, int y, SDL_Surface * source, SDL_Surface *
destination,SDL_Rect *clip = NULL)
{
    ...
}

そして、これは私がmain.cppでそれを使用する方法です

#include "functions.h"
int
main (int argc, char * argv[])
{
    apply_surface(bla,bla,bla,bla); // 4 arguments, since last one is optional.
}

しかし、main.cppは最後のパラメーターがオプションであることを知らないため、これはコンパイルされません。この作業を行うにはどうすればよいですか?

44
yasar

宣言を行います(ヘッダーファイルで-functions.h)には定義(functions.cpp)。

//functions.h
extern void apply_surface(int, int, SDL_Surface *, SDL_Surface *,SDL_Rect * clip = NULL);

//functions.cpp
void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface *
destination,SDL_Rect *clip /*= NULL*/)
{
    ...
}
80
Luchian Grigore

デフォルトのパラメーター値は、関数定義(function.cpp)ではなく、関数宣言(functions.h)にある必要があります。

10
Didier Trosset

使用する:

extern void apply_surface(int, int, SDL_Surface *, SDL_Surface *,SDL_Rect * = NULL);

(ここではチェックできないことに注意してください。近くにコンパイラはありません)。

2
Michel Keijzers