Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 纯虚函数_C++_Linker - Fatal编程技术网

C++ 纯虚函数

C++ 纯虚函数,c++,linker,C++,Linker,Hi Stack Over Flow用户 需要一些纯虚拟函数的帮助。我已经在谷歌上搜索了我得到的错误:“未定义对‘vtable for Sphere’的引用”,并阅读了stack overflow的其他帖子。我收集的是链接器不知道函数体在哪里 完整错误消息:/tmp/ccXEIJAQ.o:main.cpp(.rdata$.refptr.\u ZTV6Sphere[.refptr.\u ZTV6Sphere]+0x0):未定义对“vtable for Sphere”的引用 collect2:错误:

Hi Stack Over Flow用户

需要一些纯虚拟函数的帮助。我已经在谷歌上搜索了我得到的错误:“未定义对‘vtable for Sphere’的引用”,并阅读了stack overflow的其他帖子。我收集的是链接器不知道函数体在哪里

完整错误消息:/tmp/ccXEIJAQ.o:main.cpp(.rdata$.refptr.\u ZTV6Sphere[.refptr.\u ZTV6Sphere]+0x0):未定义对“vtable for Sphere”的引用 collect2:错误:ld返回了1个退出状态

在粘贴以下相关代码部分之前:

在Windows7 Professional下的Cygwin 64位环境中编译

抽象基类:原语 子类:球体

下面是文件Primitive.h中的内容

#include "Color.h"
#include "Vector3D.h"

#ifndef PRIMITIVE_H
#define PRIMITIVE_H

class Primitive
{
public:
    Primitive();
    virtual ~Primitive();
    virtual bool intersection(double &, const Vector3D &) = 0;    // pure virutal function

// other functions and private variables omitted for clarity.

};

#include "Primitive.cpp"

#endif
下面是Sphere.h

#include "Primitive.h"
#include "Point3D.h"
#include <iostream>
#include "Vector3D.h"

using namespace std; 

#ifndef SPHERE_H
#define SPHERE_H

class Sphere : public Primitive
{
public:
    Sphere(const Point3D &, double, const Color &, double, const Color &, double, double);
    Sphere(const Point3D &, double, const Color &, double, const Color &, double, double, const Color &, double, double);
    bool intersection(double &, const Vector3D &);
    Point3D getCenter();
    double getRadius();
    ~Sphere();

    friend ostream & operator << (ostream &, const Sphere &);

protected:
    Point3D *center;
    double radius;
};

#include "Sphere.cpp"

#endif
#包括“Primitive.h”
#包括“Point3D.h”
#包括
#包括“Vector3D.h”
使用名称空间std;
#ifndef球面
#定义球面
类球体:公共原语
{
公众:
球体(常量点3D&,双精度,常量颜色&,双精度,常量颜色&,双精度,双精度);
球体(常量点3D&,双精度,常量颜色&,双精度,常量颜色&,双精度,双精度,常量颜色&,双精度,双精度);
布尔交叉点(双和,常量向量3D和);
Point3D getCenter();
双getRadius();
~Sphere();

friend ostream&operator问题是,在
Sphere.cpp
中,您正在定义另一个名为
intersection
的函数。您需要做的是实现类中声明的函数,如下所示:

bool Sphere::intersection(double &intersect, const Vector3D &ray)
而不是

bool intersection(double &intersect, const Vector3D &ray)

问题是,在
Sphere.cpp
中,您正在定义另一个名为
intersection
的函数。您需要做的是实现类中声明的函数,如下所示:

bool Sphere::intersection(double &intersect, const Vector3D &ray)
而不是

bool intersection(double &intersect, const Vector3D &ray)

您忘记在函数定义中包含类名:

bool Sphere::intersection(double &intersect, const Vector3D &ray)
{
    //just trying to get it to compile.

    cout << "Hello.\n";
    return true;
}
bool Sphere::intersection(双相交和相交,常量向量3D和光线)
{
//只是想把它编译一下。

cout您忘记在函数定义中包含类名:

bool Sphere::intersection(double &intersect, const Vector3D &ray)
{
    //just trying to get it to compile.

    cout << "Hello.\n";
    return true;
}
bool Sphere::intersection(双相交和相交,常量向量3D和光线)
{
//只是想把它编译一下。


难道什么是
#包括“Primitive.cpp”
?@RichardCritten,不,错了duplicate@SergeyA:Primitive.cpp包含在基类中定义的需要实体的其他方法的定义。例如,Color Primitive::getDiffuseColor(){return difColor;}@Borgleader:出于某种原因,如果我在cygwin环境的.h文件中没有包含.cpp文件,程序将不会编译。如果我使用XCode,我必须去掉这一行。你不应该包含cpp文件。编译使用该类的文件所需的只是包含头。也许你在y中缺少了一些东西我们的头文件。不应该包括cpp文件;它应该像任何其他源代码文件一样进行编译和链接。到底是什么
#包括“Primitive.cpp”
?@RichardCritten,不,错了duplicate@SergeyA:Primitive.cpp包含在基类中定义的需要实体的其他方法的定义。例如,Color Primitive::getDiffuseColor(){return difColor;}@Borgleader:出于某种原因,如果我在cygwin环境的.h文件中没有包含.cpp文件,程序将不会编译。如果我使用XCode,我必须去掉这一行。你不应该包含cpp文件。编译使用该类的文件所需的只是包含头。也许你在y中缺少了一些东西我们的头文件。cpp文件不应该包括在内;它应该像任何其他源代码文件一样进行编译和链接。天哪……我不敢相信我忘了写那个。我整天都在盯着这个,想知道到底出了什么问题。谢谢你那双新鲜的眼睛。@Yousaf在过去无数次发生在我身上。你从你的错误。@Yousaf还有,如果你在选择答案时遇到困难,我是第一个:)我有时会自下而上地阅读文章。所以我先看到了另一篇文章,然后看到了你的。我对这两篇文章都投了赞成票。它们都是对的。@Yousaf谢谢你!天哪……我真不敢相信我忘了写那篇文章。我整天都在盯着这篇文章想到底是怎么回事。谢谢你那双新鲜的眼睛。@Yousaf在过去无数次发生在我身上。你从错误中吸取教训。@Yousaf而且,如果你在选择答案时遇到困难,我是第一个:)我有时会自下而上地阅读文章。所以我先看到了另一篇文章,然后看到了你的。我对这两篇文章都投了赞成票。他们都是对的。@Yousaf谢谢!谢谢。一双新鲜的眼睛会创造奇迹。谢谢。一双新鲜的眼睛会创造奇迹。