2014-12-05 66 views
-5

所以我对C++非常陌生,所以原谅我的业余代码。我试图做一个代码,像一个迷你SIRI,其中包含继承类和头文件,但我目前在我的头文件中出现一个奇怪的错误。C++错误“期望不符合标识之前')'标记”(第1行)

这是我在每个文件

的main.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; 
} 

* JarvisFunctions.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; 
     } 
} 

JARVIS.h

#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 

我敢代码肯定还有很多错误与类和我怎么称呼他们,但现在我不知道是什么导致这个错误。

+3

class'math'在关闭'}'后缺少结尾分号。 “数学”的衍生物没有一个是正确的。你把他们当作功能对待;不是课程。 – WhozCraig 2014-12-05 11:00:45

+0

这将有助于完整的编译器日志和用于编译程序的命令,它看起来像某处的语法错误。 – gauteh 2014-12-05 11:00:54

+0

class'addition'也不正确。 – 2014-12-05 11:01:24

回答

1

你忘了在类数学定义的大括号后放置一个分号。

另外定义这样

class addition:public math 
{ 
    int finalVal = val1 + val2; 
    return finalVal; 
}; 

无效在C++中。一个类不是一个函数,它的定义可能不包含return语句。

而且没有任何意义,以限定在未在此用作if语句

if (command == "math") 
    { 
    jarvis_running = true; 
    math ma; 
    } 

对象毫安不会在如果的复合语句外部存在的块范围的局部对象。

或者其中mathCommand定义,在声明中

使用
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++代码。

+1

而且没有叫'divions'的类,但它在'main()'中使用,'math :: setValues'将不会编译,因为它在与参数相同的范围内重新声明了'a'和'b'并且如同轻量级指出的那样,即使它编译了它仍然是错误的,因为赋值方向是错误的,所以使用非初始化内容从'val1'和'val2'。 – WhozCraig 2014-12-05 11:15:46

+0

好的,我修复了分号。当我编译代码时,我似乎仍然得到同样的错误。是的,我应该意识到这个类不会这样工作。仍然会在类中添加一个函数来执行简单的附加操作吗?我不能通过if语句运行函数吗?我现在定义了mathCommand。感谢您的帮助,我知道它充斥着错误,我想调试但是这个当前的错误似乎阻止了其他错误的出现,它说只有1个错误当我知道还有更多的方法。 – GunR 2014-12-05 11:32:41

+0

我也颠倒了a和b并修正了班级分数,拼错了。 – GunR 2014-12-05 11:37:34

相关问题