Dear All.

Basically I am having trouble passing my class object to myQueue. I know my Queue works as I had it working for Integers and Charachters but after approx 25hrs over 4 days I still can not get my Object to store in my Queue. Any and All help would be greatly appreciated.

As you can see on line 42 I have tryed to pass my object to the queue but with no satisfaction.

Thx

Tim

Dani AI

Generated

— concise troubleshooting and practical fixes (building on points from ).

Most failures when moving from primitives to user objects come from one of three issues: a type mismatch (the queue holds pointers but you push objects, or vice versa), broken copy semantics in your class (leading to crashes when the queue copies or destroys elements), or a wrong function signature/return type in your queue wrapper. Work through those in order and you will usually find the cause quickly.

Checklist to apply

  • Verify the exact element type your queue stores. If it stores T* you must push pointers; if it stores T push objects or references. Match push/enqueue signature to that type.
  • Distinguish compile errors from runtime crashes. For crashes, suspect double-free / dangling pointer caused by a missing copy ctor / assignment operator / destructor.
  • Isolate the problem with a minimal test: create a tiny class that only contains an std::string, push it into the queue and read it back. If that works, the issue is something inside your original class (raw pointers, custom destructor, etc.).
  • Prefer modern, safer choices: store objects by value when possible, or store smart pointers to avoid manual ownership bugs.

Minimal examples to try (replace Customer with your class):

// value semantics: requires a correct copyable Customer
std::queue<Customer> q;
q.push(Customer("Bob"));
auto copy = q.front();  // copied safely if Customer is well-formed
// pointer semantics: avoids copying the object itself
std::queue<std::shared_ptr<Customer>> q;
q.push(std::make_shared<Customer>("Bob"));
auto p = q.front();     // p is a shared_ptr

Final notes: if your class manages raw memory, implement the rule-of-three (or rule-of-five in modern C++) or switch to std::string/smart pointers. Run with an address checker (Valgrind / ASan) to catch use-after-free or double-free quickly.

Recommended Answers

All 3 Replies

Here's a quick guess without trying to compile, look too closely...
I think because QueueElement is typedef-ed to customer * and not customer
and Tim is a customer, not a customer *.

Here's a quick guess without trying to compile, look too closely...
I think because QueueElement is typedef-ed to customer * and not customer
and Tim is a customer, not a customer *.

Winbatch, Unfortunately I tryed and it did not compile. Now currently running on 30hrs after 5 days. starting to get desperate. Can you suggest another line of attack.

Regards
Thx for the response
Tim

Do as I mentioned in my earlier post and also remove the const on the addQueue function. Also, since you are returning a QueueElement when you call Q.front(), you should ask for the name (or id, or something else). Otherwise, you need to write the operator << function for QueueElement.

cout<<Q.front().getName();

Here's what I got after making the changes above.

a - add to queue
d - display ALL
e - check if queue is empty
f - check the front value
r - remove from queue
s - delete the list
q - quit Program
a
Add Details
Add Name
BOB
TEST:BOB

a - add to queue
d - display ALL
e - check if queue is empty
f - check the front value
r - remove from queue
s - delete the list
q - quit Program
f
The front element is
BOB
a - add to queue
d - display ALL
e - check if queue is empty
f - check the front value
r - remove from queue
s - delete the list
q - quit Program


-------------------
Note there are other things that are wrong with the code. (It will crash when trying out some of the other options - you just need to work through it/debug it - I'm too lazy)

By the way, there is a MUCH better way to do this. Instead of making an array of a fixed length, you should write a wrapper around the vector class. You can have the vector hold QueueElements.

(ie:
vector<QueueElement> myVect;
myVect.push_back( Tim );
...etc..

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.