user3601898
user3601898

Reputation: 1

C++ Templates: Error while instantiate object

i try to instantiate a class template with an int Variable. template class:

template <int N>
class GRAPH {
    // ...
}

when i try to do this like:

    GRAPH<100> mygraph;

it works fine. But when I do this like:

int maxVertices=100;
GRAPH<maxVertices> mygraph;

I get following error:

invalid type in declaration before ';' token

Can someone help me?

Thx

Upvotes: 0

Views: 45

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Change your code to

const int maxVertices=100;
GRAPH<maxVertices> mygraph;

Template parameters are evaluated at compile time, thus you can only pass a constant expression as template parameter here.

Upvotes: 2

Related Questions