2016-04-07 6 views
-3

このエラーを受け取り続けるには、このコードで何が問題になりますか?cout/cinは型エラーの名前を付けません

エラーは、mainに「distanceFormula」を入れるのではなく、私はそれを自分のクラスにしました。

#include <iostream> 
#include <string> 

using namespace std; 

class distanceFormula { 
    public: 
int speed; 
int time; 
int distance; 

cout << "What is the speed?" << endl; 
cin >> speed; 

cout << "How long did the action last?" << endl; 
cin >> time; 


distance = speed * time; 

cout << "The distance traveled was " << distance << endl; 
}; 




int main() 
{ 
distanceFormula ao 
ao.distanceFormula; 

return 0; 
}; 
+0

なぜあなたは独自のクラスを作ったのですか?あなたはあなたのコードをコンパイルしましたか? –

+1

関数とクラスを区別します。 –

+1

'ao.distanceFormula;'それは何をするはずですか? – drescherjm

答えて

0

クラス宣言の本体は、データ又は機能宣言、及び任意アクセス指定子のいずれかであることができるメンバーを含有することができます。

関数内のコードをラップして、あなたはまだクラスを使用する必要がある場合、オブジェクト

class distanceFormula { 
public: 
int speed; 
int time; 
int distance; 
void init() 
{ 
    cout << "What is the speed?" << endl; 
    cin >> speed; 

    cout << "How long did the action last?" << endl; 
    cin >> time; 
    distance = speed * time; 
    cout << "The distance traveled was " << distance << endl; 
} 
}; 

int main() 
{ 
    distanceFormula ao; 
    ao.init(); 
    return 0; 
}; 
+0

ありがとう! XD –

+0

@AmmarTという非批判的な唯一の人物です。私は答えを更新し、理由も追加しました! –

0

によってmainのそれを呼び出します。あなたのやり方は次のとおりです。

#include <iostream> 

class distanceFormula 
{ 
private: 
    int speed; // private 
    int time; // private 
public: 
    distanceFormula(); // constructor 
    int getSpeed(); // to get 
    int getTime(); // to get 
    int getDistance(); // to get 
    void setSpeed(int); // to set 
    void setTime(int); // to set 
}; 

distanceFormula::distanceFormula() 
{ 
    this->time = 0; 
    this->speed = 0; 
} 

int distanceFormula::getSpeed() 
{ 
    return this->speed; 
} 
int distanceFormula::getTime() 
{ 
    return this->time; 
} 
int distanceFormula::getDistance() 
{ 
    return this->time * this->speed; 
} 
void distanceFormula::setSpeed(int speedVal) 
{ 
    this->speed = speedVal; 
} 
void distanceFormula::setTime(int timeVal) 
{ 
    this->time = timeVal; 
} 


int main() 
{ 
    distanceFormula YourObject; // create obj 
    int SpeedValue; 
    int TimeValue; 
    std::cout << "Enter the Speed:"; 
    std::cin >> SpeedValue; // take speed 

    std::cout << "Enter the Time:"; 
    std::cin >> TimeValue; // take time 


    YourObject.setSpeed(SpeedValue); // set 
    YourObject.setTime(TimeValue); // set 

    std::cout << "This is the distance: " << YourObject.getDistance(); // retrieve result 

    getchar(); // wait 
    return 0; 
} 
関連する問題