我是 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
//命名风暴.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;
}