对子类的未定义引用
Undefined reference to Sub Class
我有一个子类:PointTwoD,它继承自BaseClass:locationdata。我收到此错误:在我的主函数中未定义对 PointTwoD 的引用,有人可以向我解释为什么吗?
基类位置数据.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;
};
基类位置数据.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;
};
子类点二D.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
}
编译涉及多个源文件(即.cc
或.cpp
文件)的项目时,必须确保在创建可执行文件时涉及每个文件。如何完成这取决于您的编译器,但是使用 g++
,我做到了,并且编译得很好(在main()
的最后一个 cout
语句中添加缺少的;
后):
g++ main.cpp locationdata.cpp PointTwoD.cpp
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中