Inserting Object into a List

I am writing a program that requires the user to enter information about a car, that information is stored into an object called Carritos, that object is then sent to a List and from the list to a file. I have no problem with the file part, my problem is that the object is not getting into the list.

This is the object (I'm not going to post the function definitions since I have no problem with this class):
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
class Carrito
{
private:
	char tablilla[8];
	char marca[16];
	char modelo[31];
	char year[6];
	char color[13];
	char powerwindow[3];
	char powerlock[3];
	char alarm[3];
	char ac[3];
	char puertas[3];
	char trans[5];
	char owner[31];
	char tel[14];


public:
	Carrito(void);
	~Carrito(void);

	void setTablilla(char tablilla[]);
	void setMarca(char marca[]);
	void setModelo(char modelo[]);
	void setYear(char year[]);
	void setColor(char color[]);
	void setPW(char pw[]);
	void setPL(char pl[]);
	void setAlarm(char alarm[]);
	void setAc(char ac[]);
	void setPuertas(char puertas[]);
	void setTrans(char trans[]);
	void setOwner(char owner[]);
	void setTel(char tel[]);

};


The List I am using is a template defined by the professor and us, I have used it in other exercises with no problems. As such I am giving only the information on the parts I think are causing the problems. We use two classes, List which is the actual list and ListNode to determine each node of the list.

This is ListNode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<class T> class List;
template<class T>
class ListNode
{
	friend class List<T>;
	public:
		ListNode(T&);
		T getData();
	private:
		T data;
		ListNode<T> *next;
};

template<class T>
ListNode<T>::ListNode(T &info): data(info), next(NULL)
{
}

template<class T>
T ListNode<T>::getData() 
{
	return data;
}


This is the part of List that has the Insert function, which is where I believe my problem is, and it is the only one I use in my main:
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
#include "ListNode.h"
#include<iostream>
using namespace std;
template <class T>

class List{
private:
	ListNode<T> *head;
	int size;
	int counter;
	ListNode<T> *find(int index) const;
public:
	List();
	List(const List<T> &aList);
	~List();
	int getLength() const; //not included in the code I am providing
	void insert(int index, T tempData);
	void remove(int index); //not included in the code I am providing
	void retrieve(int index, T &tempData);//not included in the code I am providing
	bool isEmpty() const; //not included in the code I am providing
	void print();
};//end List class

template <class T>
List<T>::List():head(NULL), size(0), counter(0)
{
}//endListDefinition


template<class T>
void List<T>:: insert(int index, T tempData)
{
	int newLength=getLength()+1;
	if((index<1)||(index>newLength))
		cout<<"Insert index out of range\n";
	else
	{ 
		ListNode<T>*newPtr=new ListNode<T> (tempData);
		if(newPtr==NULL)
			cout<<"Insert cannot allocate memory\n";
		else
		{
			size=newLength;
			if(index==1)
			{
				newPtr->next=head;
				head=newPtr;
			}
			else
			{
				ListNode<T>*previousPtr=find(index-1);
				newPtr->next=previousPtr->next;
				previousPtr->next=newPtr;
			}
		}
	}
}






Also the print function in list does not want to work (I only call it to see if the list has saved the information) because I get the following error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Carrito' (or there is no acceptable conversion)


This is the print function code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void List<T>::print()
{
	
	if(isEmpty())
	{
		cout<<"List is empty/n";
		return;
	}
	else
	{
		ListNode<T>*currentPtr=head;
		while(currentPtr!=NULL)
		{
			cout<<currentPtr->data<<endl;
			currentPtr=currentPtr->next;
		}
	}
	cout<<"\n\n";
}


I am using VisualStudio 2k5 in Windows XP. Any help would truly be appreciated.
As far as your print function is concerned - you have to provide an overloaded << operator for the Carrito class (using a friend operator overload) in which you have to format the ouput of the character arrays in your Carrito class.

Carrito will need a specialized copy constructor and assignment operator in order to put objects of that type into a container properly.

Why use char[] when C++ has this perfectly good string type?

I prefer strings myself, but the professor wants those to be char[].

I'll work on that copy constructor, thanks.
hmmmm this is a little off topic but this is the second time I've heard of professors wanting students to use char * instead of string. I can understand wanting to dip backward and pick up a little C in the process of learning C++, but I think they should do it in places where char * is acceptable (like in some private housekeeping function of the class), not in places when strings are clearly called for.

stepping off my soapbox now. I'm done.
Why use char[] when C++ has this perfectly good string type?


"perfectly good" is a stretch.
"better than nothing" is more like it.

anyway yeah I'm in overall agreement with the generial sentiment here. This also seems to be a clear example of "making member vars private just for the sake of doing it".

Sometimes I'm glad I'm self-taught. It seems that a lot of what schools teach you is stuff you have to unlearn later. (Though granted I've unlearned a lot of stuff on my own)
Topic archived. No new replies allowed.