2014-12-05 36 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

相關問題