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;
}

Recommended Answers

All 3 Replies

//Listinterface.h
// the List Operations
//
// 
//
#include <cstdio>		// FILE*

// must have Key, Element, Comparison typedef'ed -- usually by having #include "Element.h" before including this file
// must have List typedef'ed                     -- usually by having #include "List.h"    before including this file

// CONSTRUCTOR and DESTRUCTOR
// create a new empty list
List createList();
// destroy an existing list
void destroyList(List&);

// STATUS
// check whether a list is empty
bool emptyList(const List);
// return the element count of a list
int lengthList(const List);

// INSERTION -- return indicates success or failure
// insert the element into the list as the head
bool insertHead(List&, const Element);
// insert the element into the list as the tail
bool insertTail(List&, const Element);

// EXTRACTION -- return indicates success or failure
// return the element at the head of the list
bool head(const List, Element&);
// return the element at the tail of the list
bool tail(const List, Element&);
// return the element matching the key
bool findMatching(const List, const Key, Element&);

// DELETION -- return indicates success or failure
// delete the element at the head of the list
bool deleteHead(List&);
// delete the element at the tail of the list
bool deleteTail(List&);
// delete the element matching the key
bool deleteMatching(List&, const Key);

// DEBUGGING
// print a list to the given file
void fPrintList(FILE*, const List&);

Would submit please help? I am stuck.

Would somebody help please?*
Sorry for the typo.

Simply enough, your code:

typedef Car Element;

is invalid since Element is already a defined type. What you are saying is that Car is Element. Is that what you really mean? Please clarify what you REALLY mean here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.