C+;中工厂模式中的运行时错误+; 我最近从java切换到C++。 我正在尝试实现工厂设计模式
这是我的档案 Shape.hC+;中工厂模式中的运行时错误+; 我最近从java切换到C++。 我正在尝试实现工厂设计模式,c++,factory-pattern,virtual-functions,C++,Factory Pattern,Virtual Functions,这是我的档案 Shape.h #pragma once class Ishape { public: virtual void draw()=0; }; #pragma once #include"Shape.h" class Rectangle :public Ishape { public: void draw(); }; #pragma once #
#pragma once
class Ishape
{
public:
virtual void draw()=0;
};
#pragma once
#include"Shape.h"
class Rectangle :public Ishape
{
public:
void draw();
};
#pragma once
#include"Shape.h"
class Square :public Ishape
{
public:
void draw();
};
#pragma once
#include "Shape.h"
#include<iostream>
class ShapeFactory
{
public:
static Ishape* getShape(std::string type);
};
矩形.h
#pragma once
class Ishape
{
public:
virtual void draw()=0;
};
#pragma once
#include"Shape.h"
class Rectangle :public Ishape
{
public:
void draw();
};
#pragma once
#include"Shape.h"
class Square :public Ishape
{
public:
void draw();
};
#pragma once
#include "Shape.h"
#include<iostream>
class ShapeFactory
{
public:
static Ishape* getShape(std::string type);
};
矩形.cpp
#include"Rectangle.h"
#include<iostream>
void draw()
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void draw()
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
#include<iostream>
#include"ShapeFactory.h"
int main()
{
using namespace std;
Ishape* shape = ShapeFactory::getShape("Rectangle");
shape->draw();
return 0;
}
#include"Rectangle.h"
#include<iostream>
void Rectangle::draw()
// ^^^^^^^^^^^
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void Square::draw()
// ^^^^^^^^
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
Square.cpp
#include"Rectangle.h"
#include<iostream>
void draw()
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void draw()
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
#include<iostream>
#include"ShapeFactory.h"
int main()
{
using namespace std;
Ishape* shape = ShapeFactory::getShape("Rectangle");
shape->draw();
return 0;
}
#include"Rectangle.h"
#include<iostream>
void Rectangle::draw()
// ^^^^^^^^^^^
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void Square::draw()
// ^^^^^^^^
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
客户端代码:
Source.cpp
#include"Rectangle.h"
#include<iostream>
void draw()
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void draw()
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
#include<iostream>
#include"ShapeFactory.h"
int main()
{
using namespace std;
Ishape* shape = ShapeFactory::getShape("Rectangle");
shape->draw();
return 0;
}
#include"Rectangle.h"
#include<iostream>
void Rectangle::draw()
// ^^^^^^^^^^^
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void Square::draw()
// ^^^^^^^^
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
#包括
#包括“ShapeFactory.h”
int main()
{
使用名称空间std;
Ishape*shape=ShapeFactory::getShape(“矩形”);
形状->绘图();
返回0;
}
当我运行此操作时,出现以下运行时错误:
Severity Code Description Project File Line Suppression State
Error LNK2005 "void __cdecl draw(void)" (?draw@@YAXXZ) already defined in Rectangle.obj 1
Error LNK2019 unresolved external symbol "public: static class Ishape * __cdecl ShapeFactory::getShape(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?getShape@ShapeFactory@@SAPAVIshape@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main Project1 C:\Users\nikhil\source\repos\Project1\Project1\Source.obj 1
Error LNK1120 1 unresolved externals Project1 C:\Users\nikhil\source\repos\Project1\Debug\Project1.exe 1
严重性代码描述项目文件行抑制状态
错误LNK2005“void\uu cdecl draw(void)”(?draw@@YAXXZ)已在Rectangle.obj 1中定义
错误LNK2019未解析的外部符号“public:static类Ishape*uu cdecl ShapeFactory::getShape(类std::basic_字符串)”(?)?getShape@ShapeFactory@@SAPAVIshape@@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@@Z)在函数\u main Project1 C:\Users\nikhil\source\repos\Project1\Project1\source.obj 1中引用
错误LNK1120 1未解析的外部项目1 C:\Users\nikhil\source\repos\Project1\Debug\Project1.exe 1
几点:
我读到将头文件和代码文件分开保存是一种很好的做法,这就是为什么我遵循这个原则
我遵循了这个工厂设计模式的例子
任何人都可以建议如何克服错误。C++中的其他技巧也会有帮助。 < P> <强>矩形。CPP
#include"Rectangle.h"
#include<iostream>
void draw()
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void draw()
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
#include<iostream>
#include"ShapeFactory.h"
int main()
{
using namespace std;
Ishape* shape = ShapeFactory::getShape("Rectangle");
shape->draw();
return 0;
}
#include"Rectangle.h"
#include<iostream>
void Rectangle::draw()
// ^^^^^^^^^^^
{
std::cout << "Rectangle Draw" << std::endl;
}
#include"Square.h"
#include<iostream>
void Square::draw()
// ^^^^^^^^
{
std::cout << "Square Draw" << std::endl;
}
#include"ShapeFactory.h"
#include"Rectangle.h"
#include"Square.h"
static Ishape* getShape(std::string type)
{
Ishape* s = nullptr;
if (type == "Rectangle")
{
s= new Rectangle;
}
else if (type == "Square")
{
s= new Square;
}
return s;
}
你在面向对象方面做得很好。但显然你不理解C++的一般原理。当您在矩形中写入
void draw()
时,cpp实际上是自由函数,但没有Rectangle
的成员方法(在其他情况下类似)这些是链接器错误而不是运行时错误。哦,我如何修复它们?您创建了两个名为draw()
的自由函数。它们与你的课程无关。链接器抱怨你违反了一个定义规则。你可能会感兴趣。“我最近从java切换到C++”——然后步骤一就是要忘记你所知道的关于java的一切,C++是一种全新的语言。语法上有相似之处,但就目前而言,看似相同的结构的语义通常是完全不同的。只是提示一下。矩形和正方形这两个类是从Ishape扩展而来的,它们定义了自己的绘图。我可以用它来应用polmorphism吗