C++工厂模式中的运行时错误

Runtime Error in Factory Pattern in C++

我最近从 JAVA 转向了 C++。 我正在尝试实施工厂设计模式。

这是我的文件。

Shape.h

    #pragma once

    class Ishape
    {
    public:
        virtual void draw()=0;
    };

Rectangle.h

    #pragma once
    #include"Shape.h"
    class Rectangle :public Ishape
    {
    public:
        void draw();
    };

Rectangle.cpp

    #include"Rectangle.h"
    #include<iostream>
    void draw()
    {
        std::cout << "Rectangle Draw" << std::endl;
    }

Square.h

    #pragma once
    #include"Shape.h"

    class Square :public Ishape
    {
    public:
        void draw();
    };

Square.cpp

    #include"Square.h"
    #include<iostream>

    void draw()
    {
        std::cout << "Square Draw" << std::endl;
    }

ShapeFactory.h

    #pragma once
    #include "Shape.h"
    #include<iostream>
    class ShapeFactory
    {
    public:
        static Ishape* getShape(std::string type);
    };

ShapeFactory.cpp

    #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<iostream>
    #include"ShapeFactory.h"
    int main()
    {
        using namespace std;
        Ishape* shape = ShapeFactory::getShape("Rectangle");
        shape->draw();
        return 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   

几点: 我读到将头文件和代码文件分开保存是一种很好的做法,这就是我遵循这一点的原因。

我遵循了这个工厂设计模式的例子。(https://www.tutorialspoint.com/design_pattern/factory_pattern.htm)

任何人都可以提出建议,如何克服 error.Any 来自 c++ 点的其他提示也将有所帮助。

Rectangle.cpp

#include"Rectangle.h"
#include<iostream>
void Rectangle::draw()
//   ^^^^^^^^^^^
{
    std::cout << "Rectangle Draw" << std::endl;
}

Square.cpp

#include"Square.h"
#include<iostream>

void Square::draw()
//   ^^^^^^^^
{
    std::cout << "Square Draw" << std::endl;
}

ShapeFactory.cpp

#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;
}

您在 OOP 方面做得很好。但显然你不了解 C++ 的通用原理。当您在 Rectangle.cpp 中编写 void draw() 时,您实际上定义了自由函数,但没有 Rectangle 的成员方法(在其他情况下类似)