C ++错误C2280:尝试引用已删除的函数

时间:2017-04-10 22:02:13

标签: c++

我正在开发一个C ++项目,其中一个特性是在中缀表示法中采用代数表达式,将其转换为修复后符号并解决它。转换到post-fix工作正常,我相信我用于评估修复后表达式的代码将按预期工作,但由于错误C2280,我无法编译。这是修复后评估的代码(我对C ++很新,所以任何有经验的人在看到这可能有多差时都可能有动脉瘤):

string EvaluatePostfix(string expression) {
    double num;
    double leftOp;
    double rightOp;
    string leftOpString;
    string rightOpString;
    stringstream numberToPush;
    double tempResult;
    string tempResultString;
    stringstream tempResultSS;
    stack<stringstream> S;
    string finalResultString;


    for (std::size_t i = 0, l = expression.size(); i < l; ++i) { 
        if (expression[i] == '=')
            break;
        else if (isalnum(expression[i])) { //If the first character is alphanumeric, get ready to push to stack. 
            while (expression[i] != ' ') { //Wait until hitting space to ensure entire number is pushed, not just 1 digit
                numberToPush << expression[i];
                i++;
            }
            S.push(numberToPush);

        }

        else if (IsOperator(expression[i])) { //When an operator is hit, top two strings are taken from stack and converted to double.
            switch (expression[i]) {          //Operation is applied, they are converted back to string and result is pushed onto stack
            case '+':
                leftOpString == S.top().str();
                S.pop();
                rightOpString == S.top().str();
                S.pop();
                leftOp = atof(leftOpString.c_str());
                rightOp = atof(rightOpString.c_str());
                tempResult = leftOp + rightOp;
                tempResultString = to_string(tempResult);
                for (int i = 0; i < tempResultString.length() - 1; i++) {
                    tempResultSS << tempResultString.at(i);
                }
                S.push(tempResultSS);   
                break;
            case '-':
                leftOpString == S.top().str();
                S.pop();
                rightOpString == S.top().str();
                S.pop();
                leftOp = atof(leftOpString.c_str());
                rightOp = atof(rightOpString.c_str());
                tempResult = leftOp - rightOp;
                tempResultString = to_string(tempResult);
                for (int i = 0; i < tempResultString.length() - 1; i++) {
                    tempResultSS << tempResultString.at(i);
                }
                S.push(tempResultSS);
                break;
            case '*':
                leftOpString == S.top().str();
                S.pop();
                rightOpString == S.top().str();
                S.pop();
                leftOp = atof(leftOpString.c_str());
                rightOp = atof(rightOpString.c_str());
                tempResult = leftOp * rightOp;
                tempResultString = to_string(tempResult);
                for (int i = 0; i < tempResultString.length() - 1; i++) {
                    tempResultSS << tempResultString.at(i);
                }
                S.push(tempResultSS);
                break;
            case '/':
                leftOpString == S.top().str();
                S.pop();
                rightOpString == S.top().str();
                S.pop();
                leftOp = atof(leftOpString.c_str());
                rightOp = atof(rightOpString.c_str());
                tempResult = leftOp / rightOp;
                tempResultString = to_string(tempResult);
                for (int i = 0; i < tempResultString.length() - 1; i++) {
                    tempResultSS << tempResultString.at(i);
                }
                S.push(tempResultSS);
                break;
            case '^':
                leftOpString == S.top().str();
                S.pop();
                rightOpString == S.top().str();
                S.pop();
                leftOp = atof(leftOpString.c_str());
                rightOp = atof(rightOpString.c_str());
                tempResult = pow(leftOp, rightOp);
                tempResultString = to_string(tempResult);
                for (int i = 0; i < tempResultString.length() - 1; i++) {
                    tempResultSS << tempResultString.at(i);
                }
                S.push(tempResultSS);
                break;
            }
        }
    }
    finalResultString == S.top().str();
    return finalResultString;

}

以下是整个错误消息:

Error   C2280   'std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>::basic_stringstream(const std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>> &)': attempting to reference a deleted function  ConsoleApplication2 c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0 737

以下是第733-738行,其中包含错误所在的行:

template<class _Objty,
    class... _Types>
    void construct(_Objty *_Ptr, _Types&&... _Args)
    {   // construct _Objty(_Types...) at _Ptr
    ::new ((void *)_Ptr) _Objty(_STD forward<_Types>(_Args)...);
    }

如果有人能指出我正确的方向来解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

根据Barman的评论和HairyDuck给出的答案,根据字符串流参考

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/

表示删除了复制操作符 副本(3)
stringstream(const stringstream&amp;)= delete;

并假设stack是std :: stack,它指定了push

“在堆栈顶部插入一个新元素,位于其当前顶部元素上方。此新元素的内容初始化为val的副本。” http://www.cplusplus.com/reference/stack/stack/push/

=&GT;不要使用不可复制类型的stack :: push。