Unresolved External Symbol linker error (C++)
I am trying to develop abstract design pattern code for one of my project as below.. But, I am not able to compile the code ..giving some compile errors(like "unresolved external symbol "public: virtual void __thiscall Xsecs::draw_lines(double,double)" (?draw_lines@Xsecs@@UAEXNN@Z)" ).. Can any one please help me out in this...
#include "stdafx.h"
#include <iostream>
#include <vector>
#include "Xsecs.h"
using namespace std;
//Product class
class Xsecs
{
public:
virtual void draw_lines(double pt1, double pt2);
virtual void draw_curves(double pt1, double rad);
};
class polyline: public Xsecs
{
public:
virtual void draw_lines(double pt1,double pt2)
{
cout<<"draw_line in polygon"<<endl;
}
virtual void draw_curves(double pt1, double rad)
{
cout<<"Draw_curve in circle"<<endl;
}
/*void create_polygons()
开发者_运维知识库 {
cout<<"create_polygon_thru_draw_lines"<<endl;
}*/
};
class circle: public Xsecs
{
public:
virtual void draw_lines(double pt1,double pt2)
{
cout<<"draw_line in polygon"<<endl;
}
virtual void draw_curves(double pt1, double rad)
{
cout<<"Draw_curve in circle"<<endl;
}
/*void create_circles()
{
cout<<"Create circle"<<endl;
}*/
};
//Factory class
class Factory
{
public:
virtual polyline* create_polyline()=0;
virtual circle* create_circle()=0;
};
class Factory1: public Factory
{
public:
polyline* create_polyline()
{
return new polyline();
}
circle* create_circle()
{
return new circle();
}
};
class Factory2: public Factory
{
public:
circle* create_circle()
{
return new circle();
}
polyline* create_polyline()
{
return new polyline();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Factory1 f1;
Factory * fp=&f1;
return 0;
}
I presume you were attempting to create a virtual base class. You need to add '= 0' to the end of the draw_lines and draw_curves methods in the class Xsecs
class Xsecs
{
public:
virtual void draw_lines(double pt1, double pt2) = 0;
virtual void draw_curves(double pt1, double rad) = 0;
};
the compiler is complaining as you haven't any implementation for the methods in question.
You should inherit publicly from A
, like
class ProductA1 : public ProductA {
...
Without the public
keyword, this relationship is private inheritance, which is not an is-a relationship, therefore you can't simply cast from ProductA1
to ProductA
.
Scott Meyers explains this in Effective C++, Third Ed., Item 39:
[...] compilers, when given a hierarchy in which a class
Student
publicly inherits from a classPerson
, implicitly convert Students to Persons when that is necessary for a function call to succeed.[...] the first rule governing private inheritance you've just seen in action: in contrast to public inheritance, compilers will generally not convert a derived class object (such as
Student
) into a base class object (such asPerson
) if the inheritance relationship between the classes is private. [...] The second rule is that members inherited from a private base class become private members of the derived class, even if they were protected or public in the base class.Private inheritance means is-implemented-in-terms-of. If you make a class
D
privately inherit from a classB
, you do so because you are interested in taking advantage of some of the features available in classB
, not because there is any conceptual relationship between objects of typesB
andD
. As such, private inheritance is purely an implementation technique.
Update for the 2nd version of the post: if you want pure virtual functions, you should declare them so:
virtual void draw_lines(double pt1, double pt2) = 0;
virtual void draw_curves(double pt1, double rad) = 0;
Otherwise the linker will miss their definition.
In all your class definitions you forgot to use the public keyword :
class ProductA1 : ProductA
should be
class ProductA1 : public ProductA
and so on
You either need to add an implementation for Xsecs::draw_lines / Xsecs::draw_curves, or define them as pure virtual, by appending "= 0" to their definition.
class Xsecs
{
public:
virtual void draw_lines(double pt1, double pt2)
{
// Do something
}
virtual void draw_curves(double pt1, double rad)
{
// Do something
}
};
Or...
class Xsecs
{
public:
virtual void draw_lines(double pt1, double pt2) = 0;
virtual void draw_curves(double pt1, double rad) = 0;
};
change "class" to "struct" this make the default public inheritance rather than private
精彩评论