Weird Segmentation Fault right at the start of main

Viewed 164

This code below shows a segmentation fault. However, right when I unquote the cout << endl statement, it gets rid of the seg fault. I also put a print statement without endl, and it hit seg fault right at the start of main(). Can someone please help me figure this out? Thanks!

#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;
}
3 Answers
Related