2013-10-05 39 views
1

我有一個繼承自BaseClass:locationdata的子類:PointTwoD。我得到這個錯誤:未定義在我的主要功能PointTwoD引用,有人可以向我解釋爲什麼?對子類的未定義引用

基類locationdata.h

#include <string> 
#include <iostream> 

using namespace std; 

class locationdata 
{ 
    public: 
    locationdata(); //default constructor 
    locationdata(string,int,int,float,float); //constructor 

//setter 
void set_sunType(string); 
void set_noOfEarthLikePlanets(int); 
void set_noOfEarthLikeMoons(int); 
void set_aveParticulateDensity(float); 
void set_avePlasmaDensity(float); 

//getter 
string get_sunType(); 
int get_noOfEarthLikePlanets(); 
int get_noOfEarthLikeMoons(); 
float get_aveParticulateDensity(); 
float get_avePlasmaDensity(); 


float computeCivIndex(); 
friend class PointTwoD; 

     private: 

    string sunType; 
    int noOfEarthLikePlanets; 
    int noOfEarthLikeMoons; 
    float aveParticulateDensity; 
    float avePlasmaDensity; 

}; 

基類locationdata.cpp

#include <iostream> 
#include "locationdata.h" 
using namespace std; 

locationdata::locationdata() 
{ 
    this->sunType = ""; 
    this->noOfEarthLikePlanets=0; 
    this->noOfEarthLikeMoons=0; 
    this->aveParticulateDensity=0; 
    this->avePlasmaDensity=0; 

} 

locationdata::locationdata(string sunType , int noOfEarthLikePlanets , 
         int noOfEarthLikeMoons , float aveParticulateDensity , 
         float avePlasmaDensity) 

{ 
    this->sunType = sunType; 
this->noOfEarthLikePlanets = noOfEarthLikePlanets; 
this->noOfEarthLikeMoons = noOfEarthLikeMoons; 
this->aveParticulateDensity = aveParticulateDensity; 
this->avePlasmaDensity = avePlasmaDensity; 

} 

void locationdata::set_sunType(string sunType) 
{ 
    this->sunType = sunType; 

} 

void locationdata::set_noOfEarthLikePlanets(int noOfEarthLikePlanets) 
{ 

this->noOfEarthLikePlanets = noOfEarthLikePlanets; 
} 

void locationdata::set_noOfEarthLikeMoons(int noOfEarthLikeMoons) 
{ 
this->noOfEarthLikeMoons = noOfEarthLikeMoons; 
} 

void locationdata:: set_aveParticulateDensity(float aveParticulateDensity) 
{ 
this->aveParticulateDensity = aveParticulateDensity; 

} 

void locationdata::set_avePlasmaDensity(float avePlasmaDensity) 
{ 
this->avePlasmaDensity = avePlasmaDensity; 
} 


string locationdata::get_sunType() 
{ 
return this->sunType; 
} 

int locationdata::get_noOfEarthLikePlanets() 
{ 
return this->noOfEarthLikePlanets; 
} 

int locationdata::get_noOfEarthLikeMoons() 
{ 
return this->noOfEarthLikeMoons; 
} 

float locationdata::get_aveParticulateDensity() 
{ 
return this->aveParticulateDensity; 
} 

float locationdata::get_avePlasmaDensity() 
{ 
return this->avePlasmaDensity; 

} 

float locationdata::computeCivIndex() 
{ 
string temp = this->get_sunType(); 
int sunTypePercent; 
float CivIndex ; 

if (temp == "Type O") 
{ 
sunTypePercent = 30; 
} 
else if (temp == "Type B") 
{ 
sunTypePercent = 45; 

} 
else if (temp == "Type A") 
{ 
sunTypePercent = 60; 

} 
else if (temp == "Type F") 
{ 
sunTypePercent = 75; 
} 
else if (temp =="Type G") 
{ 
sunTypePercent = 90; 
} 
else if (temp =="Type K") 
{ 
sunTypePercent = 80; 
} 
else if (temp =="Type M") 
{ 
sunTypePercent = 70; 
} 




      CivIndex=1.5; 

      return CivIndex; 
} 

子類PointTwoD.h

#include <iostream> 
#include "locationdata.h" 

using namespace std; 

class PointTwoD:public locationdata 
{ 
    public: 
    PointTwoD(); 

    private: 
    int x; 
    int y; 
    float civIndex; 




}; 

子類PointTwoD.cpp

#include "PointTwoD.h" 

PointTwoD::PointTwoD() 
{ 
    this ->x = 0; 
    this->y = 0; 

    this->set_sunType(""); 
    this->set_noOfEarthLikePlanets(0); 
    this->set_noOfEarthLikeMoons(0); 
    this->set_aveParticulateDensity(0); 
    this->set_avePlasmaDensity(0); 

} 

主要功能

#include <iostream> 
#include "PointTwoD.h" 

using namespace std; 

int main() 
{ 
int choice; 
PointTwoD test; //undefined reference 
test.set_noOfEarthLikeMoons(10); // undefined reference 
cout<<test.get_noOfEarthLikeMoons() //undefined reference 
} 
+0

我想你是不是包括所有編譯時的文件。你如何編譯你的項目? – jxh

+0

@jxh我正在編譯使用quincy 2005,我只是把所有文件放在同一個文件夾中 – Computernerd

+0

是的,我不知道如何幫助你。您必須閱讀有關如何將多個源文件添加到項目的文檔。 – jxh

回答

1

當您編譯包含多個源文件(即,.cc.cpp文件)的一個項目,你必須確保當你想創建可執行的每個文件涉及。那如何來完成取決於你的編譯器,但與g++,我這樣做,它編譯罰款(你main()添加缺少的;最後cout語句後):

g++ main.cpp locationdata.cpp PointTwoD.cpp