以下是我的代码(简化):
//wrapper.h
#include <play3d.h>
class wrapper
{
private:
soundengine *soundEngine;
public:
sound *playSound3D(source *source, D3DXVECTOR3 pos, bool loop=false);
};
//wrapper.cpp
sound *wrapper::playSound3D(source *source, D3DXVECTOR3 pos, bool loop)
{
return soundEngine->play3D(source, pos, loop);
};这是我的完整代码(根据要求)。它使用irrKlang声音引擎:
//irrKlang.h
virtual ISound* play3D(ISoundSource* source, vec3df pos,
bool playLooped = false,
bool startPaused = false,
bool track = false,
bool enableSoundEffects = false) = 0;//the version i want
virtual ISound* play3D(const char* soundFileName, vec3df pos,
bool playLooped = false,
bool startPaused = false,
bool track = false,
E_STREAM_MODE streamMode = ESM_AUTO_DETECT,
bool enableSoundEffects = false) = 0;//the version vc++ finds
//fsCore.h
#include <irrklang.h>
class fsEngine
{
private:
static fsEngine *instance;
static fsBool exists;
irrklang::ISoundEngine *soundEngine;
fsEngine();
~fsEngine();
public:
static fsEngine *getInstance()
{
if (!exists)
{
instance = new fsEngine();
exists = true;
return instance;
}
else
{
return instance;
}
};
void release()
{
exists = false;
delete instance;
soundEngine->drop();
};
public:
irrklang::ISoundSource *loadSound(fsString filename);
irrklang::ISoundSource *cloneSound(irrklang::ISoundSource *source, fsString alias=NULL);
irrklang::ISound *playSound2D(irrklang::ISoundSource *source, fsBool loop=false);
irrklang::ISound *playSound3D(irrklang::ISoundSource *source, D3DXVECTOR3 soundpos, fsBool loop=false);
};
//fsCore.cpp
#include "fsCore.h"
irrklang::ISound *fsEngine::playSound3D(irrklang::ISoundSource *source, D3DXVECTOR3 soundpos, bool loop)
{
return soundEngine->play3D(source, soundpos, loop);
};我不知从哪里得到了一部C2664 error。
1>c:\users\...\documents\visual studio 2008\projects\core\fscore.cpp(20) : error C2664: 'irrklang::ISound *irrklang::ISoundEngine::play3D(const char *,irrklang::vec3df,bool,bool,bool,irrklang::E_STREAM_MODE,bool)' : cannot convert parameter 1 from 'irrklang::ISoundSource *' to 'const char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style castPlay3D()有两个定义:一个接受'const char *'作为参数1,另一个接受'source *'作为参数1。智能感知指出了这两个定义,但我无法让VC++ 2008 express编译器识别我想要的版本。我要改变什么?
发布于 2013-11-10 12:48:49
您的代码有两个问题,都在注释中指出。
#1,来自@john:
virtual ISound* play3D(ISoundSource* source,
vec3df pos,
bool playLooped = false,
bool startPaused = false,
bool track = false,
bool enableSoundEffects = false) = 0;
irrklang::ISound *fsEngine::playSound3D(irrklang::ISoundSource *source,
D3DXVECTOR3 soundpos, bool loop)
{
return soundEngine->play3D(source, soundpos, loop);
}调用方传递一个D3DXVECTOR3,但被调用方需要一个vec3df。它们是不同的类型,并且它们之间没有隐式转换,所以编译器会给你一些错误。在这种情况下,它给出的特定错误可能会有一点误导性。
(这两个类的大小相同并且具有相同顺序的相同数据成员,这并不重要。与how they happen to be implemented internally.无关的C++ only treats two types as equal if they equal, )
#2,来自@Named:
即使D3DXVECTOR3和vec3df是完全相同类型的typedefs,您也会遇到另一个问题。您的两个虚拟方法具有相同的名称,因此它们是重载;而且由于函数重载与虚拟重载的交互方式,子类的重载实际上隐藏了父类的版本。示例:
struct X; struct Base { virtual void foo(X *); };
struct Y; struct Derived : public Base { virtual void foo(Y *); };
void f(Derived *d, X *x) { d->foo(x); }
% clang++ -Wall test.cc
test.cc:2:59: warning: 'Derived::foo' hides overloaded virtual function [-Woverloaded-virtual]
struct Y; struct Derived : public Base { virtual void foo(Y *); };
^
test.cc:1:59: note: hidden overloaded virtual function 'Base::foo' declared here
struct X; struct Base { virtual void foo(X *); };
^
test.cc:4:39: error: cannot initialize a parameter of type 'Y *' with an lvalue of type 'X *'
void f(Derived *d, X *x) { d->foo(x); }
^
test.cc:2:66: note: passing argument to parameter here
struct Y; struct Derived : public Base { virtual void foo(Y *); };
^解决此问题的一种方法是使用using指令:
struct X; struct Base { virtual void foo(X *); };
struct Y; struct Derived : public Base { virtual void foo(Y *); using Base::foo; };
void f(Derived *d, X *x) { d->foo(x); }当我们在涉及Base::foo的上下文中查找foo时,这会将Derived重新添加到候选列表中。然而,一种更好的解决方案是对唯一功能使用唯一标识符:
struct X; struct Base { virtual void foox(X *); };
struct Y; struct Derived : public Base { virtual void fooy(Y *); };
void f(Derived *d, X *x) { d->foox(x); }这里没有混淆你想要调用哪个函数-编译器没有混淆,人类读者也没有混淆。C++中的函数重载是一个用来解决特定问题的工具。If you don't have that problem, don't use that tool!
https://stackoverflow.com/questions/16364461
复制相似问题