Celyn Marasigan
Celyn Marasigan

Reputation: 1

What does undeclared identifier and identifier is undefined mean? How to fix the error?

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define TIMEQUANTUM 1;
#define MAXPROCTIME 100;


struct SortList
{
    int pid;
    int arrivalTime;
    int burstTime;
    SortList *nxt;
};

struct JobList {
    int pid;
    int ct_arrival;
    int ct_service;
    int vt_served;
    int vt_residence;
    int vt_wait;
    int vt_completion;
    JobList *nxt;
};



bool initJobList(JobList *&,int *,int);
bool deinit(JobList *&);
bool doFCFS (JobList *&);
bool doRR (JobList *&);
bool doSJF(JobList *&, SortList *&);
bool doNPSJF (JobList *&);
bool doPSJF (JobList *&);
bool sortJob (JobList *&, SortList *&);

int _tmain(int argc, _TCHAR* argv[])
{

    JobList *ptrJobStart;
    // arrivaltime , bursttime
    int arrJobs[][2] = {
                        {0,7},
                        {2,4},
                        {4,1},
                        {5,4}
    };   
    SortList *ptrList;
    initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
    printf("\ndoFCFS\nCPU\t\tPROCESS\n");
       doFCFS (ptrJobStart);
    deinit(ptrJobStart);

    initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
    printf("\n\ndoSJF\nCPU\t\tPROCESS\n");
        doSJF(ptrJobStart, ptrList);
    deinit(ptrJobStart);


    initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
    printf("\ndoRR\nCPU\t\tPROCESS\n");
    doRR(ptrJobStart);
    deinit(ptrJobStart);

    initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) / (sizeof(int)*2)));
    printf("\ndoPSJF\nCPU\t\tPROCESS\n");
    doRR(ptrJobStart);
    deinit(ptrJobStart);

    getch();
    return 0;   
}

bool doRR(JobList *&ptrJobStart)
{


    if(ptrJob==NULL) return false;
    JobList *ptrJobStart, *tempJobList;
    ptrJobStart = ptrJob;
    int maxTime = MAXPROCTIME;
    int timeQuantum = TIMEQUANTUM;
    bool flag = false;
    ptrJobStart->vt_served = 0;
    for(int processTime = 0; (processTime < maxTime) && !flag ;)
    {
        //check if all jobs are finished
        flag = true;
        tempJobList = ptrJob;
        while(tempJobList!=NULL)
        {
            if(tempJobList->vt_served!=tempJobList->ct_service)
            {
                flag = false;
            }
            tempJobList = tempJobList->nxt;
        }
    }
    //logic
    return true;
}

bool doSJF(JobList *&ptrJobStart, SortList *&ptrList)
{
    if(ptrJobStart==NULL) return false;
    sortJob(ptrJobStart, ptrList); 
    doFCFS (ptrJobStart);
    return true;
}

bool sortJob (JobList *&ptrJob, SortList *&ptrList)
{
    JobList *ptrJobStart = ptrJob;
    if(ptrJobStart==NULL) return false;
    int maxTime = MAXPROCTIME;
    SortList *newList;
    ptrList = NULL;
    for(; ptrJobStart!=NULL; ptrJobStart = ptrJobStart->nxt) // accumulate all arrival time = 0
    {
        if(ptrJobStart->ct_arrival==0)
        {
            if(ptrList==NULL)
            {
                ptrList = new SortList;
                ptrList->pid = ptrJobStart->pid;
                ptrList->arrivalTime = ptrJobStart->ct_arrival;
                ptrList->burstTime = ptrJobStart->ct_service;
                ptrList->nxt = NULL;
            }
            else
            {
                newList = new SortList;
                newList->pid = ptrJobStart->pid;
                newList->arrivalTime = ptrJobStart->ct_arrival;
                newList->burstTime = ptrJobStart->ct_service;
                newList->nxt = NULL;
                ptrList->nxt = newList;
            }
        }
    }
    SortList *tempList;
    tempList = ptrList;
    for(int processTime = 1 ; processTime < maxTime ; processTime++) // accumulate all burst time from 1 to 100
    {
        ptrJobStart = ptrJob;
        do
        {
            if((ptrJobStart->ct_arrival!=0) && (processTime == ptrJobStart->ct_service))
            {
                newList = new SortList;
                newList->pid = ptrJobStart->pid;
                newList->arrivalTime = ptrJobStart->ct_arrival;
                newList->burstTime = ptrJobStart->ct_service;
                newList->nxt = NULL;
                tempList->nxt = newList;
                tempList = tempList->nxt;
            }
            ptrJobStart = ptrJobStart->nxt;
        }while(ptrJobStart!=NULL);
    }
    //sort jobs
    ptrJobStart = ptrJob;
    JobList *tempJobList;
    tempJobList = ptrJobStart;
    for(; ptrList!=NULL; ptrList = ptrList->nxt)
    {
        tempJobList->pid = ptrList->pid;
        tempJobList->ct_arrival = ptrList->arrivalTime;
        tempJobList->ct_service = ptrList->burstTime;
        tempJobList = tempJobList->nxt;
    }
    return true;
}


bool doFCFS (JobList *& ptrJob)
{
    JobList *ptrJobStart = ptrJob;
    if(ptrJobStart==NULL) return false;
    int maxTime = MAXPROCTIME;
    ptrJobStart->vt_served = 0;
    for(int processTime = 0; processTime < maxTime; processTime++)
    {
        if(ptrJobStart->vt_served!=ptrJobStart->ct_service)
        {
            ptrJobStart->vt_served+=1;
        }
        else
        {
            ptrJobStart->vt_completion = processTime;
            ptrJobStart->vt_residence = processTime - ptrJobStart->ct_arrival;
            ptrJobStart->vt_wait = processTime - ptrJobStart->vt_served;
            cout<<"\n/* vt_served:"<<ptrJobStart->vt_served<<"\tvt_residence:"<<ptrJobStart->vt_residence
                <<"\tvt_wait:"<<ptrJobStart->vt_wait<<"\tvt_completion:"<<ptrJobStart->vt_completion<<" */";
            ptrJobStart = ptrJobStart->nxt;
            if(ptrJobStart==NULL) break;
            else
                ptrJobStart->vt_served = 1;
        }
        cout<<"\n "<<processTime<<"\t>>\t P"<<ptrJobStart->pid;
    }
    return true;
}


bool deinit (JobList *& ptrJobStart) {

    JobList *ptrJobDel = NULL;
    do {

        ptrJobDel = ptrJobStart;
        ptrJobStart = ptrJobStart->nxt;
        delete ptrJobDel;
    }while (ptrJobStart != NULL);
    return true;
}


bool initJobList(JobList*& ptrJobStart,int *pJob,int nJobs){

    JobList * ptrJobs = NULL;
    int i;
    int j=1;

    for (i=0; i<nJobs*2; i+=2) {
        if (ptrJobs == NULL) {
            ptrJobs = new JobList;
            memset(ptrJobs,0,sizeof(JobList));
            ptrJobStart = ptrJobs;
        }
        else {
            ptrJobs->nxt = new JobList;
            memset(ptrJobs->nxt,0,sizeof(JobList));
            ptrJobs = ptrJobs->nxt;
        }
        ptrJobs->ct_arrival = *(pJob+i);
        ptrJobs->ct_service = *(pJob+(i+1));
        ptrJobs->pid = j;
        ptrJobs->nxt = NULL;
        j++;
    }
    return true;

This code is for the scheduling algorithm. The shortest Job Next(SJN) / Shortest Job First (SJF) (which is the doSJN) is working. But when I included the doRR (do Round Robin), there are a lot of errors occuring.

error C2065: 'ptrJob' : undeclared identifier error C2082: redefinition of formal parameter 'ptrJobStart error C2065: 'ptrJob' : undeclared identifier error C2065: 'ptrJob' : undeclared identifier see declaration of 'getch'

How can i fix this?

Upvotes: 0

Views: 1384

Answers (2)

David W
David W

Reputation: 10184

The first error (redefinition of formal parameter 'ptrJobStar') occurs here:

bool doRR(JobList *&ptrJobStart)
{


    if(ptrJob==NULL) return false;
    JobList *ptrJobStart, *tempJobList;

You're defining a parameter ptrJobStart, then declaring the same-named parameter in the code.

I believe the second error ('ptrJob': Undeclared identifier) occurs here:

bool doRR(JobList *&ptrJobStart)
{


    if(ptrJob==NULL) return false;

That's because 'ptrJob' doesn't appear to be declared within doRR().

Hope that helps.

Upvotes: 1

cedrou
cedrou

Reputation: 2790

In doRR, the argument is named ptrJobStart. It should be ptrJob instead.

The error you got states that on the first line of doRR, ptrJob has not been declared.

Upvotes: 1

Related Questions