compilation error in code blocks regarding class in C++

I Entered a program on operator overloading in C++ in code blocks. But error occurred during compilation.

//operator overloading where operator returns a value
#include<iostream>
#include<conio.h>
using namespace std;
class pair
{
private:
int x, y;
public:
pair(int a, int b)
{
x=a;
y=b;
}
void display()
{
cout<<"x= "<<x<<"y= "<<y<<endl;
}
friend int operator ++(pair);
};
int operator ++(pair m)
{
return(m.x+m.y);
}
void main()
{
pair p1(2,3);
p1.display();
cout<<"Result is "<<++p1;
}
If you tell us the error, we can help you much faster. If you don't tell us the error, you are relying on us to either read every line of your code, or compile it ourselves.

void main() is wrong. C++ uses
int main()

Sticking using namespace std; at the top of your code is a (very) bad idea and renders useless the whole purpose of namespaces. When you put using namespace std; at the top, you introduced something called pair to your code. Then, when you tried to create a class named pair, there was an error, because pair already exists.
Last edited on
It doesn't seem right to use the postfix operator here. Why not just use something like:
1
2
3
4
int pair::sum()
{
  return x + y;
}
@Moschops Thank you for your reply, that was really helpful. I corrected the return type of main. I am using namespace std; because in code blocks it will show an error that the function cout has not been declared.
I encountered the error that reference to pair is ambiguous, and below it was error: template<class _T1, class _T2> struct std::pair when I clicked on it, a header file appeared named stl_pair.h with a body of statements. Then it just clicked me that why not change the name of the class, and then the program was successsfully "built".

I tried this program because I was taught this in my C++ class to demonstrate the feature of C++ that how an operator can be overloaded. I just put the program here as I had seen on the board, except the void instead of int.
yes, the problem was in the name, becouse std::pair is already declared. So, you can mark this as solved or what?
closed account (zb0S216C)
AbdulHannan wrote:
I am using namespace std; because in code blocks it will show an error that the function cout has not been declared.

When you remove using namespace std, you'll have to prefix any types and/or functions that reside within the standard namespace with std::. For example, std::cout.

Wazzak
Last edited on
Yes my problem has been solved thanks all of you guys.
Now I want to know that can I delete my post?
If you have a bunch of couts, another option is to use: using std::cout; using std::endl; This will allow you to skip writing std:: in-front of cout and endl and will also allow you to avoid your std::pair conflict.
Last edited on
Now I want to know that can I delete my post?


Do not delete your post.
OK
Dont delete the post, but nark it as solved! on the top of the page, just below the title, there will be a button "mark as solved". Click it, and iinstead of the blue question mark the icon will be a green check. That will mean that the question is answered, the problem is solved, or whatever, and you won't even need to delete the post 'cos everyone will know they dont need to go read this becouse it's solved, OK?
OK, thank you. I did not know that this could be done.
Topic archived. No new replies allowed.