类文件中出现未解析的外部符号
第一点:我不是 C++ 专家,远非如此。大约一年前,我简单地浏览过它,直到大约两周前,当我决定自学 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:多次指定对象;附加内容被忽略 我删除了 CUSTOMVERTEX 和 FVF 的重复定义,并将它们添加到标头中,但无法解决这些问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您忘记将mapCube.cpp 添加到您的VC++ 项目中。将此行添加到mapCube.cpp 的顶部:
如果该行没有生成编译器错误,则不会编译mapCube.cpp。
尝试重建解决方案。有时事情会不同步,您只需从头开始重新编译所有内容。
Perhaps you forgot to add mapCube.cpp to your VC++ project. Add this line to the top of mapCube.cpp:
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.
只是认为值得指出明显的出血问题,但是您的项目中是否包含mapCube.cpp?
Just thought it is worth pointing to the bleeding obvious, but is mapCube.cpp included in your project?
第二个问题很容易解决:
checkColl
实现(在mapCube.cpp
中)缺少其mapCube::
前缀。这导致编译器认为它是一个“自由函数”,而不是mapCube
的成员函数。The second issue is easily solved: the
checkColl
implementation (inmapCube.cpp
) is missing itsmapCube::
prefix. This causes the compiler to think it is a "free function", not a member function ofmapCube
.