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

|
Related Articles |
|
|