I'm dealing with a Linked List. It's made up of classes. When I try to import information from a text file into the first pointer of the list with this function: (much more detailed description of my file hierarchy and code at the bottom)
void productList::readInProducts(){
ifstream file;
string line = "";
cout << "What's the name of the file you would like to open?" << endl;
getline(cin, input);//string input is in productList and shared between this function and writeOutProducts()
file.open(input);
if (!file.is_open())
{
cout << "Cannot open " << input << endl;
return;
}
head->products.setName(line);
getline(file, line);
head->products.setPrice(stof(line));
getline(file, line);
head->products.setAmount(stoi(line));
getline(file, line);
while (!file.eof())
{
Node* n = new Node;
n->products.setName(line);
getline(file, line);
n->products.setPrice(stof(line));
getline(file, line);
n->products.setAmount(stoi(line));
getline(file, line);
current = current->next;//make the current pointer point to the next node in the list
current->next = n; //Set the current (empty) node equal to the n which contains three lines from the file
}
}
I receive this error:
and visual studio opens up (what I assume is a CPP) file called xstring which has an break arrow pointing to line 2245 on it.
Now let me explain my code in a little more detail. I have five files: productList.h, productList.cpp, product.h, product.cpp, & main.cpp. Here's productList.h:
#include "product.h"
namespace std{
class productList{
public:
productList();
void addAProduct();
void changeInventory();
void printProducts();
void readInProducts();
void writeOutProducts();
private:
short count;
string input; //Used to share the input between the read & write functions
product products[200];
struct Node{
product products;
Node* next;
};
Node* head;
Node* current;
Node* temp;
Node* tail;
};
}
And here's product.h:
#include <string>
namespace std{
class product{
public:
product();
string getName();
void setName(string s);
float getPrice();
void setPrice(float p);
int getAmount();
void setAmount(int a);
private:
string name;
float price;
int amount;
};
}
My productList.cpp file includes:
#include <iostream>
#include <fstream>
#include <iomanip>
#include "productList.h"
My product.cpp file just includes product.h and my main.cpp file (which contains my executing function) includes:
#include <iostream>
#include <array>
#include "productList.h"
Although these relations may seem a little convoluted, they should all be valid. The cpp files are linked to their header files with the exact same name (providing the header file exists), along with main.cpp being linked to productList.h and productList.h being linked to product.h. I can't see what I'm doing wrong with my code—although that's probably just because I'm having such a hard time understanding exactly how pointers work. I imagine that the error is in the code of my productList::readInProducts(){
function but I can't tell what's wrong.
Any help you guys can give would be greatly appreciated, as I am stumped. Here's a dropbox link to all the files: https://www.dropbox.com/s/9welj7k0yy2i1zj/productList.zip?dl=0