Creating linked list from virtual class

Hello,

I am trying to create a linked list that includes two types of subobjects. I'm struggling to explain this. Basically, I have a vehicle class and I am trying to create one linkedlist the contains objects from both my cars and trucks classes.

In my vehicleManager class I am trying to create the list like so:
 
  m_vehicles = new LinkedList<Vehicle*>;


In the headerfile it looks like this:
1
2
3
4
5
6
7
8
class vehicleManager
{
   public:
   //public methods
   private:
   LinkedList<Vehicle>* m_vehicles;
};
#endif 


The error I am getting is "cannot cpnvert 'LinkedList<Vehicle*?*' to 'LinkedList<Vehicle>*' in assignment.
1
2
3
4
5
6
7
8
class vehicleManager
{
   public:
   //public methods
   private:
   LinkedList<Vehicle*> m_vehicles;
};
#endif  


By the way don't use new. Consider using a smart pointer.

http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
I have written the code the way you suggested in the header file and am now getting the following errors:

'error: no match for 'operator=' (operand types are 'LinkedList<Vehicle*>' and 'LinkedList<Vehicle*>*')
m_vehicles = new LinkedList<Vehicle*>;

note: candidate: constexpr LinkedList<Vehicle*>& LinkedList<Vehicle*>::operator=(const LinkedList<Vehicle*>&)
class LinkedList

note: no known conversion for argument 1 from 'LinkedList<Vehicle*>* to 'const LinkedList<Vehicle*>&'
That is exactly the same sort of error as in the OP. One has to be consistent with types everywhere.
I've tweaked some singly linked list code I posted last week (sorry can't find the link) to take into a/c inheritance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <limits>

struct Vehicle
{
    std::string m_reg;
    /*Vehicle (const std::string& reg) : m_reg(reg){}
    virtual std::string getReg()const
    {
        return m_reg;//this method not strictly required for struct
    }*/
    Vehicle* m_next = 0;
    virtual ~Vehicle(){std::cout << "Goodbye " << m_reg << '\n';}
};
struct Car : public Vehicle
{
    using Vehicle::Vehicle;
};
struct Truck : public Vehicle
{
    using Vehicle::Vehicle;
};

struct LList
{
    Vehicle* start = nullptr;
    void insert (const std::string& reg);
    void deleteNode(const std::string& reg);
    void display()const;
    ~LList()
	{
	    Vehicle* temp = start;
	    while (temp)
        {
            Vehicle* next = temp -> m_next;
            delete temp;
            temp = next;
        }
        start = nullptr;
    }
};
void LList::insert(const std::string& reg)
{
	std::cout << "Car(1) or Truck(2)? \n";
	int choice{};
    std::cin >> choice;//input validation required
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    Vehicle* temp;
	if(choice == 1)
    {
        temp = new Car(reg);
    }
    else {temp = new Truck(reg);};
    if(!start)
    {
        start = temp;
    }
    else
    {
        Vehicle* last = start;
        while (last -> m_next)
        {
            last = last -> m_next;
        }
        last -> m_next = temp;
    }
}
void LList::display()const
{
	Vehicle* show = start;
	while (show)
	{
		std:: cout << show ->m_reg << '\n';
		show = show-> m_next;
	}
}
void LList::deleteNode(const std::string& reg)
{

    if (start -> m_reg == reg)//first node delete
    {
        Vehicle* temp  = start;
        start = start -> m_next;
        delete temp;
        temp = nullptr;
        return;
    }
    bool found = false;
    if (start -> m_next)//list has more than 1 node
    {
        Vehicle* lead = start -> m_next;
        Vehicle* current = start;
        while (lead -> m_next)
        {
            if(lead -> m_reg == reg)
            {
                current -> m_next = lead -> m_next;
                Vehicle* temp = lead;
                lead = lead -> m_next;
                delete temp;
                temp = nullptr;
                if(lead -> m_next)
                {
                    lead = lead -> m_next;
                    current = current -> m_next;
                }
                found = true;
            }
            else
            {
                lead = lead -> m_next;
                current = current -> m_next;
            }
        }
        if (lead -> m_reg == reg)//now lead is the last node
        {
            current -> m_next = nullptr;
            delete lead;
            lead = nullptr;
            found = true;
        }
    }
    if(found == false){std::cout << "No match found to delete \n";}
}
int main()
{
    LList n;
	std::string reg{};
	do
    {
        std::cout << "Insert vehicle registration \n";
        getline(std::cin, reg);;//suggest input validation
        if(reg != "Quit")
        {
            n.insert(reg);
        }
    } while (reg != "Quit");//make this more robust
    n.display();
    std::string delItem{};
    std::cout << "Enter another element to delete \n";
    getline(std::cin, delItem);
    n.deleteNode(delItem);
    n.display();
}

Topic archived. No new replies allowed.