2013-06-19 2 views
0

내가 ++ C에 새로운 오전 잃었어요 그리고 난 C++에 내 옛날 자바 코드를 변환하여 실행하고있다. 나는 많은 오류를 접하면서 거의 포기하지 않았다. 또한 주 파일에서 오류를 수정하려고하는데 주 파일에서 함수를 호출하려고하는데 미친 듯이 구문 오류가 발생하고 잘못된 점을 알지 못합니다. 나는 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; 
} 
+0

ok LNK2001 오류가 더 이상 나타나지 않고 main.cpp의 호출 기능에 "식별자를 찾을 수 없음"오류가 표시됩니다. –

답변

0

나는 (모든 주석) 및 다시 항목을 추가 작은 조각들로 그것을 깨는 제안 할 수 한 번에 하나씩 - 당신이가는대로 각각을 이해하도록하십시오. 은 "발견되지 식별자"에

, 그것은 당신이 정적 클래스 메소드를 호출 위해 노력하고 있지만,이 클래스의 이름 접두사가없는 것처럼 보인다. 주에서

displayOutput(); 

(), 당신이 원하는 :

NamedStorm::displayOutput(); 

을하지만 방법은 폭풍 배열을하고 매개 변수가 제공되고 있지 않기 때문에 심지어는 매우 옳지 않아 내가 대신 생각합니다. 그래서 올바른 서명이있을 것이라고 생각 : 의도가 글로벌 전화 인 경우

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

"displayOutput()"를, 내가 하나 그래서 그 경우 오류 메시지를 설명 할 정의 표시되지 않습니다.

PART II - ostream에 오퍼레이터 ========================================= =====================

윽 ... 정말 상단에서 시작된다! 그래, 나도 일할 수 없었어 - 템플릿을 올바르게 정의하는 방법에 대한 모든 링크를 살펴 봤지만 ... 문제는 오류 메시지만큼이나 간단합니다. 참조가 존재하지 않기 때문에 참조가 해결되지 않았습니다! 새 연산자를 정의했지만 구현을 제공하지 않았습니다. 즉시 구현 다음 선언을 필요로하지 않지만, 일반적으로 선언이 .H 파일에 갈 것 또는이 것 실제로

// 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; 
} 

지금 : 당신이 필요로하는, 구현을 제공과 같이하는 것입니다 선언이 전혀 없으면 위의 구현을 .CPP 파일에 넣을 수 있습니다. 그러나 다른 소스 파일은 그것에 대해 알 수 없습니다.

using namespace std; 
대신

, 예를 들어, 당신이 원하는 namespace에있는 더 명시 :

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

그냥 사용 "말을 쉽게 보인다

그것은 일반적으로 고추에 가난한 연습와 프로젝트 간주 모든 것 ... "반나절 만 추적 할 때까지는 벌레를 추적하기 만하면 올바른 수업을 보지 못한다는 것을 깨닫게됩니다!

휴 ..이제 쉬어 요 ;-)

+0

제안 사항이 적용되었습니다! 그러나 LNK2001 오류가 다시 나타납니다 ... –

+0

오류 LNK2001 : 해결되지 않은 외부 기호 "개인 : 정적 int NamedStorm :: stormCount"(? stormCount @ NamedStorm @@ 0HA). 아마 개인 변수일까요? –

+0

"stormCount"는 비공개입니다. 클래스 내부에서 참조하기 때문에 괜찮습니다. 여기서 문제는 정적이라는 것입니다 만, 인스턴스 메소드 인 생성자에서 참조하려고합니다. 생성자 안에 "NamedStorm :: stormCount ++"라고 쓰면 잘 작동합니다. (소멸자에서 그것을 감소시킬 수도 있습니다 - 몰몬교는 당신의 필요에 달려 있습니다). 다행이 당신을 위해 일하고 있습니다. –