類文件中出現未解析的外部符號 (Unresolved External Symbol Occuring with Class Files)


問題描述

類文件中出現未解析的外部符號 (Unresolved External Symbol Occuring with Class Files)

First point: I'm not a C++ expert, far from it.  I glanced at it briefly almost a year ago and have not touched again until about 2 weeks ago when I decided to teach myself DirectX in C++.  I have had my fair share of errors but have (for the most part) been able to solve them myself.  I give up on this one though.  As far as I can tell the problem resides in how I am using the .h and .cpp files of the mapCube class, so I will post those.  

The errors themselves are few but infuriating:  I get a LINK:2019 unresolved external symbol error regarding all the functions of mapCube except the constructors, it tells me they are referenced in the main program but claims they are not initialized.  The second error I know of has to do with the checkColl function, in that function alone VC++ 2010 decides that x, y, and z are no longer a part of the mapCube class, it is rather perplexing.

The code: mapCube.h

#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>

extern const float TILE_WIDTH, TILE_HEIGHT;
extern LPDIRECT3DDEVICE9 d3ddev;

// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

#ifndef MAPCUBE_H
#define MAPCUBE_H

class mapCube{
        struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL; DWORD COLOR;};    //might be able to put these elsewhere
        #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)             //they are already in main, but mapCube needs them too
public:
    LPD3DXMESH cubeMesh;
    float x,y,z;
    void setCoord(float, float, float);
    D3DXVECTOR3 getCoord(){return D3DXVECTOR3(x,y,z);};
    mapCube();
    mapCube(float, float, float);
    mapCube(float, float, float, D3DXCOLOR);
    void draw(D3DXMATRIX);
    void setColor(D3DXCOLOR);
    int checkColl(D3DXVECTOR3, D3DXVECTOR3);
};

#endif

mapCube.cpp

#include "mapCube.h"

mapCube::mapCube()
{
    x=0;
    y=0;
    z=0;
    setColor(D3DCOLOR_XRGB(128,128,128));
} 

mapCube::mapCube(float nx, float ny, float nz)
{
    x=nx;
    y=ny;
    z=nz;
    setColor(D3DCOLOR_XRGB(128,128,128));
}

mapCube::mapCube(float nx, float ny, float nz, D3DXCOLOR color)
{
    x=nx;
    y=ny;
    z=nz;
    setColor(color);
}

void mapCube::setCoord(float nx, float ny, float nz)    //this function and the next one are both called
{                                                       //when the cube is created because I'm using
        x=nx;                                               //an array of cubes instead of one-by-one
    y=ny;
    z=nz;
};

void mapCube::setColor(D3DXCOLOR color)             //basically just colors each vertex 'color'
{
    LPD3DXMESH tmpMesh=NULL;

    D3DXCreateBox(d3ddev, TILE_WIDTH, TILE_HEIGHT, TILE_WIDTH, &tmpMesh, NULL);

    tmpMesh->CloneMeshFVF( 0, CUSTOMFVF, d3ddev, &cubeMesh );

    LPDIRECT3DVERTEXBUFFER9 tmpVertBuf=NULL;

if( SUCCEEDED(cubeMesh->GetVertexBuffer(&tmpVertBuf)))
{
    int nNumVerts = cubeMesh->GetNumVertices();
    CUSTOMVERTEX *pVertices = NULL;

    tmpVertBuf->Lock( 0, 0, (void**)&pVertices, 0 );
    {
            int i=0;
            while(i<nNumVerts)
            {
                pVertices[i].COLOR=color;
                i++;
            }
        }
    tmpVertBuf->Unlock();

    tmpVertBuf->Release();
    }
};

void mapCube::draw(D3DXMATRIX matWorld)
{
    D3DXMATRIX matTranslate;
    D3DXMatrixTranslation(&matTranslate,x,y,z);         //translation to the cubes stored coordinates

    d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate * matWorld));      //...combined with the total world transform
    cubeMesh->DrawSubset(0);
};

int checkColl(D3DXVECTOR3 vecTest, D3DXVECTOR3 vecThis)         //2nd arg bc compiler decided to forget
{                                                               //that this class has x,y,z vars
    if(vecTest.x>=vecThis.x-(TILE_WIDTH/2.0f) || vecTest.x<=vecThis.x+(TILE_WIDTH/2.0f))    //rudimentary attempt at collision checking
    {
        return 1;
    }
    else if(vecTest.z>=vecThis.z-(TILE_HEIGHT/2.0f) || vecTest.z<=vecThis.z+(TILE_HEIGHT/2.0f))
    {
        return 2;
    }
    else
    {
        return 0;
    }
}

Sorry if the formatting is a bit off, this is the first time I have used this interface.  The compiler reports no syntax errors or otherwise, after I change the x/z references in the last function to a passed D3D vector anyway.  Any help/criticism is welcome, whether it relates to d3d or c++.  I didn't post the main source because I do not believe the problem is there, but if asked I will post it as well.

Following the solution of that problem, I now notice this warning:     1>Debug\mapCube.obj : warning LNK4042: object specified more than once; extras ignored I removed the duplicate definitions of CUSTOMVERTEX and FVF and made them extern in the header, but am unable to resolve these problems.


參考解法

方法 1:

Just thought it is worth pointing to the bleeding obvious, but is mapCube.cpp included in your project?

方法 2:

The second issue is easily solved: the checkColl implementation (in mapCube.cpp) is missing its mapCube:: prefix. This causes the compiler to think it is a "free function", not a member function of mapCube.

方法 3:

Perhaps you forgot to add mapCube.cpp to your VC++ project. Add this line to the top of mapCube.cpp:

#error This file is indeed being compiled

If that line does not generate a compiler error, then mapCube.cpp is not being compiled.


Try Rebuild Solution. Sometimes things get out of sync, and you just have to recompile everything from scratch.

(by coderCobradoronMacEmile Cormier)

參考文件

  1. Unresolved External Symbol Occuring with Class Files (CC BY-SA 3.0/4.0)

#direct3d #C++






相關問題

OpenGL ES 或 Direct3D 中的表面究竟是什麼? (What exactly is a surface in OpenGL ES or Direct3D?)

您可以將一個進程的 Direct3D 窗口渲染到另一個進程的 HWND 中嗎? (Can you render a Direct3D window for one process into another process' HWND?)

OpenGL等圖形庫的目的是什麼? (What is the purpose of a graphics library such as OpenGL?)

Win32 的 CoreImage (CoreImage for Win32)

在 3D 圖形庫中對 I-Section 進行建模 (Modelling an I-Section in a 3D Graphics Library)

編譯器不允許我使用“DDSTextureLoader.h”和“WICTextureLoader.h” (Compiler won't let me use "DDSTextureLoader.h" and "WICTextureLoader.h")

在新紋理上切割具有圓形形狀的 Direct3D 紋理或更改部分像素的 Alpha 值 (cut Direct3D texture with circular shape on a new texture OR change Alpha value of a part of pixels)

Win10上DirectX的SDK在哪裡? (Where is the SDK for DirectX on Win10?)

GLSL / HLSL 著色器中的星球大戰全息效果 (Star Wars holographic effect in GLSL / HLSL shader)

使用 Direct3D 繪製帶有圓角末端的線條 (Drawing lines with rounded endings with Direct3D)

類文件中出現未解析的外部符號 (Unresolved External Symbol Occuring with Class Files)

錯誤,不運行程序(Windows) (error, not run program (windows))







留言討論