getting linking Error while creating a flyweight_pattern [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
using namespace std;
class Shape
{
public :
virtual void draw()=0;
virtual ~Shape(){}
};
class Circle : public Shape
{
string color;
int x;
int y;
int radius;
public:
Circle(string color){
color = color;
}
void setX(int x) {
x = x;
}
void setY(int y) {
y = y;
}
void setRadius(int radius) {
radius = radius;
}
void draw() {
cout << "color :" << color << x << y ;
}
};
class ShapeFactory {
public:
static map<string, Shape*> circlemap;
static Shape* getcircle(string color)
{
Shape *mcircle;
mcircle = circlemap.find(color)->second;
if(mcircle == nullptr) {
mcircle = new Circle(color);
circlemap[color] = mcircle;
// circlemap.insert(std::make_pair(color,mcircle));
}
return mcircle;
}
};
class Flyweightpattern
{
public:
static string getRandomColor(string colors[]) {
int m_rand = rand() % 5;
return colors[m_rand];
}
static int getRandomX() {
return (int)(rand() % 100);
}
static int getRandomY() {
return (int)(rand() % 100);
}
};
int main()
{
string colors[] = { "Red", "Green", "Blue", "White", "Black" };
for(int i=0; i < 20; ++i) {
Circle *circle = dynamic_cast<Circle *>(ShapeFactory::getcircle(Flyweightpattern::getRandomColor(colors)));
circle->setX(Flyweightpattern::getRandomX());
circle->setY(Flyweightpattern::getRandomY());
circle->setRadius(100);
circle->draw();
}
getchar();
return 0;
}
I am getting the linking error during run is below :
flyweight_pattern.obj : error LNK2001: unresolved external symbol
"public: static class std::map,class std::allocator >,class Circle
*,struct std::less,class std::allocator > >,class
std::allocator,class std::allocator > const ,class
Circle *> > > ShapeFactory::circlemap"
(?circlemap#ShapeFactory##2V?$map#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAVCircle##U?$less#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2#V?$allocator#U?$pair#$$CBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAVCircle###std###2##std##A)
I have a map in the ShapeFactory class and tried to creating filling the map in the class itself but still not able to resolve the problem.

You didn't defined circlemap, it's a static member, so you should define it (and initialize) in global scope:
map<string, Shape*> ShapeFactory::circlemap = {};
Integral nonvolatile static members can be initialized in class.
Oh, and it is not recommended to do using namespace std; in global scope, that leads to side effects.
You can write something like
using std::map;
to target selected id (map in this case), and you can write using in namespace that contains usage.

Related

std::sort of polymorphic types not sorting as expected

I have taken sample code from ch9 Open Closed Principle in Agile Software Development, as below, which is supposed to sort shapes by precedence in the order given by the table typeOrderTable in the code below. Well, I did have to add a main and test code but pretty much copied the book code. But the output shows it is not sorting as per the table. And if I try to debug the code, the code path does not enter the Shape::Precedes function.
How can I fix this?
Code:
#include <typeinfo>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Shape {
public:
virtual void Draw() const = 0;
bool Precedes(const Shape& s) const;
bool operator<(const Shape& s) const {
return Precedes(s);
}
private:
static const char* typeOrderTable[];
};
bool Shape::Precedes(const Shape& s) const {
const char* thisType = typeid(*this).name();
const char* argType = typeid(s).name();
bool done{ false };
int thisOrd{ -1 };
int argOrd{ -1 };
for (int i = 0; !done; ++i) {
const char* tableEntry = typeOrderTable[i];
if (tableEntry != 0) {
if (strcmp(tableEntry, thisType) == 0)
thisOrd = i;
if (strcmp(tableEntry, argType) == 0)
argOrd = i;
if (argOrd >= 0 && thisOrd >= 0)
done = true;
}
else done = true;
}
return thisOrd < argOrd;
}
class Square : public Shape {
public:
virtual void Draw() const {
cout << "square\n";
}
};
class Circle : public Shape {
public:
virtual void Draw() const {
cout << "circle\n";
}
};
class Rectangle : public Shape {
public:
virtual void Draw() const {
std::cout << "rectangle\n";
}
};
const char* Shape::typeOrderTable[] = {
typeid(Circle).name(),
typeid(Rectangle).name(),
typeid(Square).name(),
0
};
void DrawAllShapes(vector<Shape*> shapes) {
vector<Shape*> orderedList = shapes;
sort(orderedList.begin(), orderedList.end());
for (auto shape : orderedList) {
shape->Draw();
}
}
int main() {
Shape* circle = new Circle();
Shape* square = new Square();
Shape* rectangle = new Rectangle();
Shape* rectangle2 = new Rectangle();
vector<Shape*> shapes{ rectangle, square, circle, rectangle2 };
DrawAllShapes(shapes);
/* should print:
circle
rectangle
rectangle
square
but instead prints:
rectangle
square
rectangle
circle
ie unsorted - actually it is random in the output
*/
}
You need to change your call to std::sort(...) to use a comparator. The way you have it, it is pointers to shapes being sorted not shapes so the operator< defined for shapes will not be called, instead the numeric values of the pointers will be compared.
One way to fix this is to sort like this
sort(orderedList.begin(), orderedList.end(),
[](Shape* s1, Shape* s2) { return s1->Precedes(*s2); }
);
providing a lambda taking Shape*'s to do the actual comparing.

visual studio 2015 c++ unresolved external symbol link error [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm a visual studio 2015 c++ newby who's trying to write some game code at home.
I'm getting this link error:
LNK2019 unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall display_utils::fit_int_2(int)" (?fit_int_2#display_utils##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "public: void __thiscall bat_stats::disp_bat_stats(struct bat_stats::bat_stats_typ)" (?disp_bat_stats#bat_stats##QAEXUbat_stats_typ#1##Z)
It apparently doesn't like the string I'm using to access the returned string from function fit_int_2. I've google searched for a solution, but can't find anything that fixes my problem. Note that the code compiled and linked before i I added the fit_int_2 call. Thanks in advance if you can help me out. The code is below:
bat_stats.h
#pragma once
class bat_stats
{
public:
struct bat_stats_typ
{
int gm;
int ab;
int ht;
int dbl;
int trpl;
int hr;
int rbi;
int sb;
int cs;
int bb;
int ibb;
int sf;
int sac;
int k;
int gidp;
int err;
float ave;
float slg;
float obp;
};
void disp_bat_hdr();
void disp_bat_stats( bat_stats_typ );
private:
int dummy;
};
bat_stats.cpp
#include <iostream>
using std::cout;
std::cin;
#include <string>
using std::string;
#include "bat_stats.h"
#include "display_utils.h"
void bat_stats::disp_bat_hdr()
{
cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n";
}
void bat_stats::disp_bat_stats( bat_stats_typ bat )
{
display_utils dut;
string s;
s = dut.fit_int_2( bat.gm ); // <- the problem is here!
cout << s << bat.gm << " ";
cout << bat.ab << "\n\n";
}
display_utils.h
#pragma once
#include <string>
using std::string;
class display_utils
{
public:
void insert_5_lines();
string fit_int_2( int );
private:
int dummy;
};
display_utils.cpp
#include <iostream>
using std::cout;
#include "display_utils.h"
void display_utils::insert_5_lines()
{
cout << "\n\n\n\n\n";
}
string fit_int_2(int i0)
{
string s0 = "";
if (i0 < 10)
{
s0 = " ";
}
return s0;
}
You need to change
string fit_int_2(int i0)
to
string display_utils::fit_int_2(int i0)
(You need to define the member function - currently you're defining an unrelated global function.)

c++ LINK2019 i am a rookie,i was trapped .please help me [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
i think my problem is from class Car;but i can not found it ,please help me.
#include <iostream>
void main()
{
int n = rand() % 6;
int time = 1;
for (int i = 0; i < n; i++) {
Car<int> car1; // this place is error
// BianDao.QInsert(car);
int n = 0;
car1.Get_Car_Number(n);
cout << n << endl;
}
}
template <class T>
class Car {
int Car_Number;
int Start_Time;
int Out_Time;
public:
Car(const T &item = 0);
~Car();
bool Get_Car_Number(T &item)
{
item = Car_Number;
return true;
}
bool Get_Start_Time(T &item);
bool Get_Out_Time(T &item);
bool Set_Start_Time(const T &item);
};
template <class T>
Car<T>::Car(const T &item)
: Start_Time(item)
{
Car_Number = 9999 + rand() % 99999;
}
Error:
error LNK2019: 无法解析的外部符号 "public: __thiscall
Car<int>::~Car<int>(void)" (??1?$Car#H##QAE#XZ),该符号在函数 _main
中被引用 C:\Users\Administrator\documents\visual studio
2013\Projects\停车场管理系统\停车场管理系统\main.obj 停车场管理系统
Now it is working:
#include <iostream>
template<typename T> // was missing
class Car{
int Car_Number;
int Start_Time;
int Out_Time;
public:
Car(const T&item = 0);
~Car();
bool Get_Car_Number(T&item){
item = Car_Number; return true;
}
bool Get_Start_Time(T&item);
bool Get_Out_Time(T&item);
bool Set_Start_Time(const T&item);
};
template<typename T> // was missing
Car<T>::Car(const T&item):Start_Time(item){//this is car's constructor
Car_Number = 9999 + rand() % 99999;
}
you forgot to place template<typename T> before class and its constructor.
Check it: http://ideone.com/YZN2EA

C++ Sorting a Array of Pointer to Objects [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ how to sort an array variable
I got a parent class call
Shape
Shape got 2 child call
Square and Rectangle
Shape class got a variable call area, which is of int type
So i created some object of Square, Rectangle like this
int main()
{
Shape *shaped[100];
//then i did some adding of object..
int areaValue;
areaValue=1;
shaped[0] = new Rectangle();
shaped[0]->setArea(areaValue);
areaValue=7;
shaped[1] = new Square();
shaped[1]->setArea(areaValue);
areaValue=5;
shaped[2] = new Square();
shaped[2]->setArea(areaValue);
shapeCounter = 3;
sort(shaped, shaped + 3, sort_by_area());
for (int i=0;i<shapeCounter;i++)
{
cout << shaped[i].getArea() << endl;
}
}
Then at e.g Square.cpp
I did this
struct sort_by_area
{
static bool operator()(Shape* x, Shape* y)
{
return x->getArea() < y->getArea();
}
};
This code above works. and can sort by area, but my question is that can i still sort if i don't use struct , cause if i don't use struct, it will say the sort_by_area is not declared in scope.
Must i really use struct so my main.cpp can access the sort code that is located at the child class .cpp
Thanks
This works perfectly:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Shape{
private:
int x;
public:
void setArea(int x){
this->x = x;
}
int getArea(){
return this->x;
}
};
class Rectangle: public Shape{
public:
};
class Square: public Shape{
public:
};
bool sort_by_area (Shape* x,Shape* y) { return (x->getArea() < y->getArea()); }
int main()
{
Shape *shaped[100];
//then i did some adding of object..
int areaValue,shapeCounter = 0;
areaValue=1;
shaped[0] = new Rectangle();
shaped[0]->setArea(areaValue);
areaValue=7;
shaped[1] = new Square();
shaped[1]->setArea(areaValue);
areaValue=5;
shaped[2] = new Square();
shaped[2]->setArea(areaValue);
shapeCounter = 3;
sort(shaped, shaped + 3, sort_by_area);
for (int i=0;i<shapeCounter;i++)
{
cout << shaped[i]->getArea() << endl;
}
return 0;
}

memory error (access violation) on method parameters

I've got weird problem. I'm trying to write simple game in C++, but I failed on objects and data types. There's a code:
// C++
// Statki
#include <stdio.h>
#include <time.h>
#include <map>
#include <vector>
#include <string>
#include <list>
#define D true
using namespace std;
void _(char* message){printf("%s\n",message);};
struct relpoint { int x,y; };
struct point { int x,y; };
struct size { int w,h; };
map<const char*, vector<relpoint> > shipshape;
list<char*> shipTypes = {"XS", "S", "M", "L", "XL"};
string alpha="ABCDEFGHIJKLMNOPRSTUVWXYZ";
enum fieldtype { UNKNOWN=-1,EMPTY=0,SHIP=1,HIT=2,MISS=3,};
enum rotation { EAST=0, SOUTH=1, WEST=2, NORTH=3 };
class Ship
{
char* type;
};
class Sea
{
public:
void init(size mapsize) { init( mapsize, EMPTY ); };
void init(size mapsize, fieldtype fill)
{
if(D)printf("Generating sea\n");
vector<fieldtype> v;
seamap.reserve(mapsize.h);
v.reserve(mapsize.w);
for (int y=0; y<mapsize.h; y++)
{
v.clear();
for(int x=0; x<mapsize.w; x++)
{
v.push_back(fill);
}
seamap.push_back(v);
}
view();
};
bool place_ship(Ship ship);
void view()
{
for( vector< vector<fieldtype> >::const_iterator yy = seamap.begin(); yy != seamap.end(); ++yy )
{
for( vector<fieldtype>::const_iterator xx = (*yy).begin(); xx != (*yy).end(); ++xx )
{
if(D)printf("%d ", *xx);
}
if(D)printf("\n");
}
};
private:
vector< vector<fieldtype> > seamap;
};
class Game
{
public:
void initmap(size mapsize)
{
if(D) printf("\nInit %d×%d map\n", mapsize.w, mapsize.h);
(*enemymap).init(mapsize, UNKNOWN);
//(*selfmap).init(mapsize);
};
bool placeship(string type, point position, rotation rotate);
fieldtype shoot(point target);
void viewmap(){(*selfmap).view();};
bool eog();
Sea * enemymap;
Sea * selfmap;
};
class Bot
{
public:
void init(size mapsize)
{
if(D)_("Init Bot");
}
private:
Game * g;
};
class Player
{
public:
Player() { if(D){_("Player fake init");} };
void init(size mapsize)
{
(*g).initmap(mapsize);
};
void viewmap(){(*g).viewmap();};
private:
Game * g;
};
class Router
{
public:
void startgame();
void welcomescreen()
{
printf("\n\n\n\t\t\tShips minigame\n\t\t\t\tby Kris\n\n");
mainmenu();
};
void mainmenu()
{
printf("Menu (type letter):\n\tN: New game\n\tS: Settings\n\tQ: Quit game\n\n > ");
char opt;
opt = toupper(getchar());
size ms;
switch(opt)
{
case 'N':
ms = getmapsize();
(*P1).init(ms);
(*P2).init(ms);
break;
case 'S':
break;
case 'Q':
break;
default:
printf("Invalid option %c", opt);
mainmenu();
}
};
private:
Player * P1;
Bot * P2;
size getmapsize()
{
size ms;
printf("\nSet map size (X Y)\n > ");
scanf("%d %d", &ms.w, &ms.h);
return ms;
};
};
int main () {
vector<relpoint> shp;
shp.reserve(5);
list<char*>::const_iterator tp = shipTypes.begin();
shp.push_back({0,0});
shipshape[*(tp++)] = shp;
shp.push_back({1,0});
shipshape[*(tp++)] = shp;
shp.push_back({2,0});
shipshape[*(tp++)] = shp;
shp.push_back({3,0});
shipshape[*(tp++)] = shp;
shp.push_back({2,1});
shipshape[*tp] = shp;
Router R;
R.welcomescreen();
printf("\n\n");
return 0;
}
It can be compiled, but after line Init 5×5 map program stops with Naruszenie ochrony pamięci (Memory access violation in polish) error. Problem seems to occur at both Sea::init() functions.
I'm compiling it with g++ -std=c++0x -Wno-write-strings ships2.cpp (to prevent warnings) on Ubuntu.
Any idea what's wrong with it?
All the classes contain pointers, but you never seem to initialize the pointers or allocate space for the objects they should point to.
Doing this
(*enemymap).init(mapsize, UNKNOWN);
when enemymap doesn't point anywhere, is an almost sure way to get an access violation.
You are using an uninitialized pointer. You can fix it by instantiating an object here, or in a constructor somewhere else.
Here is an example of instantiating in the initmap call.
void initmap(size mapsize)
{
// Initialize the pointer by instantiating a class
enemymap = new Sea;
if(D) printf("\nInit %d×%d map\n", mapsize.w, mapsize.h);
(*enemymap).init(mapsize, UNKNOWN);
//(*selfmap).init(mapsize);
};
+1 for Bo. But for your own sake:
compile with -g and then then
gdb ./mygame.bin
type 'run'
after setting the map size 5 5 :
Program received signal SIGSEGV, Segmentation fault.
mainmenu (this=<optimized out>) at memacvio.cpp:158
158 (*P1).init(ms);
This should tell you that P1 is probably not a valid poiner.