简体   繁体   English

未解析的外部符号“类”

[英]Unresolved External symbol “class”

Error LNK2001 unresolved external symbol "class Object objects" 错误 LNK2001无法解析的外部符号“类对象对象”

When the class is declared as an extern, it will create this linker error for every piece of code connected to it. 当将该类声明为extern时,它将为连接到它的每一段代码创建此链接器错误。 But when I create a private version of the class in other files, they work fine. 但是,当我在其他文件中创建该类的私有版本时,它们可以正常工作。

Changing the name of the class variable has no effect, so it does not conflict with the mapping system causing the error. 更改类变量的名称无效,因此它不会与导致错误的映射系统冲突。

I am unable to see and amend the error. 我看不到并修改错误。 It was working previously to which I undid the files in the hopes of it functioning again, but it did not. 以前它在工作,我撤消了文件,希望它能再次运行,但没有。

Object.cpp Object.cpp

#include "object.h"

#include "Ball.h"

// Constructor
Object::Object() {

    std::map<int, std::map<int, IMAGEINFO>> object;

};

Object::~Object() {

    object.clear();

};    

void Object::Update() {

    // Gets all the base object ID's
    for (unsigned int i = 0; i < object.size(); i++) {

    // Gets all the object's inside that ID.
        for (unsigned int j = 0; j < object[i].size(); j++) {

            // Gets the object.
            IMAGEINFO obj = object[i][j];

            // Calls for the processes of the object
            obj.Update();
        }
    }
}

void Object::Render(HDC hdc, RECT* prc, BITMAP bm) {

    // Gets all the base object ID's
    for (unsigned int i = 0; i < object.size(); i++) {

        // Gets all the object's ID's. 
        for (unsigned int j = 0; j < object[i].size(); j++) {

            // Gets the object.     
            IMAGEINFO obj = object[i][j];

            // Calls for the rendering of the object        
            obj.Render(hdc, prc, bm);

        }
    }
}

int Object::addObject(IMAGEINFO obj) {

    bool index = false;
    int indexVal;
    short run = 0;

    while (!index) {

        if (typeid(obj).name() == typeid(object[run]).name()) {

            object[run][object[run].size()] = obj;
            indexVal = object[run].size();
            index = true;

        }
        else if (run == object.size()) {

            object[run][0] = obj;
            indexVal = 0;
            index = true;

        }

        run++;

    }

    return indexVal;

}

object.h 对象

#ifndef OBJECT_H
#define OBJECT_H
#endif

#include <windows.h>
#include <iostream>
#include <map>

#include "renderer.h"
#include "resource.h"

class Object {
public:

    // Constructors/Deconstructors
    Object();
    ~Object();

    // Processes
    int addObject(IMAGEINFO value);
    void Update();
    void Render(HDC hdc, RECT* prc, BITMAP bm);

private:

    std::map<int, std::map<int, IMAGEINFO>> object;

};

extern Object objects;

Ball.cpp 球cpp

#include "Ball.h";

int indexPos;

HBITMAP hbm_Ball_Image = NULL;
HBITMAP hbm_Ball_Mask = NULL;

IMAGEINFO ballInfo;

void Ball(BITMAP bm, int assx, int assy) {

    indexPos = objects.addObject(ballInfo);

    hbm_Ball_Image = createImage((HBITMAP) BITMAP_BALL);
    hbm_Ball_Mask = createMask((HBITMAP)BITMAP_BALL, RGB(255, 255, 255), bm);

    GetObject(hbm_Ball_Image, sizeof(bm), &bm);

    ballInfo.height = bm.bmHeight;
    ballInfo.width = bm.bmWidth;

    ballInfo.x = assx;
    ballInfo.y = assy;

    ballInfo.speedX = 1;
    ballInfo.speedY = 0;

}

ball.h

#ifndef BALL_H
#define BALL_H
#endif

#include <windows.h>
#include <iostream>
#include <map>

#include "renderer.h"
#include "resource.h"
#include "object.h"

void Ball(BITMAP bm, int assx, int assy);
void render(HDC hdc, RECT* prc, BITMAP bm);

Add

Object objects;  // construct an instance of the class

to the end of file Object.cpp 到文件Object.cpp的末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM