简体   繁体   中英

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

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.

Template parameters are resolved at compile-time. Since maxVertices isn't a constant, its value isn't known at compile-time, so you get an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM