从函数返回一个std :: Vector需要一个默认值

问题描述 投票:0回答:1

我有一个这样的功能

static int locationinfo( const char *pszSrcFilename 
                       , const char  *pszLocX 
                       , const char *pszLocY
                       , const char *Srsofpoints=NULL
                       , std::vector<PixelData>& results=std::vector<PixelData> 
                       /* char **papszOpenOptions = NULL,int nOverview = -1,*/  
                       )
{
--filling results 
return 1;


}

我想从上面的函数返回results。我使用&但编译器需要results的默认值,如何在函数定义中为std::vector<PixelData>定义默认值?

这是我的错误

error: default argument missing for parameter 5 of ‘int locationinfo(const char*, const char*, const char*, const char*, std::vector<PixelData>&)’
 static int locationinfo(const char *pszSrcFilename , const char  *pszLocX ,const char *pszLocY,const char *Srsofpoints=NULL
            ^~~~~~~~~~~~

谢谢

c++ stdvector
1个回答
2
投票

您可以简单地重新排序参数,以消除对const引用和默认参数声明的需要:

static int locationinfo( const char *pszSrcFilename 
                       , const char  *pszLocX 
                       , const char *pszLocY
                       , std::vector<PixelData>& results // <<<<
                       , const char *Srsofpoints=NULL    // <<<<
                       /* char **papszOpenOptions = NULL,int nOverview = -1,*/  
                       )
{ 
   // ...
}

但是,如果您需要一个仅采用前三个参数的函数,则可以另外使用一个简单的过载:

static int locationinfo( const char *pszSrcFilename 
                       , const char  *pszLocX 
                       , const char *pszLocY
                       ) { 
   std::vector<PixelData> dummy;
   return locationinfo(pszSrcFilename,pszLocX,pszLocY,dummy);
}
© www.soinside.com 2019 - 2024. All rights reserved.