template functions

S

saneman

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?
 
P

peter koch

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
 
M

Martin York

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

}

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

}
 
S

Salt_Peter

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 said:
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top