Expected unqualified id before '+' token

Can someone help me with this?
I keep getting the error above.("Expected unqualified id before '+' token")

Here's the code:

#include<iostream>
using namespace std;

class sum
{
int x,y;
public:

void add(x,y)
{
cout<<"Enter two numbers"<<"\n";
cin>>x>>y;

cout<<x<<"+"<<y<<"="<<x+y<<"\n";

}

};
sum o;

int main()
{
int a,b,k;
here:
cout<<"Press 1 to add two numbers."<<"\n";
cout<<"To exit, Press 2."<<"\n";
cin>>k;
if(k==1)
{
o.add(a,b);
goto here;
}
if(k==2)
{
return 0;
}


}


sorry if the code's amateur.(C++ is my optional subject and honestly I have no idea what happens at school...)

cheers!
In your class you haven't defined what the types of x and y are in your member function add.

Other then that, you have two other issues I see right now:
1) Don't use gotos here; use a loop or something instead
2) Sum is a rather useless class...but this is probably for an assignment I suppose
firedraco,

i defined types for both x and y, but no result. and yes, it's an assignment; i have to show the teacher i know what is a class and its object.
btw, we learn C++ in school on a TurboC compiler. My eyes kinda hurt coz of the blue screen, so I switched to DevC++ and Code::Blocks. This thing here was done in Code::Blocks. Maybe there's some setting I need to fix? There's only so much in Code::Blocks I don't understand...
i defined types for both x and y, but no result

No, you did not.

You have defined member variables 'x' and 'y' of the class, but you have not defined the parameters of the member function "add". Furthermore, reusing the names 'x' and 'y' for parameters creates confusion, because the class has members with those names.
1
2
3
4
5
6
7
8
here:
cout<<"Press 1 to add two numbers."<<"\n";
cout<<"To exit, Press 2."<<"\n";
cin>>k;
if(k==1)
{
o.add(a,b);
goto here;


Read up on 'do...while', 'while' and 'for' to use for control flow in program. NEVER USE GOTO is a good general rule for you to follow.

1
2
3
4
5
6
7
//Example
while(k != 2)
{
    //Do something
    std::cout << "Enter 2 to exit.\n";
    std::cin >> k; 
}


And please please use code tags...
Last edited on
hi, im sorry for deserting the topic here. i never solved the problem. however, i started using dev c++ and found out that i had the errors only in code blocks. the program worked fine on dev c++. i have no idea why code blocks is giving me a hard time...
The problem's fixed. I uninstalled code blocks and deleted all files with that keyword by performing a search. Turns out that was the problem. Code blocks works fine now. Although I have no idea why.


Thanks for the help.
Topic archived. No new replies allowed.