下面的代码显示了分段错误.但是,当我cout << endl取消引用该语句时,它就摆脱了seg错误.我也没有打印声明endl,并且它在开始时就遇到了seg错误main().有人可以帮我解决这个问题吗?谢谢!
#include <iostream>
using namespace std;
typedef struct node{
string city;
node * next;
} Node;
class Vertex{
public:
Vertex(string cityName) {
x->city = cityName;
x->next = NULL;
}
void printCity() {
cout << x->city << endl;
}
private:
Node * x;
};
int main() {
//cout << endl;
Vertex x("Phoenix");
x.printCity();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么这个程序不会反向打印出"你好"?当我取消注释循环内的行时,它可以工作.我想知道为什么没有存储字符串值的概念.谢谢!
#include<iostream>
using namespace std;
void reverse(string str) {
int length = str.length();
int x = length, i = 0;
string newString;
while(x >= 0) {
newString[i] = str[x-1];
//cout << newString[i];
x--;
i++;
}
cout << newString;
}
int main() {
reverse("hello");
return 0;
}
Run Code Online (Sandbox Code Playgroud)