这是我的代码(简化):
//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 cast
Play3D()有两个定义:一个接受'const char *'
作为参数1,另一个接受'source *'
作为参数1. Intellisense指出两个定义,但我不能得到VC ++ 2008快递编译器识别我想要的版本。我该怎么改变?
答案 0 :(得分:0)
您的代码存在两个问题,两者都在评论中标明。
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
。这些是不同的类型,它们之间没有隐式转换,因此编译器会给你一些类错误。在这种情况下,它给你的特殊错误可能会有点误导。
(两个类的大小相同并且具有相同顺序的相同数据成员并不重要.C ++ only treats two types as equal if they are equal,,无论how they happen to be implemented internally.}
即使D3DXVECTOR3
和vec3df
是完全相同类型的typedef,您还有另一个问题。您的两个虚方法具有相同的名称,因此它们是重载;并且由于加载的功能如何与虚拟的骑行交互,子类的重载实际上隐藏了父类的版本。例如:
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!