首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shared_ptr编译器错误无效转换

shared_ptr编译器错误无效转换
EN

Stack Overflow用户
提问于 2015-12-10 07:18:29
回答 1查看 842关注 0票数 0

我有一个名为State的类,它的字段是shared_ptrweak_ptrint。我还有一个名为Automata的类,它有一个状态的shared_ptr。我使用State来模拟NFA中的状态。Automata是NFA中状态的链接列表。状态与shared_ptr连接,自循环用weak_ptr表示。

代码语言:javascript
复制
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的构造函数时会发生编译器错误。

代码语言:javascript
复制
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
    }
}

我有个错误说

代码语言:javascript
复制
// 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构造函数的错误?你能帮我修一下吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-10 07:21:56

你不想写:

代码语言:javascript
复制
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
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34195854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档