问题
我想在类定义中分配值,该类定义位于类声明cpp的单独头文件中。
在编译时,我收到了这些错误消息:
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mRegexes’ is not a static member of ‘class bob’const std::map<uint,std::string> bob::mRegexes = {
^
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mResponses’ is not a static member of ‘class bob’ const std::map<uint,std::string> bob::mResponses = {
这两个都绝对令人生气,因为我不明白为什么编译器忽略typedef
为std::string
我觉得我在这里遗漏了一些东西,但我不是确定为什么bob.h文件看到的参数与bob.cpp不同。
bob.h
#ifndef BOB_H
#define BOB_H
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <map>
typedef unsigned int uint;
// This was first to go when I started having problems.
/*using std::string;*/
using std::map;
// boost::regex > c++11::regex (gcc doesn't follow standards).
using boost::regex;
class bob
{
enum respond_to{
QUESTION,
YELL,
NAME,
DEFAULT,
LENGTH
};
public:
static const respond_to mResponseTypes;
static const map<uint,std::string> mRegexes;
static const map<uint,std::string> mResponses;
static std::string hey(std::string sentence);
static const std::string* evaluate (const std::string& sentence);
static const std::string* getResponse(const std::string& sentence, const respond_to& type) noexcept(true);
};
#endif
bob.cpp
#include "bob.h"
const std::map<uint,std::string> bob::mRegexes = {
{QUESTION, "[a-z]+\\?"},
{YELL,"[A-Z]+"}
};
const std::map<uint,std::string> bob::mResponses = {
{QUESTION,"Sure"},
{YELL,"Whoah, chill out!"},
{DEFAULT,"Whatever."}
};
// ...