2011-07-28 24 views
0

第一點:我不是C++專家,遠非如此。大約一年前,我簡單地看了一遍,直到大約2周前,我決定在C++中自學DirectX時纔再次碰到它。我有我的公平份額的錯誤,但(大部分)已經能夠自己解決它們。儘管我放棄了這個。據我所知,問題在於我如何使用mapCube類的.h和.cpp文件,因此我將發佈這些文件。使用類文件發生未解析的外部符號

這些錯誤本身是少數,但真氣:我得到一個LINK 2019:關於mapCube ,除了構造函數的所有功能解析外部符號錯誤,它告訴我,他們都在主程序中,但聲稱他們是引用未初始化。我知道的第二個錯誤與checkColl函數有關,在這個函數中,VC++ 2010決定x,y和z不再是mapCube類的一部分,它很複雜。

的代碼: 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; 
    } 
} 

很抱歉,如果格式是有點過,這是第一次我已經使用這個接口。無論如何,在將最後一個函數中的x/z引用更改爲傳遞的D3D矢量之後,編譯器不會報告語法錯誤或其他情況。歡迎任何幫助/批評,無論它涉及到d3d還是C++。我沒有發佈主要來源,因爲我不相信問題出在那裏,但是如果問到我會發布它。

繼解決該問題之後,我現在注意到這個警告: 1> Debug \ mapCube.obj:警告LNK4042:對象多次指定; extras ignored 我刪除了CUSTOMVERTEX和FVF的重複定義,並將它們設置爲標題中的extern,但無法解決這些問題。

+1

在包含任何內容之前,您的'MAPCUBE_H'頭文件應位於頭文件的頂部。 –

+0

謝謝,從來沒有太多的錯誤檢查和類似的東西,所以我有點撿起它,因爲我去 – coderCobra

回答

0

也許你忘了添加mapCube.cpp到你的VC++項目。該行添加到mapCube.cpp的頂部:

#error This file is indeed being compiled 

如果該行不生成編譯器錯誤,那麼mapCube.cpp沒有被編譯。


嘗試重建解決方案。有時候,事情會不同步,你只能從頭開始重新編譯所有東西。

+0

好的和壞的一面:壞的(在解決問題的方面)=它打印錯誤好=我知道確定它正在編譯,並且措辭給了我一個非常需要的笑 – coderCobra

+0

您確實添加了mapCube。cpp作爲源文件(添加源文件)添加到您的項目中,並沒有像'#include「mapCube.cpp」'做一些傻事,對吧? –

+0

您也可以嘗試重建解決方案。有時候,事情會不同步,你只能從頭開始重新編譯所有東西。 –

0

只是認爲這是值得指出的出血明顯,但是是mapCube.cpp包括在您的項目?

0

第二個問題很容易解決:checkColl實現(在mapCube.cpp中)缺少其前綴mapCube::。這會導致編譯器認爲它是一個「自由函數」,而不是mapCube的成員函數。

+0

我想這就是我整天盯着代碼,一個小時刻...謝謝雖然。 – coderCobra