Ok, I have three queueus, MM, FP & TXT. Each of them contain objects which have variables associated with them. How do I access any of the variables that the front of my Queue points to?

For example, I am trying to print out the name of the item that is in the begginning of my MM queue.

I want to type something like...

cout<<mm.front.name<<;

but that doesn't work. I tried many variations on that but to no avail. Below is my pertinent code. Any help is greatly appreciated. Thanks!

class NodeType
{
public:
job info;
NodeType* next;
NodeType* back;
};



class Queue
{
public:
Queue();
~Queue();


void makeEmpty();
void addFrontNode(job newJob);
void addRearNode(job newJob);
void delNode(job &delJob);
bool isEmpty() const;
bool isFull() const;
int getItems();
int items;


NodeType* front;
NodeType* rear;
};



class job
{
public:
int switches, fTime, sTime;
int length, aTime;
string name;
string type;
string urgent;
string getName()
{
return name;
}


job()
{
switches=0;
fTime=0;
sTime=0;
};
~job()
{
};

Easy,
1. You have 'front' defined as pointer and you're using the '.', think of '->'
2. You are accessing name, but name is defined in class 'info'

i could fix it but it looks like you'll get it.

INTEL

Easy,
1. You have 'front' defined as pointer and you're using the '.', think of '->'
2. You are accessing name, but name is defined in class 'info'

i could fix it but it looks like you'll get it.

INTEL

ok, I changed the output lines to this...

cout<<"MM ITEMS:  "<<mm.items<<endl;
     cout<<"FP ITEMS:  "<<fp.items<<endl;
     cout<<"TXT ITEMS:  "<<txt.items<<endl;
     cout<<"CTIME:  "<<cTime<<endl;
     cout<<"Front Name: "<<mm.front->name<<endl;

Now it prints out the number of items in each queue but the last line only prints out a bunch of nonsense and I get an access violation error.

It sounds like my pointer is pointing somewhere it shouldn't be. What is going on?

I also changed around the queue class and the nodetype class to this...

class NodeType
{
public:
NodeType* next;
NodeType* back;
int	switches, fTime, sTime;
int length, aTime;
string name;
string type;
string urgent;
string getName()
{
    return name;
}    

NodeType()
{
	switches=0;
	fTime=0;
	sTime=0;
};
~NodeType()
{
};

class Queue
{  
public:
    Queue();
    ~Queue();
    
    void makeEmpty();
    void addFrontNode(NodeType newJob);
    void addRearNode(NodeType newJob);
    void delNode(NodeType &delJob);
    bool isEmpty() const;
    bool isFull() const;
    int getItems();
    int items;
    
    NodeType* front;
    NodeType* rear;
};

Also, please look at my addNode fuction. Maybe its not actually putting the items into the queue?

void Queue::addRearNode(NodeType newJob)
{
    if (isFull())
        throw FullQueue();
    else 
    {
        NodeType* newNode;
        newNode = new NodeType;
        newNode = &newJob;
        newNode->next = NULL;
        if (rear == NULL)
                front = newNode;
        else
                rear->next = newNode;
        rear = newNode;
        items++;
    }
}    

void Queue::addFrontNode(NodeType newJob)
{
    if (isFull())
        throw FullQueue();
    else
    {
        NodeType* newNode;
        newNode = new NodeType;
        newNode = &newJob;
        newNode->next = NULL;
        if (front ==NULL)
                rear = newNode;
        else
                front->next = newNode;
        front = newNode;
        items++;
    }
}

Any Help?

Never mind, I figured it out!!! :cheesy:

Here is the code I had to change...

void Queue::addRearNode(NodeType newJob)
{
    if (isFull())
        throw FullQueue();
    else 
    {
        NodeType* newNode;
        newNode = new NodeType;
        newNode->name = newJob.name;
        newNode->aTime = newJob.aTime;
        newNode->length = newJob.length;
        newNode->switches = newJob.switches;
        newNode->fTime = newJob.fTime;
        newNode->sTime = newJob.sTime;
        newNode->urgent = newJob.urgent;
        newNode->type = newJob.type;
        
        newNode->next = NULL;
        if (rear == NULL)
                front = newNode;
        else
                rear->next = newNode;
        rear = newNode;
        items++;
    }
}    

void Queue::addFrontNode(NodeType newJob)
{
    if (isFull())
        throw FullQueue();
    else
    {
        NodeType* newNode;
        newNode = new NodeType;
        newNode->name = newJob.name;
        newNode->aTime = newJob.aTime;
        newNode->length = newJob.length;
        newNode->switches = newJob.switches;
        newNode->fTime = newJob.fTime;
        newNode->sTime = newJob.sTime;
        newNode->urgent = newJob.urgent;
        newNode->type = newJob.type;
        newNode->next = NULL;
        if (front ==NULL)
                rear = newNode;
        else
                front->next = newNode;
        front = newNode;
        items++;
    }
}

I was trying to just assign the name of the old object to the new one. But I had to actually assign each member in the old object to each member in the new object. Once I did that, everything worked out!

And besides figuring it out, it makes sense too!!! :p

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.