Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a static const set<string> in implementation file?

My initialisation of a static const set<string> appears to be incorrect, I'd appreciate your guidelines on this:

obj.h:

class obj
{
  ...

private:
  static const set<string> keywords;
  ...

}

obj.cpp:

const string kw[] = {"GTR","LTR","LEQ","GEQ","NEQ","SQRT","sqrt"};
const set<string> obj::keywords = (kw,kw + sizeof(kw)/sizeof(kw[0]));

But this yields the error: error: conversion from ‘const string* {aka const std::basic_string<char>*}’ to non-scalar type ‘const std::set<std::basic_string<char> >’ requested

Can somebody tell me the correct way to initialise this set?

like image 363
Prem Avatar asked May 03 '13 04:05

Prem


1 Answers

I'm wondering why you're using an array to initialize the std::set.

You could directly initialize the set as:

const set<string> obj::keywords {"GTR","LTR","LEQ","GEQ","NEQ","SQRT","sqrt"};

That is what you should be doing if you're using a compiler supporting C++11.


As for what is wrong with your code, as the other two answers says, remove =:

const string kw[] = {"GTR","LTR","LEQ","GEQ","NEQ","SQRT","sqrt"};
const set<string> obj::keywords(kw,kw + sizeof(kw)/sizeof(kw[0]));

Hope that helps.

like image 157
Nawaz Avatar answered Oct 11 '22 00:10

Nawaz