I'm pretty sure there's just a small bug I'm missing, but I can't seem to find it.

Error:

g++ -Wall Proj3.cpp -o proj3 -pthread
Proj3.cpp: In function âvoid initLockVar()â:
Proj3.cpp:52: error: expected primary-expression before â{â token
Proj3.cpp:52: error: expected `;' before â{â token
Proj3.cpp:256: error: expected `}' at end of input
make: *** [Proj3] Error 1

Code up to that point. In this comment-removed code block, line 34 in the code block corresponds to line 52 in the error (original code.)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <queue>
#include <iostream>
#include <fstream>
#include <signal.h>
using namespace std;

struct car {
  time_t arrivalTime;  // Arrival time of the car
  time_t startTime;    // Time car moves through the intersection
  int id;				 // ID# of the car
  char dir;			 // Which direction queue car is on
};

const int minToRun = 5;		// Minutes to run the simulation
// Marker for when to end the simulation.
time_t endTime = time(NULL) + (60 * minToRun);
int numCars; 				// Used to keep track of total cars.
queue<car> nStreetQueue;	// Queue of cars waiting on the north street.
queue<car> sStreetQueue;	// Queue of cars waiting on the south street.

// Mutex locks and conditional variables
pthread_mutex_t lock;
pthread_cond_t nNotEmpty;

// Output streams
ofstream carLog("carevent.log");
ofstream flagLog("flagperson.log");

void initLockVar() {
  lock = PTHREAD_MUTEX_INITIALIZER;
  nNotEmpty = PTHREAD_COND_INITIALIZER;
  srand(time(NULL));
  carLog << "carID     direction     arrival-time    start-time    end-time"
		 << endl;
  flagLog << "Time          State" << endl;
}

Any help would be greatly appreciated.

Since the lines in question are 52 and 256, and you posted only 40 lines, it's impossible to tell.

I figured the bug was somewhere before line 256, and to reiterate what I said before, line 52 in the original code corresponds to line 34 in the code block. I just wanted to remove some of the comments so i didn't actually post all 256 lines of code.

Line 256 is actually just the closing } in my code.

I could post it as is, I just figured that would be spammy.

Edited 6 Years Ago by therobot: n/a

The code you've posted seems to be OK, so if the problem still persists, go ahead and post all of the code.

The problem was this:

lock = PTHREAD_MUTEX_INITIALIZER;
nNotEmpty = PTHREAD_COND_INITIALIZER;

When the initializers were put up in the global section with the declarations, no compilation problem.

This question has already been answered. Start a new discussion instead.