解决“对 [costum class] 的未定义引用的问题
problems solving "undefined reference to [costum class]
我已经为一个错误苦苦挣扎了大约一天,但仍然没有找到解决我的错误的方法,特别是未定义的引用错误:
对 `Lines2D::Point2D::~Point2D()' 的未定义引用 engine.cc /Graphicsengine 第 20 行 C/C++ 问题
我调用 "Lines2D.h" class 的每个对象都收到此错误,所以我可能在那里做错了。
相关代码如下:
"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);
}
}
}
header定义了类Point2D
和Line2D
,成员函数在源文件中定义。相反,源文件定义了具有相同名称但位于本地名称空间中的不同 类。 "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 中定义它,如果您希望它普遍可用。
我已经为一个错误苦苦挣扎了大约一天,但仍然没有找到解决我的错误的方法,特别是未定义的引用错误:
对 `Lines2D::Point2D::~Point2D()' 的未定义引用 engine.cc /Graphicsengine 第 20 行 C/C++ 问题
我调用 "Lines2D.h" class 的每个对象都收到此错误,所以我可能在那里做错了。
相关代码如下:
"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);
}
}
}
header定义了类Point2D
和Line2D
,成员函数在源文件中定义。相反,源文件定义了具有相同名称但位于本地名称空间中的不同 类。 "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 中定义它,如果您希望它普遍可用。