gpt4 book ai didi

非模板类型的 C++ 显式实例化

转载 作者:行者123 更新时间:2023-11-30 05:13:34 26 4
gpt4 key购买 nike

我正在阅读一本关于 C++ 的书。我仍然对此很满意,但我仍在努力完成细节问题。这部分涉及继承,目标是制作一个形状类,然后在其上构建第一个二维形状,然后在二维形状的基础上构建三维形状。

到目前为止,我已经能够一路向上并完成二维形状。那里的功能工作正常。我现在已经转向三维形状,这就是我遇到的一点问题。

这本书说要实现一个允许长度、高度和三维宽度为整数或 float 的系统,所以我在其中放入了一个模板来实现这一点。这是我认为遇到问题的地方。

我确实将二维类复制并传递到三维类中,并尝试从那里完成它。然后我用它解决了一些问题。

当我尝试编译时,我遇到了两个主要错误。

错误:“ThreeD”不是类模板错误:非模板类型“ThreeD”的显式实例化

我认为是需要提交相关信息的最小一 block 。请记住,main.cpp、shape.h、shape.cpp、twod.h 和 twod.cpp 按原样工作,但我包含了完整的标题而不是完整的其他 cpps。

如果您能提供任何帮助,我们将不胜感激。

我已将 ThreeD.cpp 放在顶部,因为这是错误的来源。

我不认为它是“为什么模板只能在头文件中实现?”的重复。因为代码适用于 TwoD 类,而不适用于 ThreeD 类。

三维.cpp

#include <iostream>
#include <string>
#include "shape.h"
#include "TwoD.h"

using namespace std;

template class ThreeD<int>;
template class ThreeD<float>;


template <class T>
ThreeD<T>::ThreeD (string oobj, int osides, T olength, T oheight, T owidth)
: TwoD<T>(oobj, osides, olength, oheight)
{
setObject(obj);
setSides(sides);
setLength(length);
setHeight(height);
setWidth(owidth);
} //End of TwoD constructor

template <class T>
int ThreeD<T>::getSides() {
return sides;
} //End of function getSides

主要.cpp

#include <iostream>
#include "shape.h"
#include "TwoD.h"
#include "ThreeD.h"

using namespace std;

int main() {


TwoD<float> triangle("Triangle", 3, 3, 3);

ThreeD<float> cube("Cube", 6, 3, 3, 3);
}

形状.h

#ifndef SHAPE_H
#define SHAPE_H

#include <string>

using namespace std;

//Implimenting template <class T> as a means to have the sides
//variable become either a float or an int.
template <class T>
class Shape {

public:
Shape(string, int, T);
// void setObject(string); //Used to ensure constructor works
virtual void setObject(string) = 0;
//Used setObject as virtual function since constructor uses it to
//Make the object using the setObject function.
string getObject();
int getSides();
bool setSides(int);
float getLength();
void setLength (T);

float getPerimeter(string, T);
float getArea (string, T);

void display();
private:
string object;
int sides;
T length;
};

#endif

形状.cpp

#include <iostream>
#include <string>
#include "shape.h"

using namespace std;

//Telling the compiler template class of Shape which versions for T to
//expect either int or float.
template class Shape<int>;
template class Shape<float>;

//Constructor of Shape. Inputs shape, sides, and length.
template <class T>
Shape<T>::Shape (string shapes, int sides, T length){
setObject(shapes);
setSides(sides);
setLength(length);
} //End of Shape constructor

二维.h

#ifndef TWOD_H
#define TWOD_H

#include <string>
#include "shape.h"

using namespace std;

//Implimenting template <class T> as a means to have the sides
//variable become either a float or an int.
template <class T>
class TwoD : public Shape<T>
{

public:
TwoD(std::string, int, T, T);
void setObject(string); //Used to ensure constructor works
// virtual void setObject(string) = 0;
//Used setObject as virtual function since constructor uses it to
//Make the object using the setObject function.
string getObject();
int getSides();
bool setSides(int);
T getLength();
void setLength (T);
T getHeight();
void setHeight (T);

float getPerimeter(string, T, T);
float getArea (string, T, T);

void display();
private:
string object;
int sides;
T length;
T height;
};

#endif

二维.cpp

#include <iostream>
#include <string>
#include "shape.h"
#include "TwoD.h"

using namespace std;

//Telling the compiler template class of TwoD which versions for T to
//expect either int or float.
template class TwoD<int>;
template class TwoD<float>;

//Constructor of TwoD. Inputs TwoD, sides, and length.
template <class T>
TwoD<T>::TwoD (string obj, int sides, T length, T height)
: Shape<T>(obj, sides, length)
{
setObject(obj);
setSides(sides);
setLength(length);
setHeight(height);
} //End of TwoD constructor

三维

#ifndef THREED_H
#define THREED_H

#include <string>
#include "shape.h"
#include "TwoD.h"

using namespace std;

//Implimenting template <class T> as a means to have the sides
//variable become either a float or an int.
template <class T>
class ThreeD : public TwoD<T>
{

public:
ThreeD(std::string, int, T, T, T);
void setObject(std::string); //Used to ensure constructor works
// virtual void setObject(string) = 0;
//Used setObject as virtual function since constructor uses it to
//Make the object using the setObject function.
string getObject();
int getSides();
bool setSides(int);
T getLength();
void setLength (T);
T getHeight();
void setHeight (T);

T getWidth();
void setWidth (T);

float getPerimeter(string, T, T, T);
float getArea (string, T, T, T);

void display();
private:
std::string object;
int sides;
T length;
T height;
T width;
};

#endif

最佳答案

ThreeD.cpp你需要:

#include "ThreeD.h"

没有它,行 template class ThreeD<int>;对编译器没有意义,因为 ThreeD尚未声明。

关于非模板类型的 C++ 显式实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com