Chong Chong - 26 days ago 13
C++ Question

error when trying class with linked list c++

I am trying out simple class and linked list implementation.
Please help me out with this code as i am receiving
"list iterator not dereferencable"
when running the code.
Thanks

#include<iostream>
#include<list>
#include<string>

using namespace std;

class Car
{
public:
void getType(string x)
{
type = x;
}

string showType()
{
return type;
}

private:
string type;
};

void main()
{
string p;
list<Car> c;
list<Car>::iterator curr = c.begin();

cout << "Please key in anything you want: ";
getline(cin, p);
curr->getType(p);

cout << "The phrase you have typed is: " << curr->showType() << endl;

}

Answer

Write the following way

cout << "Please key in anything you want: ";
getline(cin, p);

c.push_back( Car() );

list<Car>::iterator curr = c.begin();

curr->getType(p);

And it is much better to rename the member function getType to setType.:)

Take into account that the function main without parameters shall be declared like

int main()
^^^
Comments