Ok so today I tried translating the ever confusing pseudo codes that our books give us and this happened. First of all, queues and lists are built in functions in the c++ library right? I have a bunch of compiler errors that wont go away. Queue undeclared, list undeclared, ifstream undeclare and so on.. can someone please tell me what I am doing wrong?

Code is supposed to be a simulation of a fast food place.
Codes:
ItemType.h

struct ItemType
{
  char eventType;
  int eventTime;
  int transTime;
};

sim.cpp

#include <iostream>
#include <fstream>
#include <queue>
#include <list>
#include "ItemType.h"
void simulate()
{
  int wait = 0, count = 0;

  Queue bankQueue;

  LinkedList eventList;

  ifstream theFile("sim.dat");
  ItemType newEvent;

  theFile >> newEvent.eventTime >> newEvent.transTime;
  newEvent.eventType = 'A';

  eventList.insert(newEvent);

  while(!eventList.isEmpty())
  {
    eventList.retrieve(0, newEvent);
    if (newEvent.evenType == 'A')
    { count++;
      processArrival(newEvent, theFile, eventList, bankQueue);
    }
    else
      processDeparture(newEvent, eventList, bankQueue, wait);
  }
}
void processArrival(itemType aEvent, ifStream& aFile, LinkedList& eList, Queue& bQ)
{
  bool atFront = bQ.isEmpty();
  bQ.enqueue(aEvent);
  eList.remove(aEvent);

  if(atFront)
  {
    itemType dEvent.eventType = 'D';
    dEvent.eventTime = aEvent.eventTime + aEvent.transTime;
    dEvent.transTime = 0;
    eList.insert(dEvent);
  }

  itemType ev;
  if (aFile >> ev.eventTime)
  { ev.eventType = 'A';
    aFile >> ev.transTime;
    eList.insert(ev);
  }
}

Main

#include "sim.cpp"
using namespace std;
int main()
{
    simulate();
    cin.get();
    return 0;
}

When you do this :

#include "sim.cpp"
using namespace std;

you are not including namespace std, inside sim.cpp, because of the ordering.

Just do this :

#include <iostream>
#include <fstream>
#include <queue>
#include <list>
#include "ItemType.h"
using namespace std;

yo

Edited 6 Years Ago by firstPerson: n/a

Errors are still there even though I already put in the namespace std. I am still lurking around the internet looking for answers but still no luck. I feel like I am missing something fundamental here. Since, ifstream is also not working even though it should work properly with fstream in there. I am caught up in my Java class so I've been away from c++ for a while.

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\shoti\Desktop\Main.cpp" -o "C:\Users\shoti\Desktop\Main.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
In file included from C:\Users\shoti\Desktop\Main.cpp:1:
C:\Users\shoti\Desktop\/sim.cpp: In function `void simulate()':
C:\Users\shoti\Desktop\/sim.cpp:11: error: `Queue' undeclared (first use this function)

C:\Users\shoti\Desktop\/sim.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\Users\shoti\Desktop\/sim.cpp:11: error: expected `;' before "bankQueue"

C:\Users\shoti\Desktop\/sim.cpp:13: error: `LinkedList' undeclared (first use this function)

C:\Users\shoti\Desktop\/sim.cpp:13: error: expected `;' before "eventList"

C:\Users\shoti\Desktop\/sim.cpp:21: error: `eventList' undeclared (first use this function)

C:\Users\shoti\Desktop\/sim.cpp:26: error: 'struct ItemType' has no member named 'evenType'

C:\Users\shoti\Desktop\/sim.cpp:28: error: `bankQueue' undeclared (first use this function)

C:\Users\shoti\Desktop\/sim.cpp:28: error: `processArrival' undeclared (first use this function)

C:\Users\shoti\Desktop\/sim.cpp:31: error: `processDeparture' undeclared (first use this function)

C:\Users\shoti\Desktop\/sim.cpp: At global scope:
C:\Users\shoti\Desktop\/sim.cpp:34: error: variable or field `processArrival' declared void

C:\Users\shoti\Desktop\/sim.cpp:34: error: `int processArrival' used prior to declaration

C:\Users\shoti\Desktop\/sim.cpp:34: error: `itemType' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:34: error: `ifStream' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:34: error: `aFile' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:34: error: `LinkedList' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:34: error: `eList' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:34: error: `Queue' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:34: error: `bQ' was not declared in this scope

C:\Users\shoti\Desktop\/sim.cpp:35: error: initializer expression list treated as compound expression

C:\Users\shoti\Desktop\/sim.cpp:35: error: expected `,' or `;' before '{' token

Execution terminated


Here they are. I am really confused about the queue being undeclared. So as the linked list and the if stream.

Queue is not the syntax.

To declare a queue in C++ using C++ library, you do this :

queue<int> q1;
queue<float> q2;
queue< ItemType >q3;

For linked list you do this :

list<int> a1;
list<float> a2;
list<ItemType> s2;

Queue and LinkedList are Data Structures for Java, in C++ its , queue<type> and list<type>

I see I see. Although I got around it by just writing the "Queue" class and "LinkedList" class itself. Took me a like 45 minutes but eh. Thanks for your help though. I'll keep that queue in mind.

This article has been dead for over six months. Start a new discussion instead.