Error

closed account (z8q4izwU)
Hello i need help i cant figure out this error can any one help?
The error is cpp no match for 'operator<<' in 'std::cout << ordered' at 30, 31,32, 82, 83, 84



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cstdlib>
#include <iostream>

#include "stack.h"

using namespace std;

int main(int argc, char *argv[])
{
    stack ordered;
    stack temp1;
    stack temp2;
    
    int number;
    int item;
    
    
    cout << "Enter the number of elements in the stack: ";
	cin >> number;
	
		for (int i=0; i < number; i++)
	{
		item = rand()%100 + 1; 
		
        temp1.push(item);
	}
	
	cout<<"Stacks at the start: "<<endl;
	
    cout << ordered;
    cout << temp1; 
    cout << temp2;
	
	while(!temp1.empty() || !temp2.empty())
	{
        if(temp2.empty())
	    {
     	stack temp = temp1;
	    
        int largest = temp1.pop();
		
        while(!temp1.empty())
			{
				int element = temp1.pop();
		
        		if (largest < element)
		  			largest = element;
			}
			while(!temp.empty())
			{
				int element = temp.pop();
		
        		if (element!=largest)
					temp2.push(element);
			}
			
			ordered.push(largest);
        }
        else
        {
        stack temp=temp2;
		
        int largest = temp2.pop();
		
        while(!temp2.empty())
		{
			int element = temp2.pop();
		
        	if (largest < element)
				largest = element;
		}
		while(!temp.empty())
		{
			int element = temp.pop();
		
        	if (element!=largest)
				temp1.push(element);
		}
		ordered.push(largest);
    }
   	
	cout << ordered;
    cout << temp1;
    cout << temp2;
	}	
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
Maybe you have to overload the operator "<<" for your stack class? Can you show the code from that header file "stack.h"?
you need to override the << operator in your stack class to take a std::ostream&.

std::ostream& operator << (std::ostream& output)
{};

closed account (z8q4izwU)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>

using namespace std;

const int stack_size = 1000;

class stack
{
      private:
          int data [stack_size];  // data for the stack
          int top;  // index of the top of the stack
      public:
          stack ();  // creates an empty stack
          void push (int item);  // puts item on the top of the stack
          int pop ();  // removes and returns the top of the stack
          bool full ();  // returns true if the stack is full
          bool empty (); // returns true if the stack is empty
		  void print ();
};

// constructor creates an empty stack
stack::stack ()
{
      top = -1;
}   

// push adds a new elment, item, to the top of the stack
void stack::push (int item)
{
     // if the stack is full, print an error message
     if (full ())
     {
         cout << "\n\nStack Error: Pushing on a full stack";
         cout << "\nThe element being pushed was: " << item;
     }
     else // OK to push an element
     {
          top++;
          data [top] = item;
     }
}

// pop removes and returns the top element of the stack
int stack::pop ()
{
     // if the stack is empty, print an error message
     if (empty ())
     {
         cout << "\n\nStack Error: Popping an empty stack";
         cout << "\nReturning ?";
         return -1;
     }
     else  // OK to pop
     {
         top--;
         return data [top + 1];
     }
}

// empty returns true if the stack is empty, else it returns false
bool stack::empty ()
{
     return top == -1;
}

// full returns true if the stack is full, else it returns true
bool stack::full ()
{
     return top == stack_size - 1;
}

void stack::print()
{
	int item;
	if (empty())
	{
		cout << "EMPTY";
	}
	while(!empty())
	{
		item=pop();
		cout << item << " ";
	}
	cout << endl;
}
I don't think you can overload the stream operators in a class. I'm pretty sure you have to do it as a top level function.
ok forsaken, I guess the question is, what exactly do you want output from those stack objects in lines 30-32? ..maybe some sample output?
Maybe you should operate on members of those objects separately instead of trying to "cout<<" the object itself..
closed account (z8q4izwU)
So make a print function before the main line? And i want it to print the numbers moving.. such as..

ordered:
temp1:2 20 1
temp2:

ordered:20
temp1:
temp2: 2 1

ordered:2 20
temp1:1
temp2:

ordered:1 2 20
temp1:
temp2:
Topic archived. No new replies allowed.