首页 > 解决方案 > LNK2001 C++ 接口类中未解析的外部符号“public:virtual void ...”

问题描述

我曾尝试使用纯虚拟方法以及测试其他类似问题的答案,但一切都继续导致同样的问题。

所以我有这个类Shape作为声明virtual void Draw()方法的接口。这个函数后来在其他三个基本形状类中被覆盖,并从一个名为 的类中调用GameObject,该类调用一个方法Draw()WireframeImage然后调用这些基本形状类之一。编译时出现以下错误:

错误:LNK2001 无法解析的外部符号“public: virtual void __thiscall Shape::Draw (class olc::PixelGameEngine,int,int,float,int,int,int)”(?Draw@Shape@@UAEXVPixelGameEngine@olc@@HHMHHH@ Z)

文件:GameObject.obj


错误:LNK2001 无法解析的外部符号“public: virtual void __thiscall Shape::Draw (class olc::PixelGameEngine,int,int,float,int,int,int)”(?Draw@Shape@@UAEXVPixelGameEngine@olc@@HHMHHH@ Z)

文件:Shape.obj


错误:LNK2001 无法解析的外部符号“public: virtual void __thiscall Shape::Draw (class olc::PixelGameEngine,int,int,float,int,int,int)”(?Draw@Shape@@UAEXVPixelGameEngine@olc@@HHMHHH@ Z)

文件:WireframeImage.obj


错误:LNK1120 1 unresolved externals

这是我的代码:

形状.h

#pragma once
#ifndef CLASS_SHAPES
#define CLASS_SHAPES

#include "olcPixelGameEngine.h"

class Shape
{
public:
    virtual void Draw(
        olc::PixelGameEngine engine,
        int originX,
        int originY,
        float scale,
        int rotation,
        int offsetX,
        int offsetY
    );
    olc::Pixel Color = olc::WHITE;
};


// Derived Shapes

class Line: public Shape {
private:
    Point P1, P2;
   public:
        Line();
        Line(Point, Point, olc::Pixel);
        void Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) override;
};

class Triangle: public Shape {
private:
    Line L1, L2, L3;
   public:
        Triangle();
        Triangle(Point, Point, Point, olc::Pixel);
        void Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) override;
};

class Rectangle: public Shape {
private:
    Line L1, L2, L3, L4;
   public:
        Rectangle();
        Rectangle(Point, Point, Point, Point, olc::Pixel);
        void Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) override;
};


#endif // !CLASS_SHAPES

形状.cpp

#include "Shapes.h"

Line::Line() { }
Line::Line(Point p1, Point p2, olc::Pixel color = olc::WHITE) {
    P1 = p1;
    P2 = p2;
    Color = color;
}
void Line::Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) {
    engine.DrawLine(P1.X, P1.Y, P2.X, P2.Y, Color);
}

Triangle::Triangle() { }
Triangle::Triangle(Point p1, Point p2, Point p3, olc::Pixel color = olc::WHITE) {
    L1 = {p1, p2, color};
    L2 = {p2, p3, color};
    L3 = {p3, p1, color};
    Color = color;
}
void Triangle::Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) {
    L1.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
    L2.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
    L3.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
}

Rectangle::Rectangle() { }
Rectangle::Rectangle(Point p1, Point p2, Point p3, Point p4, olc::Pixel color = olc::WHITE) {
    L1 = {p1, p2, color};
    L2 = {p2, p3, color};
    L3 = {p3, p4, color};
    L4 = {p4, p1, color};
    Color = color;
}
void Rectangle::Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) {
    L1.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
    L2.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
    L3.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
    L4.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
}

线框图像.h

#pragma once
#ifndef CLASS_WIREFRAMEIMAGE
#define CLASS_WIREFRAMEIMAGE

#include <iostream>

#include "Shapes.h"
#include "SymmetryMode.h"

class WireframeImage
{
private:
    std::vector<Triangle>   _shapes;
    SymmetryMode        _symmetryMode;
public:
    WireframeImage();
    WireframeImage(std::string file);
    void Load(std::string file);
    void Draw(olc::PixelGameEngine engine, int x, int y, float scale, int rotation, int offsetX, int offsetY);
};

#endif // !CLASS_WIREFRAMEIMAGE

线框图像.cpp

#include "WireframeImage.h"
#include "Shapes.h"

#include <string>
#include <iostream>

WireframeImage::WireframeImage() { }
WireframeImage::WireframeImage(std::string file) {
    Load(file);
}

void WireframeImage::Load(std::string file) {
    // Code ...
}

void WireframeImage::Draw(olc::PixelGameEngine engine, int originX, int originY, float scale, int rotation, int offsetX, int offsetY) {
    for(Shape s : _shapes) {
        s.Draw(engine, originX, originY, scale, rotation, offsetX, offsetY);
    }
}

游戏对象.h

#include "GameObject.h"

GameObject::GameObject() { }
GameObject::GameObject(olc::PixelGameEngine engine, WireframeImage sprite, int x, int y) {
    _engine = engine;
    Sprite = sprite;
    X = x;
    Y = y;
    Rotation = 0;
}
void GameObject::Draw() {
    this->Sprite.Draw(this->_engine, this->_engine.ScreenWidth()/2, this->_engine.ScreenHeight()/2, Scale, Rotation, X, Y);
}

如果有任何帮助,我正在使用 Visual Studio 2017。

标签: c++interfacelinker-errorsvirtual

解决方案


推荐阅读