0

私は C++ を初めて使用し、古い Java コードを C++ に変換する練習をしています。非常に多くのエラーに遭遇したため、ほとんど希望をあきらめました。また、メイン ファイルのエラーを修正しようとしています。メイン ファイルの関数を呼び出そうとしていますが、クレイジーな構文エラーが発生し、何が問題なのかわかりません。main.cpp でこれらのエラーを修正する方法について、何週間もグーグルで検索してみました。できれば助けていただければ幸いです。

// NamedStorm.cpp 
// CPP=> Function definition
#include <iostream>
#include <string>

#include "NamedStorm.h"
using namespace std;

// Makes sure that the displayOutput method work properly
std::ostream& operator<<(ostream& out, const NamedStorm& namedStorm);

// Default Constructor definition (removed parameter due to issues)
NamedStorm::NamedStorm(){

}

// Overload construtor definition
NamedStorm::NamedStorm(string sName, double wSpeed, string sCat,double sPress){
    stormName = sName;
    stormCategory = sCat;
    stormPressure = sPress;
    stormCount++;
}
// Destructor definition
NamedStorm::~NamedStorm(){}

// Accessor function definition
void NamedStorm::displayOutput(NamedStorm storm[]){
    for(int i = 0; i < sizeof(storm); i++){
        cout << storm[i] << "\n";
    }
}

void NamedStorm::sortByNames(NamedStorm storm[]){
    cout << "Sorting array in decsending alphabetical order by names..." << endl;
    for(int k = 1; k < 4; k++){
        for(int i = 0; i < 4 - k; i++){
            if((storm[i].getName()).compare(storm[i+1].getName()) > 0){
                NamedStorm temp;
                temp = storm[i];
                storm[i] = storm[i+1];
                storm[i+1] = temp;
            }
        }
    }
}

void NamedStorm::getAverageWindSpeed(NamedStorm storm[]){
    double averageWSpeed = 0.0;
    double totalWindSpeed = 0.0;

    totalWindSpeed = storm[0].maxWindSpeed
        + storm[1].maxWindSpeed + storm[2].maxWindSpeed + storm[3].maxWindSpeed
        + storm[4].maxWindSpeed;

    averageWSpeed = totalWindSpeed / sizeof(storm);
    cout << "The average max wind speeds of storms: " << averageWSpeed << "mph"<< endl;
}

void NamedStorm::getAverageStormPressure(NamedStorm storm[]){
    double averageSPress = 0.0;
    double totalStormPressure = 0.0;

    totalStormPressure = storm[0].getStormPressure()
        + storm[1].getStormPressure() + storm[2].getStormPressure() + storm[3].getStormPressure()
        + storm[4].getStormPressure();

    averageSPress = totalStormPressure / 5;
    cout << "The Average storm pressure: " << averageSPress << " mb" << endl;
}

int NamedStorm::getStormCount(){
    return stormCount;
}

double NamedStorm::getStormPressure(){
    return stormPressure;
}

double NamedStorm::getWindSpeed(){
    return maxWindSpeed;
}

string NamedStorm::getStormCategory(){
    return stormCategory;
}

string NamedStorm::getName(){
    return stormName;
}


// Mutator function definition

//NamedStorm.h

// Header => Function Declaration
#include <iostream>
#include <string>

using namespace std;

#ifndef NAMEDSTORM_H
#define NAMEDSTORM_H

class NamedStorm{
public:
    // Default constructor declaration
    NamedStorm();

    // Overloaded constructor declaration
    NamedStorm(string, double, string, double);

    // Destructor declaration
    ~NamedStorm();

    // Accessor (GET methods in Java) functions declarations (will return variables), use const, when not changing member variables
    static void displayOutput(NamedStorm storm[]);
    static void sortByNames(NamedStorm storm[]);
    static void sortByWindSpeed(NamedStorm storm[]);
    static void getAverageWindSpeed(NamedStorm storm[]);
    static void getAverageStormPressure(NamedStorm storm[]);
    int getStormCount();
    double getStormPressure();
    double getWindSpeed();
    string getStormCategory();
    string getName();

    // Mutator functions (SET methods in Javinese)
    void setStormName();
    void setStormCategory();
    void setMaxWindSpeed();
    void setStormPressure();

private:
    string stormName;
    string stormCategory;
    double maxWindSpeed;
    double stormPressure;
    static int stormCount;
};

// Main.cpp

#include <iostream>
#include <string>

#include "NamedStorm.h"

using namespace std;

NamedStorm storm[5];

int main(){
    NamedStorm Chris("Chris", 70, "Tropical Storm", 990);
    NamedStorm Alberto("Alberto", 45, "Tropical Storm", 1007);
    NamedStorm Gordon("Gordon", 65, "Tropical Storm", 999);
    NamedStorm Isaac("Isaac", 80, "1", 969);
    NamedStorm Ernesto("Ernesto", 50, "Tropical Storm", 1006);

    storm[0] = Alberto;
    storm[1] = Chris;
    storm[2] = Ernesto;
    storm[3] = Gordon;
    storm[4] = Isaac;

    // Error: identifier not found
    displayOutput();
    sortByNames();
    displayOutput();
    sortByWindSpeed();
    displayOutput();
    getAverageStormPressure();
    getAverageWindSpeed();
    return 0;
}
4

1 に答える 1

0

それを小さなチャンクに分割し (すべてをコメントアウトする)、一度に 1 つずつアイテムを追加し直すことをお勧めします。

「識別子が見つかりません」では、静的クラス メソッドを呼び出そうとしているように見えますが、クラスの名前がプレフィックスとして付けられていません。私は代わりに考えます:

displayOutput();

main() では、次のことが必要です。

NamedStorm::displayOutput();

メソッドがストーム配列を取り、パラメーターが提供されていないためです。したがって、正しい署名は次のようになると思います。

NamedStorm stormArray[2];
NamedStorm::displayOutput( stormArray );

グローバルな「displayOutput()」を呼び出すことが意図されていた場合、定義されたものが表示されないため、その場合のエラーメッセージを説明します。

パート II - ostream オペレーター =========================================== =================

うーん...あなたは本当にトップから始めています!OK、どちらも機能しませんでした-テンプレートを正しく定義する方法などに関するこれらすべてのリンクを見ました...しかし、問題はエラーメッセージとまったく同じくらい単純です。存在しないため、参照は未解決です! 新しい演算子を定義しましたが、実装を提供しませんでした。必要なのは、次のように実装を提供することだけです。

// this declares the operator, but it's still undefined
std::ostream& operator<<(std::ostream& out, const NamedStorm& namedStorm);

// add this to *define* it - otherwise it has no body so the linker looks in all the
// obj files, can't find it and gives the unresolved reference error
std::ostream& operator<<(std::ostream& out, const NamedStorm& namedStorm)
{
//print("we needed an implementation!");
return out;
}

実際には、宣言の直後に実装を行う必要はありませんが、通常、宣言は .h ファイルに入れるか、宣言をまったく行わず、上記の実装を .CPP ファイルに含めることができます。しかし、他のソースファイルはそれについて知りません。

プロジェクトに次の要素を追加することは、一般的に不適切な方法と考えられています。

using namespace std;

代わりに、必要な名前空間をより明示してください。たとえば、次のようになります。

std::string myString = "we know which 'string' this is";

バグを追跡するのに半日を費やして、適切なクラスを見ていないことに気付くまでは、「すべてを使用して...」と言う方が簡単に思えます!

ふう..私は今休みます;-)

于 2013-06-19T21:45:41.100 に答える