C ++ std :: queue doesn't want to press ()

This is a simple class and a simple test function:

#include <queue> #include <string> namespace { using namespace std; } class NameStream { queue<string> stream; public: string operator * () { return stream.front(); } NameStream &operator ++(int) { stream.pop(); return *this; } NameStream &operator ++() { stream.pop(); return *this; } NameStream &operator <<(string name) { stream.push(name); } operator bool() { return !stream.empty(); } }; inline void nameStreamTest() { NameStream &stream = *new NameStream; stream << "hi" << "hey" << "hoy"; while (stream) { printf("%s\n", (*stream++).c_str()); } } 

He falls in

 NameStream &operator <<(string name) { stream.push(name); } 

inside the push queue procedure, here is the stack outside of my code:

 #0 0x000b5079 in std::deque<std::string, std::allocator<std::string> >::push_back at stl_deque.h:1055 #1 0x000b50f2 in std::queue<std::string, std::deque<std::string, std::allocator<std::string> > >::push at stl_queue.h:204 #2 0x000b511c in NameStream::operator<< at NameStream.h:24 #3 0x000b520f in nameStreamTest at NameStream.h:32 

In this case, my experience does not work. What am I doing wrong?

PS :.

 NameStream &stream = *new NameStream; 

Used to delete a location

 stream 

an object at the address (offset?) 0x7d (!), which raises the same exception.

+4
source share
4 answers

Put the return statement at the end of your function.

 NameStream &operator <<(string name) { stream.push(name); return *this; } 

Add . Not returning a valid reference / value (if necessary) will result in UB, and this will cause hidden crashes or bad behavior, which are often difficult to debug (as in your case). Therefore, never ignore the compiler warning if it generates any file.

+6
source

It does not work, because the postfix operator ++ has a higher priority than the * operator, therefore, if for the last iteration of the loop, pop from the queue is executed first, and then try to make front on which the queue is empty. To solve this problem, the easiest way is to break it into two statements of the type:

 while (stream) { printf("%s\n", (*stream).c_str()); stream++; } 
+2
source

He falls in

 NameStream &operator <<(string name) { stream.push(name); } 

You do not return *this ... by chaining operator<<() , the second and third do not have a valid object address to work with.

0
source

You have a number of problems in your code.

  • There is no return to operator<< (as indicated by iammilind et al.), Which causes a runtime error that you are observing. It can be fixed as follows:

     NameStream &operator <<(string name) { stream.push(name); return *this; } 
  • Other problems are related to the sequence of calls in the body of your while loop:

    • operator ++ (int)
    • operator * ()

    Therefore, in the last iteration, you first pop() in operator++(int) , and then try calling front() in operator*() on an empty queue that will throw a runtime error.

    The easiest way to fix this is to rewrite the while loop as follows.

     while (stream) { printf("%s\n", (*stream).c_str()); ++stream; } 
  • Memory leak. You call the selection of the NameStream object with new , but never call delete . A simple solution is to allocate a stream object on the stack:

     NameStream stream; 
0
source

All Articles