【问题标题】:Undefined reference to Sub Class对子类的未定义引用
【发布时间】:2013-10-05 05:40:23
【问题描述】:

我有一个 SubClass : PointTwoD 继承自 BaseClass : locationdata 。我收到此错误:在我的主函数中未定义对 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
}

【问题讨论】:

  • 我认为您在编译时没有包括所有文件。你是如何编译你的项目的?
  • @jxh 我正在使用 quincy 2005 进行编译,我只是将所有文件放在同一个文件夹中
  • 是的,我不知道如何为您提供帮助。您必须阅读有关如何将多个源文件添加到项目中的文档。
  • 感谢您的帮助

标签: c++ class debugging inheritance


【解决方案1】:

当您编译涉及多个源文件(即.cc.cpp 文件)的项目时,您必须确保在创建可执行文件时涉及每个文件。具体如何实现取决于您的编译器,但使用g++,我这样做了,并且编译得很好(在main() 中的最后一个cout 语句中添加缺少的; 之后):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2021-03-13
    • 2012-09-01
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多