I need another set of eyes to look over this code. I must be overlooking something. I cannot figure out what the compiler's complaint is. When I try to compile, GCC gives me this error:
sim.cpp: In function âint main()â:
sim.cpp:21: error: ârunSimulationâ was not declared in this scope

Can someone point out what the problem is? Thanks.

Here is the code (the line that the compiler error refers to is on line 14 in the pasted section below):

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include "customerType.h"
#include "customerQueueType.h"
#include "serverType.h"
#include "serverListType.h"

using namespace std;

int main(){

  if (!runSimulation())
    cerr<< "Program will now end." << endl;
  return 0;  
}



bool setSimulationParameters(size_t& sTime, size_t& numServers, size_t& tTime,
                             size_t& tbArrival){
  //Precondition:
  //  The function is called with appropriate parameters
  //
  //Postcondition:
  //  If input is valid, values for all parameters are stored and true is
  //  returned, else false is returned and values are stored only for parameters
  //  for which input was valid.

  cout<< "Enter the simulation time in whole units: ";
  cin>> sTime;
  cout<< endl;

  if (!cin)
    return false;

  cout<< "Enter the number of servers: ";
  cin>> numServers;
  cout<< endl;

  if (!cin)
    return false;

  cout<< "Enter the average transaction time in whole units: ";
  cin>> tTime;
  cout<< endl;

  if (!cin)
    return false;

  cout<< "Enter the average time between customer arrivals in whole units: ";
  cin>> tbArrival;
  cout<< endl;

  if (!cin)
    return false;

  return true;
}



bool runSimulation(){

  size_t sTime; //Simulation time
  size_t numServers; //Number of servers
  size_t tTime; //Time each transaction will take
  size_t tbArrival; //Average time between customer arrivals

  int clock; //Simulation clock  
  float cutoff;
  //Cutoff point to determine if a customer arrives at a time given time unit
  int totalWait = 0; //Sum of the wait times of all customers served
  int totCust = 0; //Total number of customers that arrived
  int servedCust = 0; //Total number of customers that completed a transaction
  
  serverListType servers (numServers); //List of servers
  customerQueueType customers; //Queue of customers
  customerType cust;

  if (!setSimulationParameters(sTime, numServers, tTime, tbArrival)){
      cerr<< "Invalid entry." << endl;
      return false;
  }
  //Determine cutoff using Poisson distribution
  cutoff = pow(2.72,(1.0/tbArrival));

  for (clock=1; clock <= sTime; ++clock){

    //Update busy servers' transaction times, output which customers have
    //been served along with their departing times, and update total number
    //of served customers
    int before = servers.getNumberOfBusyServers();
    servers.updateServers(cout);
    servedCust = servedCust + (before - servers.getNumberOfBusyServers());

    if (!customers.empty())
      customers.updateWaitingQueue();

    //If customer arrives, customer enters queue
    if (((float)rand() / RAND_MAX) > cutoff){
      ++totCust;
      cust.setCustomerInfo (totCust, clock, 0, tTime);
      customers.push(cust);
    }

    //If a server is free and and customers are waiting, send the customer
    //at the front of the queue to the free server
    while (servers.getNumberOfBusyServers() < numServers){
      cust = customers.front();
      customers.pop();

      totalWait = totalWait + cust.getWaitingTime();

      servers.setServerBusy (servers.getFreeServerID(), cust);
    }
  }
  cout<< endl << "The simulation ran for " << sTime << " time units" << endl
      << "Number of servers: " << numServers << endl
      << "Average transaction time: " << tTime << endl
      << "Average time between customer arrivals: " << tbArrival << endl
      << "Total waiting time: " << totalWait << endl
      << "Number of customers that completed a transaction: " << servedCust
      << endl
      << "Number of customers left at servers: "
      << servers.getNumberOfBusyServers() << endl
      << "Number of customers left in the queue: " << customers.size() << endl
      << "Average waiting time: " << (float)totalWait/totCust << endl
      << "***************END SIMULATION***************" << endl;
  
  return true;
}

Dani AI

Generated

As noted, the GCC message "'was not declared in this scope'" means the compiler saw a name used in main that had not been introduced yet. In C++ each translation unit is parsed top‑to‑bottom: a function must be declared (a prototype) or defined before its first use. That is a compile‑time scope/declaration problem, not a linker error.

Ways to fix and common gotchas: add a forward declaration or move the function definition above main; put the declaration in a header and #include it where needed; or, if the function lives in a separate .cpp, make sure that file is actually compiled and linked into the final binary. Ensure the declaration exactly matches the definition (return type, parameter types, const/ref qualifiers, namespace). Also check for accidental namespace wrapping, static at file scope (restricts linkage), or C/C++ linkage mismatches (extern "C"). A stray typo or a missing semicolon in an earlier header can produce confusing scope errors, so run a full build after fixing the declaration.

Extra debugging tips: enable strict warnings and a modern standard to catch related issues early (for example, g++ -std=c++17 -Wall -Wextra -Wpedantic ...). More on declarations in the C++ language is at declarations. For GCC warning options see GCC warning options.

Recommended Answers

All 3 Replies

Delcare your function before main and try to compile agian

I knew it was going to be something like that; I've just been staring at code for too long so I kept missing it. Thanks Nathan.

No problem. Glad to help

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.