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

时间:2015-02-03 07:15:33

标签: c++ class polymorphism virtual

基类=形状

派生类=点

目标是在Shape中创建一个Draw()函数并将其声明为virtual,然后覆盖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

2 个答案:

答案 0 :(得分:1)

您没有实施Point::DrawShape::Draw也有两种不同的实现方式):

//Point.cpp

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

这应该是:

//Point.cpp

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

答案 1 :(得分:0)

在shape.cpp中,您正在使用带有Shape类的范围解析运算符。它应该在点类

//Point.cpp

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