kits
kits

Reputation: 607

C++ virtual override function linker errors

Base class = Shape

Derived class = Point

The goal is to create a Draw() function in Shape and declare it virtual, then override the function in Point. I am stuck on linker errors and don't understand why.

//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

ERRORS

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

Upvotes: 0

Views: 1611

Answers (2)

Pankaj Bansal
Pankaj Bansal

Reputation: 919

In shape.cpp, you are using scope resolution operator with Shape class. It should be on point class

//Point.cpp

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

Upvotes: 0

Gabor Angyal
Gabor Angyal

Reputation: 2255

You did not implement Point::Draw (also you have two different implementation for Shape::Draw):

//Point.cpp

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

This should be:

//Point.cpp

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

Upvotes: 1

Related Questions