Element.h:15: error: using typedef-name ‘Element’ after ‘struct’
TrainToList.cc:6: error: ‘Element’ has a previous declaration here
I understand that Element is getting defined twice but I do not know how to fix this. I must use Element as Car (or vice-versa??:confused:). Please suggest a fix to this problem.
Now here are the codes:
// train.h -- data structures for trains
//
// CMPT 115 WT2 2010-2011
struct Car {
char *kind;
int length;
};
struct Train {
int nCars;
Car *cars;
};
// fill in a shared car structure
void setCar(Car&, const char*, const int);
// allocate a new train
Train* makeTrain(const int);
// read a train
bool readTrain(Train&, const char*);
// print a car
int printCar(const Car, const int);
// print a train
int printTrain(const Train);
// destroy a train
void destroyTrain(Train*);
//
// This file is called Element.h
//
#ifndef _ELEMENT_H_
#define _ELEMENT_H_
#include <cstdio>
// a simple Element: a number as the key
// and a string as the data
typedef int Key; // change 'int' to your key type
typedef char* Data; // change 'char*' to your data type
struct Element {
Key key;
Data data;
};
// comparing keys will return values just like strcmp() does
typedef int Comparison;
const int EQUAL_ELEMENT = 0;
// create an element with given key and data
Element createElement(const Key&, const Data&);
// destroy an element
void destroyElement(Element&);
// get the key from an element
Key getKey(const Element&);
// compare two elements' keys
Comparison compareKeys(const Key&, const Key&);
// print an element to the given place
void fPrintElement(FILE*, const Element&);
#endif /*_ELEMENT_H_*/
// List.h
// Simple Lists: the list contains a pointer to the head node and a count
//
//
//
#ifndef _SIMPLE_LIST_
#define _SIMPLE_LIST_
#include "Element.h"
// a node stores data and points to the next node
struct Node {
Element data;
Node* next;
};
// a list contains a pointer to the head node and a count
struct List {
Node* head;
int count;
};
#endif /*_SIMPLE_LIST_*/
//This file is called TrainToList.cc
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include "train.h" // from Assignment 3
typedef Car Element;
#include "List.h" // from Tutorial 5
#include "ListInterface.h"; // from Tutorial 5
// accept a train and return it as a List of Cars
// using only the operations in the List ADT
List train2List(Train t) {
List trainAsList = createList();
for (int i=0; i<t.nCars; i+=1) {
assert(insertTail(trainAsList, t.cars[i]));
}
return trainAsList;
}
// accept a List of Cars and convert it to a train
// using only the operations in the List ADT
Train list2Train(List l) {
Train t = *(makeTrain(lengthList(l)));
for (int i=0; !emptyList(l); i+=1) {
assert(head(l, t.cars[i]));
assert(deleteHead(l));
}
return t;
}
// simple activity
int main() {
while (!feof(stdin)) {
int nCars;
char tFile[BUFSIZ];
scanf(" %d %s", &nCars, tFile);
if (0 == nCars) { // stop when the count is 0
break;
}
Train t = *(makeTrain(nCars));
if (!readTrain(t, tFile)) {
fprintf(stderr, "Read failed\n");
continue;
}
char ignored[BUFSIZ];
fgets(ignored, BUFSIZ, stdin);
printTrain(t);
List l = train2List(t);
Train t2 = list2Train(l);
printf("\n This should be the same:\n");
printTrain(t2);
}
return EXIT_SUCCESS;
}