简体   繁体   中英

C++ error: not a member OR multiple definition

I am trying to use a static const field which I define inside a class.
When I define it like that:

class DisjunctionQuery : public Query
{

  public:
    DisjunctionQuery ();
    static const std::string prefix;


};
const std::string DisjunctionQuery::prefix = "Or";

It says: multiple definition of 'DisjunctionQuery::prefix' and if I change it that way (remove the two lines):

class DisjunctionQuery : public Query
{

  public:
    DisjunctionQuery ();
    //static const std::string prefix;


};
//const std::string DisjunctionQuery::prefix = "Or";

It says when I try to call it in another place 'prefix' is not a member of 'DisjunctionQuery'.

How can I make it work? thanks.

You move the definition to a single implementation file.

If you keep it in the header, you'll break the one definition rule . Each file that includes the header will attempt to define the static member, which is wrong.

//DisjunctionQuery.h
class DisjunctionQuery : public Query
{
  public:
    //....
    static const std::string prefix;
};

//DisjunctionQuery.cpp
#include "DisjunctionQuery.h"
const std::string DisjunctionQuery::prefix = "Or";

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