Re: '*' cannot appear in a constant-expression problem
On 2008-10-24 05:46:59 -0400, Stefano Sabatini
<stefano.sabatini@caos.org> said:
[~90 lines of mostly irrelevant code snipped]
I'm using g++ 4.3.1, and the syntax error I get is this:
make PizzaFactory2; and PizzaFactory2
g++ -I/home/stefano/opt/reilabs/include -I/home/stefano/include -O0 -g
-ggdb PizzaFactory2.cxx -c -o PizzaFactory2.o
PizzaFactory2.cxx:50: error: `*' cannot appear in a constant-expression
PizzaFactory2.cxx:50: error: a function call cannot appear in a
constant-expression
PizzaFactory2.cxx:50: error: `*' cannot appear in a constant-expression
PizzaFactory2.cxx:50: error: a function call cannot appear in a
constant-expression
PizzaFactory2.cxx:50: error: a function call cannot appear in a
constant-expression
PizzaFactory2.cxx:50: error: template argument 2 is invalid
The exact line of the error is:
static std::map<std::string, (Pizza *)(*)()> creators;
#include <string>
#include <map>
class Pizza;
std::map<std::string, (Pizza*)(*)()> creators;
These four lines produce the same series of error messages. Learn how
to reduce code that produces error messages to a minimal example. In
the course of doing that, the error usually becomes obvious. If it
doesn't become obvious (and this one isn't obvious), post the minimal
example. This code compiles cleanly:
#include <string>
#include <map>
class Pizza;
std::map<std::string, Pizza*(*)()> creators;
In general, though, don't write complicated types on the fly (pointers
to functions are complicated types). Use typedefs:
typedef Pizza*(*creator)();
std::map<std::string, creator> creators;
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)