Zeratas
Zeratas

Reputation: 1015

Trying to add to a LinkedList in C++. Getting a SegFault

So I am trying to add to a Linked List in c++ and I keep getting a SegFault for some reason.

Stock is the class that will 'be' the LinkedList and StockAccount is the class that will be accessing it.

The way I am inserting data into is is that it goes through a file line buy line and parses the information on the current line and inserts it into the list.

This is my code that I am using to add to the list:

void StockAccount::addStock(string sN, double sP) {


    Stock *temp, *temp2;

    temp->StockName = sN;
    temp->StockPrice = sP;
    temp->next = NULL;

    if (myHead == NULL) {
        myHead = temp;
    } else {
        temp2 = myHead;
        while (temp2->next != NULL) {
            temp2 = temp2->next;
        }
        temp2->next = temp;
    }

}

The SegFault seems to be happening at the line where I define

temp->StockName=sN;

I am new to c++ so my guess is that I am using pointers/references wrong.

Here is how I define the Stock class:

#include <string>

using namespace std;
using std::string;

class Stock {
    friend class StockAccount;
public:

    Stock() {
    }

    Stock(string name, double price) : StockName(name), StockPrice(price) {
        this->next = NULL;
    }

private:

    string StockName = "";
    double StockPrice = 0;
    Stock *next;


};

and here is how I define the StockAccount class. Account is just a simple base class.

class StockAccount : public Account {
    friend class Account;

public:



    StockAccount();


    void addStock(string sN, double sP);
private:
    Stock *myHead;
    Stock *myTail;

};

And the implementation of it:

StockAccount::StockAccount() {
    vector<string> temp;
    string line;
    std::ifstream stockfile("Results.txt");
    if (stockfile.is_open()) {
        while (stockfile.good()) {


            getline(stockfile, line);
            istringstream ss(line);
            string token;
            while (std::getline(ss, token, ',')) {
                temp.push_back(token);
            }

            //*stck = new stock(token.at(0), atof(temp.at(1)));

            addStock(temp.at(0), atof(std::string(temp.at(1)).c_str()));
            temp.clear();

        }
        stockfile.close();
    } else {
        cout << "Unable to open file" << std::endl << std::endl;
    }

}
//http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C

void StockAccount::addStock(string sN, double sP) {


    Stock *temp, *temp2;

    temp->StockName = sN;
    temp->StockPrice = sP;
    temp->next = NULL;

    if (myHead == NULL) {
        myHead = temp;
    } else {
        temp2 = myHead;
        while (temp2->next != NULL) {
            temp2 = temp2->next;
        }
        temp2->next = temp;
    }

}

Upvotes: 0

Views: 69

Answers (1)

Lawrence H
Lawrence H

Reputation: 467

The variable temp is uninitialized. By doing temp->StockName = sN;, you are essentially trying to dereference an invalid pointer, which of course gives you a segmentation fault.

What you need to do is allocate a stock object by doing Stock *temp = new Stock(); before you attempt to do anything with it.

Upvotes: 1

Related Questions