954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I access variables in my queue?

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<

compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

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

Intel
Light Poster
30 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

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?

compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

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

compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You