C++ 虚拟覆盖函数链接器错误

标签 c++ class polymorphism virtual

基类 = 形状

派生类 = 点

目标是在 Shape 中创建一个 Draw() 函数并将其声明为虚拟的,然后覆盖 Point 中的函数。我被链接器错误困住了,不明白为什么。

//shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>

using namespace std;

namespace James
{
    namespace CAD
    {
        class Shape
        {
        private:
            int m_id;
        public:
            Shape();    //default constructor
            virtual ~Shape();    //destructor
            Shape(const Shape& s);    //copy constructor
            Shape& operator = (const Shape& source);    //assignment operator
            virtual string ToString() const;    //returns id as a string
            friend ostream& operator << (ostream& os, const Shape& sh);    //global function that can access private members
            int ID() const;
            virtual void Draw() const;
        };

        inline int Shape::ID() const    //retrieve ID of shape
            {
                return m_id;
            }
    }
}
#endif

//shape.cpp

void Shape::Draw() const
        {
            cout << "shape drawing" << endl;
        }

//Point.h (inside the class)

void Draw() const;

//Point.cpp

void Shape::Draw() const
        {
            cout << "point drawing" << endl;
        }

//point.h

#ifndef POINT_H
#define POINT_H
#include <iostream>
#include "shape.h"

namespace James
{
    namespace CAD
    {
        class Point: public Shape
        {
        private:
            double m_x;
            double m_y;
        public:
            Point();    //default constructor
            ~Point();    //destructor
            Point(const Point& pt);    //copy constructor
            Point(double newX, double newY)    //constructor accepting x and y coordinates
            {
                m_x = newX;
                m_y = newY;
                std::cout << "Point(" << m_x <<","<< m_y <<")" << std::endl;
            }

            Point(double val);    //constructor accepting one double

            void X(double newXval) {m_x = newXval;}    //default inline setter
            void Y(double newYval) {m_y = newYval;}    //default inline setter

            double X() const;    //getter pre-inline
            double Y() const;    //getter pre-inline


            string ToString() const;    //returns a string description

            //distance functions
            double Distance() const;    //calculate the distance to the origin (0,0)
            double Distance(const Point& p) const;    //calculate the distance between two points

            //operator overloading
            Point operator - () const;    //negate the coordinates
            Point operator * (double factor) const;    //scale the coordinates
            Point operator + (const Point& p) const;    //add coordinates
            bool operator == (const Point& p) const;    //equally compare operator
            Point& operator = (const Point& source);    //assignment operator
            Point& operator *= (double factor);    //scale the coordinates and assign

            friend ostream& operator << (ostream& os, const Point& p);    //send to ostream (friend)
            void Draw() const;
        };

        inline double Point::X() const    //normal inline getter
        {
            return m_x;
        }

        inline double Point::Y() const    //normal inline getter
        {
            return m_y;
        }
    }
}
#endif

错误

Error   6   error LNK1120: 1 unresolved externals   C:\Users\James\Google Drive\Cylon\C++\Level 5\HW\3.5.4\Debug\3.5.4.exe  3.5.4

Error   2   error LNK2001: unresolved external symbol "public: virtual void __thiscall James::CAD::Point::Draw(void)const " (?Draw@Point@CAD@James@@UBEXXZ) C:\Users\James\Google Drive\Cylon\C++\Level 5\HW\3.5.4\3.5.4\circle.obj 3.5.4

Error   3   error LNK2001: unresolved external symbol "public: virtual void __thiscall James::CAD::Point::Draw(void)const " (?Draw@Point@CAD@James@@UBEXXZ) C:\Users\Cary\Google Drive\Cylon\C++\Level 5\HW\3.5.4\3.5.4\line.obj    3.5.4

Error   4   error LNK2019: unresolved external symbol "public: virtual void __thiscall James::CAD::Point::Draw(void)const " (?Draw@Point@CAD@Cary@@UBEXXZ) referenced in function "public: __thiscall std::basic_stringbuf<char,struct std::char_traits<char>,class std::allocator<char> >::basic_stringbuf<char,struct std::char_traits<char>,class std::allocator<char> >(int)" (??0?$basic_stringbuf@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@H@Z)  C:\Users\James\Google Drive\Cylon\C++\Level 5\HW\3.5.4\3.5.4\point.obj  3.5.4

Error   1   error LNK2005: "public: virtual void __thiscall Cary::CAD::Shape::Draw(void)const " (?Draw@Shape@CAD@Cary@@UBEXXZ) already defined in point.obj C:\Users\James\Google Drive\Cylon\C++\Level 5\HW\3.5.4\3.5.4\shape.obj  3.5.4

Error   5   error LNK2001: unresolved external symbol "public: virtual void __thiscall James::CAD::Point::Draw(void)const " (?Draw@Point@CAD@James@@UBEXXZ) C:\Users\James\Google Drive\Cylon\C++\Level 5\HW\3.5.4\3.5.4\test.obj   3.5.4

最佳答案

您没有实现 Point::Draw(您还有两个不同的 Shape::Draw 实现):

//Point.cpp

void Shape::Draw() const
{
  cout << "point drawing" << endl;
}

这应该是:

//Point.cpp

void Point::Draw() const
{
  cout << "point drawing" << endl;
}

关于C++ 虚拟覆盖函数链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293308/

相关文章:

python - 运行 def __init__(self) 函数后,如何在类对象中添加数据?

c++ - 如何在 OpenMP 中并行嵌套循环找到最近的两点?

c++ - 使用模板模板参数时模板参数推导失败

c++ malloc()不断分配给相同的内存地址

c++ - 在抽象基类中重载运算符的正确方法是什么?

java - 尝试通过在子类中扩展类来向类添加方法?

django-admin - django 中的多态内联模型形式

c++ - 命名空间中的类前向声明

Python,同时将类创建 append 到列表a

java - Java中实现观察者模式,同一个观察者跨多个类