C++ error enum and CRTP
template<class T>
struct broker
{
typedef T typeBroker;
static std::vector<std::string> extractListBroker(const std::string& broker)
{
std::vector<std::string> vec;
if(broker.empty())
{
for(int i=0;i<typeBroker::nbBroker;++i)
vec.push_back( typeBroker::listBroker[i] );
}
else
{
typedef boost::tokenizer<boost::char_separator<char> > my_tok;
boost::char_separator<char> sep( ";" );
my_tok tok( broker, sep );
for ( my_tok::const_iterator i = tok.begin(); i != tok.end(); ++i )
vec.push_back开发者_高级运维( *i );
}
return vec;
}
std::string brokerToStr(typename typeBroker::BROKER i) //<--Problem here !!
{
return typeBroker::listBroker[i];
}
};
struct brokerDisTradable : broker<brokerDisTradable>{
std::vector<std::string> listBroker;
brokerDisTradable()
{
listBroker.push_back("BRIDGE1" );
listBroker.push_back("BRIDGELONDON" );
listBroker.push_back("RECY" );
listBroker.push_back("CURRENEX" );
}
static const int nbBroker = 2;
enum BROKER { BRIDGE1, BRIDGELONDON, RECY, CURRENEX };
};
errro : error C2039: 'BROKER' : is not a member of broker_def::brokerDisTradable'
any idea?
Thanks!
You cannot use the derived type's internal types in the function declaration of the base, because the derived type's internal types aren't yet defined, the derived type is only declared.
There are a number of ways to get around this, including type traits parameter and additional template parameter, and they are perfectly discussed in Base's using a type defined in Derived, in a Curiously Recurring Template Pattern of comp.lang.c++.moderated: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/a99148265cb43680/b2581e058ffe8c91?#b2581e058ffe8c91
Since you're using the enum value as an index into an array, you could change the parameter type to brokerToStr to an int:
struct broker
{
typedef T typeBroker;
std::string brokerToStr(int i)
{
return typeBroker::listBroker[i];
}
Also, typeBroker::listBroker[i]
won't work as listBroker
is not a static member.
I don't understand why you are inheriting brokerDisTradable
from border<brokerDisTradable>
. Probably what you need to do is this:
struct brokerDisTradable {
std::vector<std::string> listBroker;
brokerDisTradable()
{
// ...
}
static const int nbBroker = 2;
enum BROKER { BRIDGE1, BRIDGELONDON, RECY, CURRENEX };
};
int main()
{
brokerDisTradable t;
broker<brokerDisTradable> b;
// ...
return 0;
}
A simplified example of the problem:
template <class T>
struct X
{
void foo(typename T::Enum);
};
struct Y: X<Y> //<-- X instantiate here, but at this point the compiler only knows
//that a struct called Y exists, not what it contains
{
enum Enum {Z};
};
As a possible workaround, perhaps move the enumeration out of Y and add a template parameter to X.
template <class T, class EnumType>
struct X
{
void foo(EnumType);
};
enum Y_Enum {Z};
struct Y: X<Y, Y_Enum>
{
};
brokerDisTradable : broker<brokerDisTradable>
seems to be an incomplete type (infinite heritage inside)
struct brokerDisTradable
and using broker<brokerDisTradable>
will work..
精彩评论