Reputation:
class CBAY_ITEM
{
public:
string name = enterName();
string condition = enterCondition();
};
when I compile, it gives 4 errors, which say
1.a function call cannot appear in a constant-expression
2.ISO C++ forbids initialization of member 'name'
3.making 'name' static
4.invalid in-class initialization of static data member of non-integral type 'std::string'
what am I doing wrong here??
Upvotes: 1
Views: 367
Reputation: 391
You cannot initialize members at their declaration in C++03, unless they are static const members being initialized with constant expressions. Constant expressions cannot contain function calls in C++03.
Either switch to C++11 (-std=c++11
or -std=c++0x
with gcc or clang) or initialize the members in the CBAY_ITEM's constructor. If you have several constructors that perform a common initialization, you can move the common initialization to a helper init method.
class CBAY_ITEM {
std::string name;
std::string condition;
public:
CBAY_ITEM() : name(enterName()), condition(enterCondition())
{}
};
Upvotes: 5
Reputation: 2240
Do you want to initialize those values in your class? Use a constructor.
#include <string>
std::string enterName();
std::string enterCondition();
class CBAY_ITEM
{
public:
std::string name;
std::string condition;
CBAY_ITEM() {
name = enterName();
condition = enterCondition();
}
};
Upvotes: -1