C++虚拟覆盖函数链接器错误
C++ virtual override function linker errors
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;
}
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接