inside Queue.h
Expand|Select|Wrap|Line Numbers
- class RequestObject
- {
- public:
- // Constructor
- RequestObject(){ mType = -1; }
- virtual ~RequestObject(){}
- int GetType() const { return mType; }
- void SetType(int i) { mType = i; }
- private:
- int mType;
- };
- class IntDoublePair : public RequestObject
- {
- public:
- IntDoublePair(){ mInt = 0; mDouble = 0.0; }
- IntDoublePair(int i, double d){ mInt = i; mDouble = d; }
- IntDoublePair(RequestObject O){}
- virtual ~IntDoublePair(){}
- void SetInt(int value){ mInt = value; }
- void SetDouble(double value){ mDouble = value; }
- int GetInt(){ return(mInt); }
- double GetDouble(){ return(mDouble); }
- private:
- int mInt;
- double mDouble;
- };
- class QueueObject
- {
- public:
- QueueObject(){}
- ~QueueObject(){}
- // Public Methods
- void Add(const RequestObject &Request);
- bool Empty();
- RequestObject Pop();
- void Clear();
- private:
- std::queue<RequestObject> Q;
- };
Expand|Select|Wrap|Line Numbers
- #include "Queue.h"
- using namespace std;
- void QueueObject::Add(const RequestObject &Request)
- {
- RequestObject O = Request;
- Q.push(O);
- }
- RequestObject QueueObject::Pop()
- {
- RequestObject O = Q.front();
- Q.pop();
- return(O);
- }
- bool QueueObject::Empty()
- {
- return(Q.empty());
- }
- void QueueObject::Clear()
- {
- while(!Q.empty())
- {
- Q.pop();
- }
- }
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include "Queue.h"
- using namespace std;
- int main(int argc, char *argv[])
- {
- QueueObject mQ;
- for(int i = 0; i < 10; ++i)
- {
- IntDoublePair tID(i, (i*1000.0/10.0));
- tID.SetType(i);
- mQ.Add(tID);
- }
- while(!mQ.Empty())
- {
- IntDoublePair ip = mQ.Pop();
- cout << "Int = " << ip.GetInt() <<
- " Doub = " << ip.GetDouble() <<
- " Type = " << ip.GetType() << endl;
- }
- return(0);
- }
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
Int = 9 Doub = 900 Type = -1
where it should be:
Int = 0 Doub = 0 Type = 0
Int = 1 Doub = 100 Type = 1
Int = 2 Doub = 200 Type = 2
Int = 3 Doub = 300 Type = 3
Int = 4 Doub = 400 Type = 4
Int = 5 Doub = 500 Type = 5
Int = 6 Doub = 600 Type = 6
Int = 7 Doub = 700 Type = 7
Int = 8 Doub = 800 Type = 8
Int = 9 Doub = 900 Type = 9
So I have two questions:
Has anyone already implemented anything like this?
If not, does anyone have an idea of why this code is not behaving as expected?
Thanks.