C++ 中的运行时错误。重定位协议版本 %d

Runtime error in c++. Relocation protocol version %d

请帮我解决以下转换spoj表达式的问题here is the link。它给出了运行时错误。

     #include <iostream>
     #include <stack>
#include <string>
using namespace std;
int main()
{
    int testcases;
    cin >> testcases;
    while(testcases-->0)
    {
        string s;
        cin >> s;
        cout << s;
        stack<string> st;
        for(int i=0;i<s.length();i++)
        {
            if(s.at(i)=='(')
                continue;
            else
                if(s.at(i)==')')
                {
                    string s2=st.top();
                    st.pop();
                    string expression=st.top();
                    st.pop();
                    string s1=st.top();
                    st.pop();
                    string tba=s1+s2+expression+"";
                    st.push(tba);
                    cout << tba << endl ;
                    }
            else
                st.push(s.at(i)+"");
                }

        string ss=st.top();
        cout << ss;
        }

    }

而且出现的错误是无法理解的。 以下是第一行和第二行的输入错误。

1
(a+(b*c))
(a+(b*c))do relocation protocol version %d.
o relocation protocol version %d.
uery failed for %d bytes at address %p
udo relocation protocol version %d.
do relocation protocol version %d.
o relocation protocol version %d.
uery failed for %d bytes at address %pery failed for %d bytes at address %p
udo relocation protocol version %d.
do relocation protocol version %d.
o relocation protocol version %d.
uery failed for %d bytes at address %pery failed for %d bytes at address %p

看来您已成为 Java 主义的牺牲品。

char ch = 'a';
string str = value + "";

不会 string str "a".

这是因为 "" 不是 string。它实际上是一个指向常量字符数组的指针,一个const char *,因为它是一个指针,所以会进行指针运算。

"" 是内存中的某个地方。假设地址 10000。'a' 有一个数值。让我们坚持使用 ASCII 并使用 97。value + ""; 告诉编译器转到地址 10000 + 97,没有人知道它会在地址 10097 找到什么。但它是 const char *,并且 string 有一个构造函数,它将接受 const char * 并尝试将其转换为 string。无论发生在 10097 的什么,都将被用来构成 string。这可能会使程序崩溃。它还可以从字符串文字的土地上抓取垃圾,这看起来就是 OP 正在发生的事情。

解决方案:

构造一个string.

string str(1, ch);

或者在 OP 的情况下,

st.push(string(1, s.at(i)));

Documentation on string constructor 参见构造函数 2.

但要小心。你有一堆其他逻辑错误 best resolved by using the debugging software 几乎肯定是你的开发环境带来的。