Unresolved external character linker error (C ++)

I am trying to develop abstract design template code for one of my projects, as shown below. But I can not compile the code. Providing some compilation errors (for example, "unresolved external character" public: virtual void __thiscall Xsecs :: draw_lines (double, double) "(? Draw_lines @Xsecs @@ UAEXNN @Z)"). Can anyone help me on 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; } 
+4
source share
5 answers

I assume you tried to create a virtual base class. You need to add '= 0' to the end of the draw_lines and draw_curves methods in the Xsecs class

 class Xsecs { public: virtual void draw_lines(double pt1, double pt2) = 0; virtual void draw_curves(double pt1, double rad) = 0; }; 

the compiler complains because you do not have any implementation for the methods in question.

+6
source

You must inherit publicly from A , e.g.

 class ProductA1 : public ProductA { ... 

Without the public keyword, this relationship is a private inheritance, which is not an is-a relationship, so you cannot just distinguish from ProductA1 to ProductA .

Scott Meyers explains this in Effective C ++, Third Ed. Item 39:

[...] when a hierarchy is defined in which the Student class is publicly inherited from the Person class, implicitly converts students into people when it is necessary for a successful function call.

[...] the first rule governing private inheritance that you just saw in action: unlike general inheritance, compilers usually do not convert an object of a derived class (for example, Student ) to an object of a base class (for example Person ), if the inheritance relationship is between 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.

Means of private inheritance implemented-in-conditions. If you make class D privately inherit from class B , you will do it because you are interested in using some of the functions available in class B , and not because there is any conceptual connection between objects of types B and D Thus, private inheritance is purely a method of implementation.

Update for the second version of the message: if you want to use pure virtual functions, you must declare them like this:

 virtual void draw_lines(double pt1, double pt2) = 0; virtual void draw_curves(double pt1, double rad) = 0; 

Otherwise, the linker will skip its definition.

+2
source

In all your class definitions, you forgot to use the public keyword:

 class ProductA1 : ProductA 

it should be

 class ProductA1 : public ProductA 

etc.

+1
source

You need to either add an implementation for Xsecs :: draw_lines / Xsecs :: draw_curves, or define them as pure virtual by adding "= 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; }; 
+1
source

change the "class" to "struct", this will do standard public inheritance, not private

-1
source

Source: https://habr.com/ru/post/1307866/


All Articles