C++ 错误"expected an unqualified id before ')' token"(第 1 行)
C++ Error "expected an unqualified id before ')' token" (line 1)
所以我真的是新手C++所以请原谅我的业余代码。我正在尝试制作一个类似于包含继承类和头文件的迷你 SIRI 的代码,但我目前在头文件中收到一个奇怪的错误。
这是我在每个文件中的代码
主.cpp
#include <iostream>
#include <string>
#include "JARVIS.h"
using namespace std;
int main()
{
string command;
bool jarvis_running = false;
cout << "J.A.R.V.I.S" << endl;
cout << "Give a command from the list of commands available." << endl;
cin >> command;
if (command == "-h")
{
jarvis_running = true;
cout << "The available commands are: math" << endl;
}
if (command = "math")
{
jarvis_running = true;
math ma;
}
if (jarvis_running == false)
{
cout << "That command was not valid" << endl;
cout << "Type '-h' if you need to know the available commands." << endl;
}
return 0;
}
*贾维斯函数.cpp
#include <iostream>
#include <string>
#include "JARVIS.h"
using namespace std;
int math()
{
cout << "(A)ddition, (S)ubtraction, (M)ultiplication, or (D)ivision?" << endl;
cin >> mathCommand;
if (mathCommand == "A")
{
cout << "input the two integers you are adding" << endl;
cin >> integer1 >> integer2;
ma.setValues(integer1, integer2);
addition add;
}
else if(mathCommand == "S")
{
cout << "input the two integers you are subtracting" << endl;
cin >> integer1 >> integer2;
ma.setValues(integer1, integer2);
subtraction sub;
}
else if(mathCommand == "M")
{
cout << "input the two integers you are multiplying" << endl;
cin >> integer1 >> integer2;
ma.setValue(integer1, integer2);
mulitplication multi;
}
else if(mathCommand == "D")
{
cout << "input the two integers you are dividing" << endl;
cin >> integer1 >> integer2;
ma.setValues(integer1, integer2);
divions div;
}
else
{
cout << "you did not input the right fuctions, either use A, S, M, or D" << endl;
}
}
贾维斯·
#ifndef JARVIS_H
#define JARVIS_H
class math
{
private:
int val1;
int val2;
public:
math()
{
cout<<"calling math constructor"<<endl;
}
void setValues (int a, int b){
int a = val1;
int b = val2;
}
~math()
{
cout<<"calling math deconstructor"<<endl;
}
}
class addition:public math
{
int finalVal = val1 + val2;
return finalVal;
};
class subtraction:public math
{
int finalVal = val1 - val2;
return finalVal;
};
class multiplication:public math
{
int finalVal = val1 * val2;
return finalVal;
};
class division:public math
{
int finalVal = val1 / val2;
return finalVal;
};
#endif //JARVIS_H
我很确定这个类以及我如何称呼它们仍然有很多问题,但现在我不确定是什么导致了这个错误。
您忘记在类数学定义的右大括号之后放置分号。
还有这样的定义
class addition:public math
{
int finalVal = val1 + val2;
return finalVal;
};
在C++中无效。类不是函数,其定义可能不包含 return 语句。
此外,在块作用域中定义一个本地对象也没有任何意义,该对象未像 if 语句中那样使用
if (command == "math")
{
jarvis_running = true;
math ma;
}
对象 ma 将不存在于 if 的复合语句之外。
或者在哪里定义了语句中使用的数学命令
cin >> mathCommand;
或者代替
void setValues (int a, int b){
int a = val1;
int b = val2;
}
我想你的意思是
void setValues (int a, int b){
val1 = a;
val2 = b;
}
所以你的程序是完全错误的。它有很多错误,所以讨论你的代码是没有意义的。首先,您应该编写或多或少有效的C++代码。
相关文章:
- 错误:创建模板类时"expected unqualified-id before '{' token"
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 这个编译器错误是什么意思 - "qualified-id in declaration before ‘=’ token" C++?
- C 预期的类名称{token,一个继承错误
- 奇怪的错误:标签'loopend'使用但未定义," if "之前应为非限定 id("goto"和" '}' token "错误相同
- 在2D伊辛模型上工作。C++新手。我在第 23 行收到错误"expected unqualified-id before '{' token"
- 如何修复"Expected Primary-expression before ')' token"错误?
- C++错误"tabulate.cc:19: error: expected primary-expression before ',' token"
- C++嵌套继承错误"expected class-name before ‘{’ token"
- 矩阵类实验中的编译错误 - "expected initializer before ‘.’ token"
- 尝试编译时出现错误"expected unqualified-id before ‘{’ token {"
- 调试前C++错误"expected unqualified-id '(' token"
- C++ 错误"expected an unqualified id before ')' token"(第 1 行)
- GCC 错误"expected ')' before '[' token"
- 模板类编译"expected constructor, destructor, or type conversion before ‘<’ token"错误
- "expected unqualified-id before ‘.’ token"错误 - C++
- 为什么我总是收到错误消息"no member named "令牌类型 " in 'Token"?
- 获取错误"array bound is not an integer constant before ']' token"
- 编译错误:solution.c:20:5: Error:期望的标识符或'('在' {' token{之前
- C++ "expected primary-expression before ‘(’ token"错误