错误:main的声明中有两个或多个数据类型
error: two or more data types in declaration of main?
对编码非常陌生,所以请理解;)
我基本上是在尝试使用while
循环和if
语句来制作一个计算器。
#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
我得到错误:"主"声明中有两个或多个数据类型
请解释这意味着什么/如何修复。
我还想知道是否需要为每个函数(加法、减法、乘法和除法)创建一个新对象
请帮忙!
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;
}
这些代码都无效。您可以从class OperateClass
开始类定义,但永远不要结束它并直接运行到main
。(简化的)类定义采用以下形式:
class [name] {
}; // semi-colon terminates definition
// so...
class OperateClass {
};
接下来,您声明main
。。。但它导致CCD_ 6分支(?)。
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?
功能也必须通过其右括号终止,即
int main() {
} // function is over!
现在看来,这些可能只是复制/粘贴错误。如果是这种情况,那么您可能只是忘记了类定义末尾的分号。
考虑以下程序:
class C
int main() {
}
class C {
}
void someFunc(){}
这基本上是你的程序归结为必需品。以下是错误:
错误:"main"的声明中有两个或多个数据类型错误:应为";"类定义之后
这是更正后的代码:
class C; //<--semicolon in forward declaration
int main() {
}
class C {
}; //<--semicolon after class definition
void someFunc(){}
让我们稍微修改一下代码,使您的语法正确。
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
您的类变量声明语句"OperateClass opOper"在每种情况下都会重复,您也可以在if-else条件之外编写该语句以避免重复,因为无论在何种情况下,该语句都是true。
相关文章:
- 可以将两个相同类型的连续数组视为一个数组吗?
- [[no_unique_address]] 和两个相同类型的成员值
- 生成一个类Name_class并将两种数据类型存储在一个向量中
- C++将两个不同类型的向量的属性连接到新的向量中
- 如何在 c++ 中将两个不同类型的变量分配给一个变量
- 为什么我不能在同一行中定义两个相同类型的类的成员指针
- CPP中的单个参考代表两种或多个数据类型
- 我想添加两个字符串类型的矩阵.用户将不会输入行数或列的数量.用户将仅输入数组
- 是否可以分配两个不同类型的结构
- C++ 重载运算符 - 减去两个相同类型的对象
- C++:从模板中构造具有两个相同类型名称的海峡
- 类中的静态函数给出了错误:声明中的两种或多个数据类型
- 比较C 中的两个数据类型
- 在C++中,当两个向量类型不同时,如何将值存储到向量内部的向量中
- 错误:'x'声明中的两个或多个数据类型
- 如何在GPU上的两个数据容器(最好是CUDA)上执行关系连接
- 在整数数据类型和连续两个字符类型之后.第 2 个字符的数据类型跳过..为什么
- 错误:main的声明中有两个或多个数据类型
- C++函数声明中的两个或多个数据类型的错误
- 按值将类型结构传递给函数,错误:声明"average"中的两个或多个数据类型!!-