我是C++的新手,我一直在練習將舊的Java代碼翻譯成C++。我遇到了很多錯誤,幾乎放棄了希望。我也試圖修復主文件中的錯誤,我試圖在主文件中調用一個函數,但是我得到的語法錯誤像瘋了一樣,我不知道什麼是錯的。我試着用Google搜索並搜索幾周,以瞭解如何在main.cpp中修復這些錯誤。如果可以的話,我很感激你的幫助。錯誤LNK2001:無法解析的外部符號?我絕對失去了
// 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;
}
確定LNK2001錯誤不再出現,現在它只是在main.cpp中調用函數的「標識符未找到」錯誤 –