error: two or more data types in declaration of main?
Solution 1
None of that code is valid. You begin a class definition with class OperateClass
, but never end it and run right into main
. A (simplified) class definition takes the form of:
class [name] {
}; // semi-colon terminates definition
// so...
class OperateClass {
};
Next, you declare main
... but it leads to an else
branch (?).
int main()
{
cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
cin >> ans;
if(ans == "add"){
OperateClass opOper;
opOper.add();
}else{ // what is this?
Functions must also terminate via their closing brace, i.e.,
int main() {
} // function is over!
Now it looks like those may just be copy/paste errors. If that's the case then you probably just forgot the semi-colon at the end of the class definition.
Solution 2
Consider the following program:
class C
int main() {
}
class C {
}
void someFunc(){}
This is basically your program boiled down to the necessities. Here are the errors:
error: two or more data types in declaration of 'main' error: expected ';' after class definition
Here's the corrected code:
class C; //<--semicolon in forward declaration
int main() {
}
class C {
}; //<--semicolon after class definition
void someFunc(){}
Solution 3
Lets make a slight modification of your code so that your syntax is right.
int main()
{
cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
cin >> ans;
if(ans == "add"){
OperateClass opOper;
opOper.add();
}
else if (ans == "subtract"){
OperateClass opOper;
opOper.subtract();
}
else if (ans == "multiply"){
OperateClass opOper;
opOper.multiply();
}
else if(ans == "divide"){
OperateClass opOper;
opOper.divide();
}
else {}
} //end of main
Your class variable declaration statement "OperateClass opOper" also repeats in each case, you could also just write that statement outside your if-else conditions to avoid repetition since that statement is true regardless of any case.
Bucky763
Updated on July 09, 2022Comments
-
Bucky763 almost 2 years
Very new to coding, so please be understanding ;)
I'm basically trying to make a calculator using the
while
loop, andif
statements.#include <iostream> using namespace std; int x = 1; int number; int total = 0; int amt = 1; int a; int b; int c; int d; string ans; class OperateClass
I get the error: two or more data types in declaration of 'main'
Please explain what this means/how to fix it.
I was also wondering if I need to make a new object for each function (Add, subtract, multiply, and divide)
Please help!
int main() { cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl; cin >> ans; if(ans == "add"){ OperateClass opOper; opOper.add(); }else{ if(ans == "subtract"){ OperateClass opOper opOper.subtract(); } }else{ if(ans == "multiply"){ OperateClass opOper opOper.multiply(); } }else{ if(ans == "divide"){ OperateClass opOper opOper.divide(); } } } class OperateClass{ public: int add(){ while(x <= 3){ cout << "Enter a number to use to add: " << endl; cin >> a; total = total + a; x++; amt++; } } int subtract(){ while(x <= 3){ cout << "Enter a number to use to add: " << endl; cin >> b; total = total - b; x++; amt++; } } int multiply(){ while(x <= 3){ cout << "Enter a number to use to add: " << endl; cin >> c; total = total * c; x++; amt++; } } int divide(){ while(x <= 3){ cout << "Enter a number to use to add: " << endl; cin >> d; total = total / d; x++; amt++; } } } int print(){ cout << "Your total is: " << total << endl; return 0; }
-
Lwin Htoo Ko almost 12 yearsyou should use if-else if-else or switch. what you are doing now is confusing. difficult to find open and close brace.
-
chris almost 12 yearsThose global variables all over the place are really not a good idea.
-
Lwin Htoo Ko almost 12 yearsIn "if(ans == "subtract"/"multiply"/"divide")" you forget ";" after "Operate Class opOper"
-
Bucky763 almost 12 yearsAlright, I'm no longer getting the semicolon errors
-
Bucky763 almost 12 yearsNow I'm getting error: aggregate 'OperateClass opOper' has incomplete type and cannot be defined. How would this be corrected?
-
chris almost 12 years@Bucky763, Just kill the
class OperateClass;
and move the whole class there instead. -
Keith Thompson almost 12 yearsThe usual way to write an
if
/else if
/ sequence is to treat the "else if" as if it were a single word. I can't format it correctly in a comment, but this gist shows the more traditional way to do this. (Some languages even haveelsif
orelseif
as a keyword; C++ doesn't.) -
chris almost 12 yearsA switch would work well here too.
-
Bucky763 almost 12 yearsOk I moved the Class definition before the main. The program runs now!
-
Bucky763 almost 12 yearsNow im wondering how to get
cout << "Your total is: " << total;
to run
-
-
chris almost 12 yearsThere's a hidden
if
. It's there, though. -
Ed S. almost 12 years@chris: Yeah that's why I added that last bit on. I think a missing semi-colon is probably the issue and this was just a bad copy/paste job.
-
Bucky763 almost 12 yearsAlright. I'm going to edit the original code and add the semicolons.
-
Bucky763 almost 12 yearsNow I'm getting the error: aggregate 'OperateClass opOper' has incomplete type and cannot be defined
-
Keith Thompson almost 12 years@Bucky763: Please don't do that. With the code corrected, the question no longer makes sense. I'll roll back the edit for you.
-
chris almost 12 yearsWell I think it's been determined now that the problem was semicolons, which are in here among other things. Upvote for ya.
-
chris almost 12 yearsJust kill the class OperateClass; and move the whole class there instead.
-
Bucky763 almost 12 yearsMoving the class def before main got the program to run!
-
Bucky763 almost 12 yearsThe only thing is, it wont run this function I added
void print(){ cout << "Your total is: " << total << endl; }
-
chris almost 12 years@Bucky763, A new question completely is more suited for further problems, but as a random guess, did you ever call it?
-
Bucky763 almost 12 yearsIll make a new question. Thanks. And can you give an example of calling it?
-
Bucky763 almost 12 yearsI think I forgot that. Adding it to main should be fine?
-
chris almost 12 years@Bucky763, Wherever you want the printing to occur.