C++ 错误"expected an unqualified id before ')' token"(第 1 行)

C++ Error "expected an unqualified id before ')' token" (line 1)

本文关键字:token 错误 an id before C++ expected unqualified      更新时间:2023-10-16

所以我真的是新手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++代码。

相关文章: