问题解决"对[自定义类]的未定义引用

时间:2015-03-04 13:30:24

标签: c++ header-files undefined-reference

我现在一直在努力解决错误,但仍然无法找到修复错误的方法,特别是未定义的引用错误:

未定义引用`Lines2D :: Point2D :: ~Point2D()' engine.cc / Graphicsengine line 20 C / C ++问题

我为我的" 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;
}

&#34; Lines2D.h&#34;

#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_ */

&#34; Lines2D.cc&#34;

#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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

标头定义类Point2DLine2D,并保留要在源文件中定义的成员函数。相反,源文件定义具有相同名称的不同类,但在本地名称空间中。 “公共”课程没有实现。

Lines2D.cc中删除本地名称空间,而是定义标题中声明的函数:

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
}

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