By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

function declaration with default parameter

P: n/a
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
Jun 14 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Jun 14, 5:20 pm, Matthias Pfeifer <pfe...@web.dewrote:
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
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.

Jun 14 '07 #2

P: n/a
On Jun 14, 6:09 pm, Amal P <enjoyam...@gmail.comwrote:
On Jun 14, 5:20 pm, Matthias Pfeifer <pfe...@web.dewrote:
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

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.
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.

Jun 14 '07 #3

P: n/a
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.
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
Jun 14 '07 #4

P: n/a
On Jun 14, 7:45 pm, Matthias Pfeifer <pfe...@web.dewrote:
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.

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
Dear Matthias,

As you know template is a mechanism which we can use for easiness
to support different datatype, if you always want it as an int, why
you still use a template?
You should not give like that because it does not worth. If always
you are giving an int there is there any use for using a template
there?

I am sorry if i have mistaken your question.

Thanks and regards,
Amal P.

Jun 14 '07 #5

P: n/a
On 14 Jun, 09:20, Matthias Pfeifer <pfe...@web.dewrote:
#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)
either
int main(int argc, char* argv[])
or
int main()
{
int* c, t, i;

int* result = f(c, t, i);
c is a pointer to int but both t and i are just ints.

also

int* c, *t, *i;
int* result = f<int>(c, t, i);

will work but but only for int

int* result = f<char>(c, t, i);

will not, since you can not convert
pair<int,charto pair<int, int>

one way would be to provide template
version

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T = list<pair<int, T () );

and a non template version for int

int* f(const int*,
const int*,
const int*,
list<pair<int, int = list<pair<int, int () );

and then call them as
int* result = f(c, t, i); // calls int version
int* result1 = f<char>(c, t, i); // calls template version

your compiler might allow it, but main should
return a value

regards

DS

Jun 14 '07 #6

P: n/a
On 14 Jun, 12:49, dasjotre <dasjo...@googlemail.comwrote:
your compiler might allow it, but main should
return a value
I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.

DS

Jun 14 '07 #7

P: n/a
On 14 Jun, 12:53, dasjotre <dasjo...@googlemail.comwrote:
On 14 Jun, 12:49, dasjotre <dasjo...@googlemail.comwrote:
your compiler might allow it, but main should
return a value
Your compiler must allow the omission of the return statement it in
the specific case of main.
I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.
There is an explicit exclusion for main(). If you reach the end of the
function and there is no return statement, the compiler assumes return
0;

Gavin Deane

Jun 14 '07 #8

P: n/a
dasjotre <da******@googlemail.comwrote:
On 14 Jun, 12:49, dasjotre <dasjo...@googlemail.comwrote:
>your compiler might allow it, but main should
return a value

I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.
main() is special, in that the Standard explicitly allows the return
statement to be omitted, with the result that it will implicitly return
0 to the environment.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 14 '07 #9

P: n/a
Matthias Pfeifer wrote:
>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.

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
The committee agrees with you and the next C++ revision will allow that
;). For now, you can use an overload to surcome the need to specify the
template argument:

int *f(const int* a, const int* b, const int* c)
{
return f(a,b,c,list<pair<int,int());
}

- Sylvester
Jun 14 '07 #10

P: n/a
On 14 Jun, 14:58, ricec...@gehennom.invalid (Marcus Kwok) wrote:
dasjotre <dasjo...@googlemail.comwrote:
On 14 Jun, 12:49, dasjotre <dasjo...@googlemail.comwrote:
your compiler might allow it, but main should
return a value
I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.

main() is special, in that the Standard explicitly allows the return
statement to be omitted, with the result that it will implicitly return
0 to the environment.
you're absolutely right, I can't think of reason why it should
be so thou. can you?

regards

DS

Jun 15 '07 #11

P: n/a
dasjotre <da******@googlemail.comwrote:
On 14 Jun, 14:58, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>main() is special, in that the Standard explicitly allows the return
statement to be omitted, with the result that it will implicitly return
0 to the environment.

you're absolutely right, I can't think of reason why it should
be so thou. can you?
I can't say for certain (maybe you should ask on comp.std.c++), but my
intuition would be to help make non-conformant code using

void main() { /* ... */ }

legal just by changing it to

int main() { /* ... */ }

But then, if you're changing the return type from "void" to "int", it's
not that much extra work just to add a "return 0;" to the bottom either.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 15 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.