我有一个名为State的类,它的字段是shared_ptr、weak_ptr和int。我还有一个名为Automata的类,它有一个状态的shared_ptr。我使用State来模拟NFA中的状态。Automata是NFA中状态的链接列表。状态与shared_ptr连接,自循环用weak_ptr表示。
class State {
public:
// ptr to next state
std::shared_ptr<State> nextState;
// ptr to self
std::weak_ptr<State> selfLoop;
// char
int regex;
// Constructor
State(const int c) : regex(c){}
// Destructor
~State();
};
#define START 256
#define FINAL 257
class Automata {
private:
std::shared_ptr<State> start;
public:
// Constructor, string should not be empty
Automata(const std::string &str);
// Destructor
~Automata();
// Determine a string matched regex
bool match(const std::string &str);
};Automata的构造函数基本上接受正则表达式并将其转换为NFA。(它工作!如果你感兴趣的话,请看这个:https://swtch.com/~rsc/regexp/regexp1.html)
编译Automata的构造函数时会发生编译器错误。
Automata::Automata(const string &str) {
start = make_shared<State>(new State(START)); // Error is here, START defined above
for (loop traversing str) {
//add more states to start
}
}我有个错误说
// A lot gibbrish above
Automata.cc:7:45: required from here
/usr/include/c++/4.8/ext/new_allocator.h:120:4: error: invalid conversion
from ‘State*’ to ‘int’ [-fpermissive]
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from Automata.h:4:0,
from Automata.cc:2:
State.h:18:2: error: initializing argument 1 of ‘State::State(int)’ [-fpermissive]
State(const int c);
^不知道我做错了什么。我对shared_ptr完全陌生,所以我不知道是make_shared的问题还是State构造函数的错误?你能帮我修一下吗?
发布于 2015-12-10 07:21:56
你不想写:
Automata::Automata(const string &str) {
start = make_shared<State>(START); // make_shared will call new internally
for (loop traversing str) {
//add more states to start
}
}https://stackoverflow.com/questions/34195854
复制相似问题