c++ - 对象 std::string 出错

标签 c++ class object constructor stdstring

这里我有一个错误,但我不知道为什么会显示。这是错误:

In file included from Exploit.cc:2:0: Command.hh:35:17: error: field
 ‘_value’ has incomplete type Command.hh: In constructor
 ‘Command::Command(const char*)’: Command.hh:27:3: error: ‘_value’ was
 not declared in this scope make: *** [Exploit.o] Error 1

这是 Command.hh

class Command {
public: 
    Command(const char* exp){
        _value=exp;
        _value.append("\n");
    }
    ~Command();
    void request(int fd);
    void response(std::string res);
    const char*  getCommand();
private: 
    std::string _value;
};

利用.cc

typedef std::shared_ptr<Command> CommandPtr;
typedef std::list<CommandPtr> CommandQueue; 
typedef std::shared_ptr<CommandQueue> CommandQueuePtr; 

Exploit::Exploit(const char* exp, int fd2, int type2): fd(fd2), type(type2){
    commands_to_execute = make_shared<CommandQueue>();
    commands_executed = make_shared<CommandQueue>();
    CommandPtr pr=std::make_shared<Command>( exp);
    commands_to_execute->push_back(pr);  
}

我希望有人能帮助我,因为这对我来说很奇怪。

谢谢!!

最佳答案

您忘记包含字符串标题:

#include <string>

Command.hh 中。


在相关说明中,让构造函数接受 std::string 可能是个好主意:

Command(const std::string& exp) {

而不是 const char*

关于c++ - 对象 std::string 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21525451/

相关文章:

c++ - C++ 标准是否保证统一初始化是异常安全的?

c++ - 基于文本的冒险游戏

Javascript - 对象属性设置的重复

c++ - 结构和类在 C++ 中真的是等价的吗?

java - 在将类的变量存储在java中之前对其进行加密

c++ - 静态对象的非静态成员分配在哪里?

Java 将对象的实例类型传递给泛型类类型参数

c++ - 从 C++ 设置系统变量

c++ - 超过 1GB 的内存分配错误

c++ - 以 root 身份运行时出现段错误?