Hi guys, trying to figure out why i can't insert this LinkList i create (TrainA) can't be placed into the Queue.
I keep getting the error 'trainA' was not declared within this scope.

Any help will be cool thanks!

class Link
{
public:
int iData; // data item (key)
  double dData; // data item
  Link *next; // next link in list

  Link(int id, double dd)
  {
    iData = id;
    dData = dd;
  }

  void displayLink()
  {
       cout << "" << endl;
cout << "{" << iData << ", " << dData << "}" << endl;
  }

};

class LinkList
{
private: Link *first;

public:

  LinkList()
  {
    delete first; // no links on list yet
  }

  void insertFirst(int id, double dd)
  { // make new link
    Link *newLink = new Link(id, dd);
newLink->next = first; // it points to old first link
first = newLink; // now first points to this
  }

  Link *find(int key)
  { // (assumes non-empty list)
    Link *current = first; // start at ‘first’
    while (current->iData != key) // while no match,
      {
    if (current->next == 0) // if end of list,
      return 0; // didn’t find it
    else // not end of list,
      current = current->next; // go to next link
      }
    return current; // found it
  }

  Link *delete_Renamed(int key)
  { // (assumes non-empty list)
    Link *current = first; // search for link
    Link *previous = first;
    while (current->iData != key)
      {
if (current->next == 0)
  return 0; // didn’t find it
else
  {
    previous = current; // go to next link

  }
current = current->next;
  } // found it

if (current == first) // if first link,
  first = first->next; // change first
else // otherwise,
  previous->next = current->next; // bypass it
return current;
  }

  void displayList()
  {
cout << "List (first-->last): ";
Link *current = first; // start at beginning of list
while (current != 0) // until end of list,
  {
current->displayLink(); // print data
current = current->next; // move to next link
  }
cout << "" << endl;
  }

}; //end of LinkList class

class Queue
{

private:
  int maxSize;
  long long *queArray;
  int front;
  int rear;

public:

  Queue(int s)
  {
    maxSize = s + 1; // array is 1 cell larger
queArray = new long long[maxSize]; // than requested
front = 0;
rear = -1;
  }

  void insert(long long j)
  {
    if (rear == maxSize-1)
  rear = -1;
queArray[++rear] = j;
  }

  long long remove()
  {
long long temp = queArray[front++];
if (front == maxSize)
  front = 0;
return temp;
  }

  long long peek()
      {
        return queArray[front];
      }

      bool isEmpty()
      {
    return (rear + 1 == front || (front + maxSize-1 == rear));
      }

      bool isFull()
      {
        return (rear + 2 == front || (front + maxSize-2 == rear));
      }

      int size()
      {
    if (rear >= front) // contiguous sequence
      return rear - front + 1;
    else // broken sequence
      return (maxSize - front) + (rear + 1);
      }
    };

    int main()
    {
    int j = 0;
    int ia = 0;
    int ib = 0;
    int ic = 0;
    int id = 0;



    Queue TrainLineAE = Queue(10);


    Queue TrainLineAW = Queue(10);
    Queue TrainLineBE = Queue(10);
    Queue TrainLineBW = Queue(10);

      for (int j = 0; j < 1;j++){
            LinkList *TrainA = new LinkList();
            ia++;
            cout << "Line A (East), TrainA "<< ia << " has been initialised" <<  endl;
            }

         for (j = 0;j < 1; j++)
        {
          TrainLineAE.insert(TrainA); // example of queue initialisation by inserting j items
}

  return 0;

    }

Recommended Answers

All 3 Replies

Could you shorten to 10-20 lines with the same behaviour?

It's because TrainA is not in the scope of the insert function.
TrainA is declared inside a for loop (line 166), which, after it's done, will free the variable, making it unknown to the whole function. Try to get it out of that for. Not to mention that this is the least of your problems regarding your program.

 Queue TrainLineBW = Queue(10);
 LinkList *TrainA = new LinkList();
      for (int j = 0; j < 1;j++){
          //LinkList *TrainA = new LinkList(); not 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.