473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function declaration with default parameter

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
11 2322
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4935
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
3485
by: Schwarzbauer Günter | last post by:
Hello, Does the C++ standard allow default parameters when defining a typedef for a function type, e.g. typedef void (*TYPE_Report)(const char* message, const char* details = 0); This was...
18
2077
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
3
3043
by: hugheslin | last post by:
Hi, Please consider the following classes: ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class Shape {...
6
1408
by: lovecreatesbea... | last post by:
Why doesn't line# 21 create an object of type `C' class? I think the default construction function invocation `C()' creates a temporary nameless object, and `o1' will be built with the copy...
16
7570
by: arne | last post by:
Hi all, imagine I call a function, but omit one of the parameters, like: foo.c: void foo( int a, int b ) { /* do something with a and b */ return; }
8
1726
by: borophyll | last post by:
I don't understand the difference between these two declarations int foo(char a, char b) { ... } int foo(a, b) char a, b; {
8
284
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
40
2287
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
8
2374
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.