template functions



 DEVELOP > c-Plus-Plus > template functions

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "saneman"
Date: 31 Jan 2008 04:23:48 PM
Object: template functions
I have made a template function i a .c++ file. But I can't call it from
main:
template <typename V>
void test_stack() {
V v;
}
int main(int, char**) {
test_stack()<int>;
}
I get the error:
In function ‘int main(int, char**)’:
error: no matching function for call to ‘test_stack()’
error: expected primary-expression before ‘int’
7: error: expected `;' before ‘int’
How do I call a template function?
.

User: "peter koch"

Title: Re: template functions 31 Jan 2008 04:31:50 PM
On 31 Jan., 23:23, saneman <y...@dd.com> wrote:

I have made a template function i a .c++ file. But I can't call it from
main:

template <typename V>
void test_stack() {
V v;

}

int main(int, char**) {
test_stack()<int>;

}

I get the error:

In function 'int main(int, char**)':
error: no matching function for call to 'test_stack()'
error: expected primary-expression before 'int'
7: error: expected `;' before 'int'

How do I call a template function?

Well.... you really ought to make an offer yourself. There must be
some documentation you can follow?
Anyway I feel generous today: you need to have the template parameter
before the parenthesis.
/Peter
.
User: "Martin York"

Title: Re: template functions 31 Jan 2008 04:35:33 PM
template <typename V>
void test_stack() {
V v;
}
int main(int, char**) {
test_stack<int>();
}
.


User: "Salt_Peter"

Title: Re: template functions 31 Jan 2008 04:38:50 PM
On Jan 31, 5:23 pm, saneman <y...@dd.com> wrote:

I have made a template function i a .c++ file. But I can't call it from
main:

template <typename V>
void test_stack() {
V v;

}

int main(int, char**) {
test_stack()<int>;

test_stack<int>();


}

I get the error:

In function 'int main(int, char**)':
error: no matching function for call to 'test_stack()'
error: expected primary-expression before 'int'
7: error: expected `;' before 'int'

How do I call a template function?

You can't call something that doesn't exist.
The program first needs to know what function version (or
specialization) to generate.
So:
test_stack<int>();
test_stack<double>();
calls 2 completely different functions.
Have you considered reading the FAQ?
read 35.4 and 35.5 here:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER