带默认参数的函数声明 [英] function declaration with default parameter
问题描述
你好,
我正在尝试声明一个带有std :: list<参数的函数。
我希望这个函数有一个空列表作为默认参数。
它也是模板功能。目前我被卡住,因为我的
编译器在我打电话时找不到匹配的功能。
这里是我的代码:
// ##############代码开始
#include< list>
using namespace std;
template< typename T>
int * f(const int *,
const int *,
const int *,
list< pair< int,T = list< pair< int,int());
int main(int argc,char * argv)
{
int * c,t,i;
int * result = f(c,t,i);
}
模板< typename T>
int * f(const int * c,const int * t,const int * i,
list< pair< int,T)
{
return(int * )0;
}
// ##############代码结束
编译器说
testcase.cpp:在函数''int main(int,char *)''中:
testcase.cpp:16:错误:否ca的匹配函数ll到''f(int *&,int&,
int&)''
编译器版本是g ++ 4.0.2。我想我的语法
在这里做错了。如果有人知道如何做到这一点会很好...谢谢
提前。
matthias
Hi there,
I am trying to declare a function that takes a std::list<parameter.
I want this function to have an empty list as a default parameter.
It''s a template function also. Currently i am stuck because my
compiler does not find a matching function when i do a call.
here is my code:
// ############## code begins
#include <list>
using namespace std;
template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T =list<pair<int, int () );
int main (int argc, char* argv)
{
int* c, t, i;
int* result = f(c, t, i);
}
template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T )
{
return (int *) 0;
}
// ############## code ends
compiler says
testcase.cpp: In function ''int main(int, char*)'':
testcase.cpp:16: error: no matching function for call to ''f(int*&, int&,
int&)''
compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
here. Would be nice if someone knows how to do this right... thanks in
advance.
matthias
推荐答案
6月14日下午5:20,Matthias Pfeifer< pfe ... @ web.dewrote:
On Jun 14, 5:20 pm, Matthias Pfeifer <pfe...@web.dewrote:
你好,
我试图声明一个带有std :: list<参数的函数。
我希望这个函数有一个空列表作为默认值参数。
这也是模板功能。目前我被卡住,因为我的
编译器在我打电话时找不到匹配的功能。
这里是我的代码:
// ##############代码开始
#include< list>
using namespace std;
template< typename T>
int * f(const int *,
const int *,
const int *,
list< pair< int,T = list< pair< int,int());
int main(int argc,char * argv)
{
int * c,t,i;
int * result = f(c,t,i);
}
模板< typename T>
int * f(const int * c,const int * t,const int * i,
list< pair< int,T)
{
return(int *)0;}
// ##############代码结束
编译器说
testcase.cpp:在函数''int main(int,char *)''中:
testcase.cpp:16:错误:否垫ching函数用于调用''f(int *&,int&,
int&)''
编译器版本是g ++ 4.0.2。我想我的语法
在这里做错了。如果有人知道如何做到这一点会很好...谢谢
提前。
matthias
Hi there,
I am trying to declare a function that takes a std::list<parameter.
I want this function to have an empty list as a default parameter.
It''s a template function also. Currently i am stuck because my
compiler does not find a matching function when i do a call.
here is my code:
// ############## code begins
#include <list>
using namespace std;
template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T =list<pair<int, int () );
int main (int argc, char* argv)
{
int* c, t, i;
int* result = f(c, t, i);
}
template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T )
{
return (int *) 0;}
// ############## code ends
compiler says
testcase.cpp: In function ''int main(int, char*)'':
testcase.cpp:16: error: no matching function for call to ''f(int*&, int&,
int&)''
compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
here. Would be nice if someone knows how to do this right... thanks in
advance.
matthias
亲爱的马蒂亚斯,
错误的原因是你没有指定类型。
小修改可以解决这个问题问题。
我在调用函数之前也指定了类型。
#include< list>
使用命名空间std;
模板< typename T>
int * f(const int *,
const int *,
const int *,
list< pair< int,T = list< pair< int,int());
>
int main(int argc,char * argv)
{
int * c,* t,* i;
int * result = f< int>(c,t,i);
}
template< typename T>
int * f(const int * c,const int * t,const int * i,
list< pair< int,T)
{
return(int *)0;
}
谢谢和问候,
Amal P.
Dear Matthias,
The reason for the error is you didnt specify the type.
A small modification can correct this problem.
I just specified the type also before calling the function.
#include <list>
using namespace std;
template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T =list<pair<int, int () );
int main (int argc, char* argv)
{
int* c, *t, *i;
int* result = f<int>(c, t, i);
}
template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T )
{
return (int *) 0;
}
Thanks and regards,
Amal P.
6月14日,6:09下午,Amal P< enjoyam ... @ gmail.comwrote:
On Jun 14, 6:09 pm, Amal P <enjoyam...@gmail.comwrote:
6月14日下午5:20,Matthias Pfeifer< pfe ... @ web .dewrote:
On Jun 14, 5:20 pm, Matthias Pfeifer <pfe...@web.dewrote:
你好,
Hi there,
我试图声明一个函数采用std :: list<参数。
我希望这个函数有一个空列表作为默认参数。
它也是一个模板函数。目前我被卡住,因为我的
编译器在我打电话时找不到匹配的功能。
I am trying to declare a function that takes a std::list<parameter.
I want this function to have an empty list as a default parameter.
It''s a template function also. Currently i am stuck because my
compiler does not find a matching function when i do a call.
这里是我的代码:
here is my code:
// ### ###########代码开始
#include< list>
// ############## code begins
#include <list>
using namespace std;
using namespace std;
template< typename T>
int * f(const int *,
const int *,
const int *,
list< pair< int,T = list< pair< int,int());
template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T =list<pair<int, int () );
int main(int argc,char * argv)
{
int * c ,t,i;
int main (int argc, char* argv)
{
int* c, t, i;
int * result = f(c,t,i);
int* result = f(c, t, i);
}
}
template< typename T>
int * f(const int * c,const int * t,const int * i,
list< pair< int,T)
{
return(int *)0;}
template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T )
{
return (int *) 0;}
// ##############代码结束
// ############## code ends
编译器说
compiler says
testcase.cpp:在函数中''int main(int,char *)'':
testcase.cpp:16:错误:没有匹配函数来调用''f(int *&,int&,>
int&)''
testcase.cpp: In function ''int main(int, char*)'':
testcase.cpp:16: error: no matching function for call to ''f(int*&, int&,
int&)''
编译器版本是g ++ 4.0.2。我想我的语法
在这里做错了。如果有人知道如何做到这一点会很好...谢谢
提前。
compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
here. Would be nice if someone knows how to do this right... thanks in
advance.
matthias
matthias
亲爱的马蒂亚斯,
错误的原因是你没有指定类型。
小修改可以解决这个问题。
我刚刚指定了在调用函数之前也输入。
#include< list>
使用命名空间std;
模板< typename T>
int * f(const int *,
const int *,
const int *,
list< pair< int,T = list< pair< int,int());
int main(int argc,char * argv)
{
int * c,* t,* i;
int * result = f< int>(c,t,i );
}
模板< typename T>
int * f(const int * c,const int * t,const int * i,
list< pair< int,T)
{
return(int *)0;
}
感谢和问候,
Amal P.
Dear Matthias,
The reason for the error is you didnt specify the type.
A small modification can correct this problem.
I just specified the type also before calling the function.
#include <list>
using namespace std;
template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T =list<pair<int, int () );
int main (int argc, char* argv)
{
int* c, *t, *i;
int* result = f<int>(c, t, i);
}
template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T )
{
return (int *) 0;
}
Thanks and regards,
Amal P.
亲爱的马蒂亚斯,
我希望你写这段代码只是为了测试默认参数。
现在也不可能运行这段代码因为没有
指针指向有效的记忆。
感谢和问候,
Amal P.
Dear Matthias,
I hope you write this code just to test the default parameter.
It wont be possible to run this code now also because none of the
pointers point to a valid memory.
Thanks and regards,
Amal P.
亲爱的马蒂亚斯,
Dear Matthias,
>
我希望你写这段代码只是为了测试默认参数。
现在也不可能运行这段代码,因为没有一个
指针指向有效的内存。
谢谢和问候,
Amal P.
>
I hope you write this code just to test the default parameter.
It wont be possible to run this code now also because none of the
pointers point to a valid memory.
Thanks and regards,
Amal P.
嗨Amal,
你是对的。该代码仅用于演示。谢谢你
你的准确答案。我的程序现在有效。顺便说一句:你知道为什么
不能给模板参数一个默认参数吗?
模板< typename T = int ...
对我来说会很好看。
Matthias
Hi Amal,
you are right. The code was just for demonstration. Thank you for
your accurate answer. My program works now. Btw: Do you know why
it is not possible to give a default parameter to the template parameter?
template<typename T=int...
would look nice for me.
Matthias
这篇关于带默认参数的函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!