Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++读取从堆栈到字符串的字符结果为“未被识别的枚举” 我是C++新手,肯定有什么东西我缺了。我的代码是: std::stack<char> operators; std::stringstream stream; stream.str("5.2 + 3"); while(stream.peek() != -1){ char token = static_cast<char>(stream.get()); //some code checking if the token is valid operators.push(token); auto tmp = operators.top(); //there I can still see the char (for example '+') std::string tmpStr = "" + tmp; //But when put into string, there is "Unrecognized enum" }_C++_Char_Stdstring - Fatal编程技术网

C++读取从堆栈到字符串的字符结果为“未被识别的枚举” 我是C++新手,肯定有什么东西我缺了。我的代码是: std::stack<char> operators; std::stringstream stream; stream.str("5.2 + 3"); while(stream.peek() != -1){ char token = static_cast<char>(stream.get()); //some code checking if the token is valid operators.push(token); auto tmp = operators.top(); //there I can still see the char (for example '+') std::string tmpStr = "" + tmp; //But when put into string, there is "Unrecognized enum" }

C++读取从堆栈到字符串的字符结果为“未被识别的枚举” 我是C++新手,肯定有什么东西我缺了。我的代码是: std::stack<char> operators; std::stringstream stream; stream.str("5.2 + 3"); while(stream.peek() != -1){ char token = static_cast<char>(stream.get()); //some code checking if the token is valid operators.push(token); auto tmp = operators.top(); //there I can still see the char (for example '+') std::string tmpStr = "" + tmp; //But when put into string, there is "Unrecognized enum" },c++,char,stdstring,C++,Char,Stdstring,变量tmpStr填充了无法识别的枚举,而不是tmp的内容 我找不到任何解决办法,但我相信这一定很简单。 谢谢你的帮助 编辑: 因此,如果我使用tmpStr.push_backtmp,它就会工作。但我使用它的方式如下: std::queue<std::string> outQueue; outQueue.push(" " + operators.top()); //some code std::string result = ""; while(!outQueue.empty()){

变量tmpStr填充了无法识别的枚举,而不是tmp的内容

我找不到任何解决办法,但我相信这一定很简单。 谢谢你的帮助

编辑: 因此,如果我使用tmpStr.push_backtmp,它就会工作。但我使用它的方式如下:

std::queue<std::string> outQueue;
outQueue.push(" " + operators.top());
//some code
std::string result = "";
while(!outQueue.empty()){
    result.append(outQueue.front() + " ");
    outQueue.pop();
}
//result then has for example something like "5.2 own enum 3 own enum"
在从操作符堆栈附加的位置上,有自己的枚举,而不是实际保存的枚举。

停止做+某事

这是C++,它不会从字符串文字中神奇地生成字符串对象。

如果上面的代码确实编译过,这意味着something是某种整型的,您将获取指向该位置的堆栈指针const char*,并在其上添加指针偏移量。在下一个NULL之前,您不会读取一些随机数据

如果要将某个内容转换为字符串,则需要将其转换。实现这一点的标准方法是使用输出流操作符

enum OP
{
    OP_ADD,
    OP_SUB,
    OP_DIV,
    OP_MUL
};

std::ostream& operator << (std::ostream& os, OP op)
{
    switch (op)
    {
        case OP_ADD:
            os << "ADD";
            break;
        case OP_SUB:
            os << "SUB";
            break;
        case OP_DIV:
            os << "DIV";
            break;
        case OP_MUL:
            os << "MUL";
            break;
        default:
            throw std::logic_error("Invalid OP");
    }
}

+tmp实际上将偏移量应用于指针,它不会向空字符串添加字符。如果要向字符串添加字符,则需要调用tmpStr.push_backtmp;。行得通,谢谢。但是,如果我将tmpStr添加到另一个字符串中,它会导致运算符+=出现同样的问题,甚至当我使用append时也是如此。还有一些特殊的方法吗?将tmpStr添加到其他字符串或字符串文本应该可以,因为有重载运算符+来处理这些情况。您应该提供一些代码来演示这个新问题;字符串tmpStr&tmp,1;我已经编辑了这篇文章,所以你可以看到新的问题。我尝试使用push_-back方法使用临时字符串变量,然后插入,但也没有帮助。谢谢你的全面回答!做了C之后很难适应。当我看到+什么的时候,所有的警铃都响了。。。我在想JavaScript,但yea C也这么做。
OP op = OP_ADD;
std::stringstream buff;
buff << op;
std::string sop = buff.str();
template <typename T>
std::string to_string(T value)
{
    std::stringstream buff;
    buff << value;
    return buff.str();
}
OP op = OP_ADD;
std::string sop = to_string(op);