0

我有一个

struct OpDesc {
        std::string  OPName;
        size_t       OPArgsMin;
        bool         IsVaribaleArgsNum;
        bool         IsOPChange;
        std::string  ChangeNodeOP;
        std::string  ChangeNodeLabel;
        bool         IsOPDelete;
        const char*  ErrMsg;
    };

并想初始化一个std::map<string, OpDesc>.

我试过这样做:

typedef std::map<std::string,struct OpDesc> OpDescMap;
OpDescMap opDesc;
opDesc["StoreOp"] = {"StoreOp",2,false,false,"","",false,""};
/// etc.

我无法用 VS10 编译它。我得到:error C2059: syntax error : '{'

如何解决?

4

4 回答 4

1

可以通过为OpDesc

OpDesc(const std::string&  oPName="StoreOp",
      size_t oPArgsMin = 0,
      bool  isVaribaleArgsNum = false,
      bool  isOPChange=false,
      const std::string&  changeNodeOP = "",
      const std::string&  changeNodeLabel = "",
      bool  isOPDelete = false,
      const char*  errMsg= "" )
  :OPName(oPName),
  OPArgsMin(oPArgsMin),
  IsVaribaleArgsNum(isVaribaleArgsNum),
  IsOPChange(isOPChange),
  ChangeNodeOP(changeNodeOP),
  ChangeNodeLabel(changeNodeLabel),
  IsOPDelete(isOPDelete),
  ErrMsg(errMsg)
{
}

OpDescMap opDesc;
opDesc["StoreOp"] = OpDesc("StoreOp", 2, false, false, "", "", false, "");
于 2013-02-02T23:44:33.457 回答
1

An alternative to @billz's solution is to construct the object and insert it into the map in two separate steps:

OpDesc od = { "StoreOp",2,false,false,"","",false,"" };
opDesc["StoreOp"] = od;
于 2013-02-02T23:45:36.673 回答
1

Your syntax is valid C++11 (see Uniform Initialization), however, VS10 does not support it. It was only added to VS12 (see C++ features in VS2012). One option is to upgrade your compiler to one that has better conformance to C++11.

If you can't upgrade, you'll have to fallback to C++03 syntax. You can either use an intermediate variable:

OpDesc op = {"StoreOp", 2, false, false, "", "", false, ""};
opDesc[op.OPName] = op;

Or add a constructor to your structure:

struct OpDesc {
   // ... all fields
   OpDesc(std::string const& opName, size_t opArgsMin, bool isVariableArgsNum,
          bool isOpChange, std::string const& changeNameOp,
          std::string const& changeNodeLabel, bool isOpDelete,
          char const* errMsg)
   : OPName(opName), OPArgsMin(opArgsMin), IsVariableArgsNum(isVariableArgsNum),
     IsOpChange(isOpChange), ChangeNameOp(changeNameOp),
     ChangeNodeLabel(changeNodeLabel), IsOpDelete(isOpDelete),
     ErrMsg(errMsg) {}
};

opDesc["StoreOp"] = OpDesc("StoreOp", 2, false, false, "", "", false, "");
于 2013-02-02T23:45:46.463 回答
0

You can use another compiler: Your source code works with clang++ V 3.3 and gcc 4.7.2.

于 2013-02-02T23:48:00.527 回答