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

C++ virtual override function linker errors

本文关键字:错误 链接 函数 虚拟 覆盖 C++      更新时间:2023-10-16

Base class = Shape

派生类 = 点

目标是在 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:UsersJamesGoogle DriveCylonC++Level 5HW3.5.4Debug3.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:UsersJamesGoogle DriveCylonC++Level 5HW3.5.43.5.4circle.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:UsersCaryGoogle DriveCylonC++Level 5HW3.5.43.5.4line.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:UsersJamesGoogle DriveCylonC++Level 5HW3.5.43.5.4point.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:UsersJamesGoogle DriveCylonC++Level 5HW3.5.43.5.4shape.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:UsersJamesGoogle DriveCylonC++Level 5HW3.5.43.5.4test.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;
}

> 在 shape.cpp 中,您正在将范围解析运算符与 Shape 类一起使用。它应该在点类上

//Point.cpp
    void Point::Draw() const
    {
        cout << "point drawing" << endl;
    }
相关文章: