小编aas*_*man的帖子

奇怪的分段错误在主要的开始

下面的代码显示了分段错误.但是,当我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)

c++ struct class

3
推荐指数
1
解决办法
134
查看次数

逐字存储字符串并打印

为什么这个程序不会反向打印出"你好"?当我取消注释循环内的行时,它可以工作.我想知道为什么没有存储字符串值的概念.谢谢!

#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)

c++ string storage reverse

0
推荐指数
1
解决办法
40
查看次数

标签 统计

c++ ×2

class ×1

reverse ×1

storage ×1

string ×1

struct ×1