c++ - 解决“对[自定义类]的 undefined reference ”的问题

标签 c++ header-files undefined-reference

我已经为一个错误苦苦挣扎了大约一天,但仍然没有找到解决我的错误的方法,特别是 undefined reference 错误:

undefined reference `Lines2D::Point2D::~Point2D()' engine.cc/Graphicsengine line 20 C/C++ Problem

我调用的“Lines2D.h”类的每个对象都会出现此错误,所以我可能在那里做错了什么。

相关代码如下:

“EasyImage.h”、“lparser.h”和“ini_configuration.hh”是我的导师提供的文件,你可以假设这些文件是正确编写的(也没有与这些相关的错误)。

#include "EasyImage.h"
#include "lparser.h"
#include "ini_configuration.hh"
#include "Lines2D.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <cmath>
#include <set>

img::EasyImage LSystem2D(unsigned int size, ini::DoubleTuple backgroundcolor, LParser::LSystem2D System, ini::DoubleTuple color)
{
img::EasyImage image(size, size);
std::string replacementstring;
std::string string = System.get_initiator();
std::set<char> const alphabet = System.get_alphabet();
Lines2D::Lines2D Lines;
Lines2D::Point2D currentpos(0,0); //Undefined reference error here
Lines2D::Point2D newpos(0,0);     //Undefined reference error here
img::Color linecolor(color.at(0)*255,color.at(1)*255,color.at(2)*255);
double angle = System.get_angle();
double currentangle = System.get_starting_angle();
for(unsigned int j = 0; j != System.get_nr_iterations(); j++)
{
    for(char& c : string)
    {
        if(alphabet.count(c) != 0)
        {
            replacementstring.append(System.get_replacement(c));
        }
        else
        {
            replacementstring += c;
        }
    }
    string = replacementstring;
    replacementstring.clear();
    std::cout << string << std::endl;
}
for(char& c : string)
{
    if(alphabet.count(c) != 0)
    {
        newpos.X = currentpos.X+cos(currentangle);
        newpos.Y = currentpos.Y+(1/sin(currentangle));
        if(System.draw(c))
        {
            Lines.push_back(Lines2D::Line2D(currentpos,newpos,linecolor)); //Undefined reference error here
            currentpos = newpos;
        }
        else
        {
            currentpos = newpos;
        }

    }
    else if(c=='-')
    {
        currentangle -= angle;
    }
    else if(c=='+')
    {
        currentangle += angle;
    }
    if(currentangle > 360){currentangle -= 360;}
    if(currentangle < -360){currentangle += 360;}
}
Lines2D::Drawlines2D(Lines, size); //Undefined reference error here
return image;
}

"Lines2D.h"

#ifndef LINES2D_H_
#define LINES2D_H_

#include "EasyImage.h"
#include "ini_configuration.hh"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <list>

using namespace std;

namespace Lines2D
{
    class Point2D
    {
    public:
        double X;
        double Y;
        Point2D();
        Point2D(double x, double y);
        ~Point2D();
    };
    class Line2D
    {
    public:
        Point2D p1;
        Point2D p2;
        img::Color color;
        Line2D();
        Line2D(Point2D &P1, Point2D &P2, img::Color &c);
        ~Line2D();
    };
    typedef list<Line2D> Lines2D;
    inline int roundtoint(double d);
    void Drawlines2D(Lines2D &lines, int size);
}

#endif /* LINES2D_H_ */

"Lines2D.cc"

#include "Lines2D.h"
#include "EasyImage.h"
#include "ini_configuration.hh"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <list>

using namespace std;

namespace
{
    class Point2D
    {
        public:
        double X;
        double Y;
        Point2D() :
            X(0),Y(0)
        {
        }
        Point2D(double &x, double &y) :
            X(x), Y(y)
        {
        }
        ~Point2D()
        {
        }
    };

    class Line2D{
    public:
        Point2D p1;
        Point2D p2;
        img::Color color;
        Line2D() :
            p1(), p2(), color()
        {
        }
        Line2D(Point2D &P1, Point2D &P2, img::Color &c) :
            p1(P1.X, P1.Y), p2(P2.X, P2.Y), color(c.red, c.green, c.blue)
        {
        }
        ~Line2D()
        {
        }
    };

    typedef list<Line2D> Lines2D;

    inline int roundtoint(double d)
    {
        return d < 0?
        std::ceil(d-0.5):
        std::floor(d+0.5);
}

    void Drawlines2D(Lines2D &lines, int size)
    {
        img::EasyImage image(size, size);
        double imgmaxX = lines.begin()->p1.X; //min/max are initialized with a value from the list in order
        double imgmaxY = lines.begin()->p1.Y; //to be able to compare it to other values while avoiding
        double imgminX = lines.begin()->p1.X; //initializing it with a lower/higher value than any other
        double imgminY = lines.begin()->p1.Y; //value in the list of points.
        for(std::list<Line2D>::iterator i = lines.begin(); i != lines.end(); i++)
        {
            imgmaxX = max(imgmaxX, max(i->p1.X, i->p2.X));
            imgmaxY = max(imgmaxY, max(i->p1.Y, i->p2.Y));
            imgminX = min(imgminX, min(i->p1.X, i->p2.X));
            imgminY = min(imgminY, min(i->p1.Y, i->p2.Y));
        }
        double Xrange = imgmaxX-imgminX;
        double Yrange = imgmaxY-imgminY;
        double ImageX = size*(Xrange/max(Xrange, Yrange));
        double ImageY = size*(Yrange/max(Xrange, Yrange));
        double d = (0.95*(ImageX/Xrange));
        double DCx = d*(imgminX + imgmaxX);
        double DCy = d*(imgminY + imgmaxY);
        double dX = (ImageX/2 - DCx);
        double dY = (ImageY/2 - DCy);
        for(std::list<Line2D>::iterator i = lines.begin(); i != lines.end(); i++)
        {
            i->p1.X = ((i->p1.X*d)+dX);
            i->p1.Y = ((i->p1.Y*d)+dY);
            i->p2.X = ((i->p2.X*d)+dX);
            i->p2.Y = ((i->p2.Y*d)+dY);
            unsigned int x1 = roundtoint(i->p1.X);
            unsigned int y1 = roundtoint(i->p1.Y);
            unsigned int x2 = roundtoint(i->p2.X);
            unsigned int y2 = roundtoint(i->p2.Y);
            image.draw_line(x1,y1,x2,y2,i->color);
        }
    }
}

最佳答案

头文件定义了类Point2DLine2D,成员函数在源文件中定义。相反,源文件定义了具有相同名称但位于本地 namespace 中的不同类。 “公共(public)”类未实现。

Lines2D.cc 中移除本地命名空间,并改为定义在 header 中声明的函数:

namespace Lines2D {
    Point2D::Point2D() : X(0), Y(0) {}
    Point2D::Point2D(double x, double y) : X(x), Y(y) {}
    Point2D::~Point2D() {} // or just get rid of this pointless destructor

    // likewise for Lines2D and the two non-member functions
}

最后,如果您只想在这个源文件中使用它,您应该从 header 中删除 roundtoint 的声明;或者从中删除 inline 或在 header 中定义它,如果您希望它普遍可用。

关于c++ - 解决“对[自定义类]的 undefined reference ”的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28855730/

相关文章:

c# - 在具有 ui/非 ui 线程差异的 WPF 中使用 PInvoke

c++ - 在头类中创建内联函数到私有(private)变量

c - 编辑行错误

c - 链接 libavcodec 和 libavformat : Undefined references

c++ - Sublime text 3 build 3083 未自动选择运行变体

c++ - 带有 std::map 的单例

c++ - 英特尔 Embree 中的这个 union 有什么作用?

c++ - 错误 : expected unqualified-id before 'bool'

c - 我在哪里可以获得 gcc/clang 等 C 标准函数的源代码?

c++ - boost静态库编译时链接错误 "undefined reference"