C++ 返回一个std::Vector<;对象>;from函数需要一个默认值
我有一个这样的函数C++ 返回一个std::Vector<;对象>;from函数需要一个默认值,c++,stdvector,C++,Stdvector,我有一个这样的函数 static int locationinfo( const char *pszSrcFilename , const char *pszLocX , const char *pszLocY , const char *Srsofpoints=NULL , std::vector<P
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;
}
谢谢您只需对参数重新排序,就不需要
常量
引用和默认参数声明:
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&results/您只需对参数重新排序,就不需要常量
引用和默认参数声明:
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&results//non-const
引用无法绑定到临时变量。您是否尝试通过const
引用传递它?除了您没有构造默认的std::vector
,它将是std::vector()
@AlgirdasPreidžius之外,如果我使用const,我可以修改并向vector添加对象吗?@MajidHojati“那么,如果我使用常量,我可以修改对象并将其添加到向量中吗?”?"不,你不能。将结果
参数移动到Srsofpoints
之前,然后摆脱对默认值的需要怎么样?@MajidHojati不,你不能。向这样的临时对象添加元素有什么意义?@user0042哇,谢谢,这是我的错误-常量
引用无法绑定到临时对象。你尝试过吗通过const
reference使用它?除了您不构建默认的std::vector
,它将是std::vector()
@AlgirdasPreidžius,所以如果我使用const,我可以修改并向vector添加对象吗?@MajidHojati“如果我使用const,我可以修改并向vector添加对象吗?”不,您不能。将结果
参数移动到Srsofpoints
之前,然后不再需要默认值,怎么样?@MajidHojati不,您不能。向这样的临时值添加元素有什么意义?@user0042哇,谢谢,这是我的错误
static int locationinfo( const char *pszSrcFilename
, const char *pszLocX
, const char *pszLocY
) {
std::vector<PixelData> dummy;
return locationinfo(pszSrcFilename,pszLocX,pszLocY,dummy);
}