提问人:Felix Neijzen 提问时间:3/4/2015 更新时间:3/4/2015 访问量:289
解决“对 [costum 类] 的未定义引用”的问题
problems solving "undefined reference to [costum class]
问:
我已经为错误苦苦挣扎了大约一天,但仍然没有找到修复错误的方法,特别是未定义的引用错误:
对“Lines2D::P oint2D::~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;
}
“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);
}
}
}
答:
1赞
Mike Seymour
3/4/2015
#1
标头定义类 和 ,并将成员函数保留在源文件中定义。相反,源文件定义具有相同名称的不同类,但在本地命名空间中。“公共”类未实现。Point2D
Line2D
从 中删除本地命名空间,改为定义标头中声明的函数: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
下一个:C++ 未定义对(函数)的引用
评论