I am working on one of the STK programs using a Sine Oscillator callback. I am having issues when creating an object from my ToneGen class that inherits from the Generator Class due to a Virtual function in the Generator class that causes my ToneGen class to abstract. i Have tried pointers but it seems to be causing issues getting the data to the appropriate method. If i use pointers my code breaks here in the ToneGen.h file
void setRate( StkFloat rate ) { rate_ = rate; };
Otherwise without pointers i get this error
src\crtToneGen.cpp(36): error C2259: 'stk::ToneGen' : cannot instantiate abstract class
due to following members:
'stk::StkFrames &stk::Generator::tick(stk::StkFrames &,unsigned int)' : is abstract
C:\VS10 Projects\StkNewInst\crtToneGen\include\Generator.h(43) : see declaration of 'stk::Generator::tick'
Here is the Virtual Function in the Generator Class
virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 )=0;
Is there anyway to avoid this, i have tried several other techniques on other post but have not had any luck yet.
The Code i am modifying can be found here
I am doing this on VisualStudio 2010 windows 7 32 bit
stk::StkFrames &stk::Generator::tick(stk::StkFrames &,unsigned int)
Is abstract, and needs to be implemented in your derived class
class ToneGen: public stk::Generator
{
stk::stkFrames& tick(stk::StkFrames& frames,unsigned int something)
{
return stk::stkFrames(); // Don't do this, return something useful.
}
}
You have to implement this function in your derived class as the base class provides no functionality for this method. It's declaration in the stk header would be:
stk::StkFrames &stk::Generator::tick(stk::StkFrames &,unsigned int) = 0;
Related
I am new in Qt.
I am trying to add texture to the example project "basicshapes", which comes from Qt Creator demo.
It is written in C++ that'w perfect, because it is my need.
There are used classes such as:
Qt3D::QTransform
Qt3D::QSphereMesh
Qt3D::QPhongMaterial
and many others
but I can not realize how to add texture to it.
There is a fragment:
Qt3D::QPhongMaterial *sphereMaterial = new Qt3D::QPhongMaterial();
sphereMaterial->setDiffuse(QColor(QRgb(0xa69929)));
so I was trying to add:
MyTextureImage *t = new MyTextureImage();
MyTextureProvider *x = new MyTextureProvider();
x->addTextureImage(t);
sphereMaterial->setTextureParameter("SphereTexture", x);
before I have derived from abstract classes:
class MyTextureProvider : public Qt3D::QAbstractTextureProvider { };
class MyTextureImage : public Qt3D::QAbstractTextureImage { };
but I got error:
error: C2259: 'MyTextureImage' : cannot instantiate abstract class
due to following members:
'Qt3D::QNode *Qt3D::QNode::doClone(void) const' : is abstract
I am not an expert of Qt, however by looking at the compiler error, you would need to override the doClone method because it is qualified as pure virtual.
More information on your compiler error can be found on MSDN: https://msdn.microsoft.com/en-us/library/zxt206sk.aspx
I hope this helps.
I am having trouble passing a template class as a parameter to a another function.
I am using VS2012 c++/cli on a Windows 8.1 machine compiling for x64.
The compiler keeps telling me:
void Channel::TestFunc(SynchQueue<T> *)' : overloaded member function not found in 'Channel'
SynchQueue is a template class for a multi-threaded queue. I created it with another class I will call Images.
In my main.cpp, I have:
QPtr = new SynchQueue<Images>;
Also in main.cpp, I created a class called WorkerThread to which I passed QPtr.
No problems with that.
Now I want WorkerThread to pass QPtr to another class that is instantiated in WorkerThread.
So I defined the function as:
Channel.h
public ref class Channel
{
public:
// other definition stuff
void TestFunc(SynchQueue<Images> *tQPtr);
}
Channel.cpp
void Channel::TestFunc(SynchQueue<Images> *tQPtr)
{
int x;
x++;
}
I keep getting the error above. What am I doing wrong?
Any help appreciated.
You need:
template<typename T>
void TestFunc(SynchQueue<T> *tQPtr);
I am writing a C++ WinRT Component DLL for use in my .NET-based WinRT application. The DLL defines a SoundSample ref class that creates an XAudio voice by calling IXAudio2::CreateSourceVoice. CreateSourceVoice takes a "IXAudio2VoiceCallback *pCallback" parameter to enable callbacks on various audio events. Now I am trying to implement that callback based on this article. XAudio will supposedly just call back into methods of my SoundCallback class defined as:
#pragma once
#include "xaudio2.h"
#include "pch.h"
class SoundCallback
: public IXAudio2VoiceCallback
{
private:
//SoundSample^ sample; //does not compile
public:
SoundCallback(void);
~SoundCallback(void);
//Called when the voice has just finished playing a contiguous audio stream.
void OnStreamEnd();
void OnVoiceProcessingPassEnd();
void OnVoiceProcessingPassStart(UINT32 SamplesRequired);
void OnBufferEnd(void * pBufferContext);
void OnBufferStart(void * pBufferContext);
void OnLoopEnd(void * pBufferContext);
void OnVoiceError(void * pBufferContext, HRESULT Error);
};
Everything is fine until I try to figure out how to call back from an instance of my native callback class to the parent SoundSample object. I was thinking I could pass an instance of the SoundSample class to the SoundCallback object, but it seems like it does not allow me to declare a ref class field in the native class:
SoundCallback.h(9): error C2143: syntax error : missing ';' before '^'
SoundCallback.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SoundCallback.h(9): error C3699: '^' : cannot use this indirection on type 'int'
I looked back at implementing callbacks in native C++ and I could not find a reasonable solution so far. What is the best/easiest way to do this?
Solved it (thanks to Jeremiah Morrill) - the problem is not with any barrier blocking the use of ref classes in basic classes. C4430 means that SoundSample is an unrecognized type, which was hidden by Intellisense - since that seemed to indicate that SoundSample is known.
What needs to be added is a declaration of the SoundSample type and this all starts working fine.
I just added
namespace MyNamespace { ref class SoundSample; }
before the SoundCallback class declaration and then SoundCallback class could declare:
MyNamespace::SoundSample^ sample;
Here's interface that I have declared:
[ServiceContract]
public interface class IShedluer
{
[OperationContract]
array<Object^>^ GetResult(UInt64 taskId);
}
Here's the class that is trying to implement it:
ref class MyShedluer:IShedluer
{
Shedluer ^shedluer;//this is NOT MyShedluer
public:
MyShedluer(void);
array<Object^>^ GetResult(UInt64 taskId)
{
return shedluer->GetResult(taskId);
}
}
When I'm trying to compile this, I'm getting
Error 15 error C3766: 'MyShedluer' must provide an implementation for
the interface method 'cli::array<Type> ^IShedluer::GetResult(unsigned __int64)'
d:\users\menkaur\documents\visual studio 2010\projects\MyProject\
\kernel\MyShedluer.h 78 1 MyProject.Kernel
Why am I getting this?
the correct syntax for implementing an interface is to add virtual:
ref class MyShedluer:IShedluer
{
public:
virtual array<Object^>^ GetResult(UInt64 taskId);
}
Also the compiler tells you this, look at your warnings as well:
warning C4488: 'MyShedluer::GetResult' : requires 'virtual' keyword
to implement the interface method 'IShedluer::GetResult'
I'm a beginner in C++, for this game project that I am creating what I'd like to have is a collection of generic "GameObject" element, each with virtual methods for Update and Draw...then have classes inheriting from it with their own definitions of such methods so that when updating the world I only need to keep one collection to cycle through...
It seems like it is impossible with Vector to hold a collection of abstract classes, I tried instead to keep the Draw and Update methods virtual, but then if I create an Enemy : public GameObject instance and add it to a Vector<GameObject> and call update then I get all sorts of Linker: undefined symbol errors....
I don't know, there's something I am not grasping.... I am sure this is a type of setting that many application would require.... what is your advice on approaching this? Should I maybe try a different type of collector than Vector?
Thank you very much
EDIT:
In response to the replies below, I tried:
class GameObject{
....
virtual void update(float t):
....
}
//-------------------------------------------
class Enemy: public GameObject{
.....
void update(float d);
.....
void Enemy::update(float d){
//Have a breakpoint here
}
//-------------------------------------------
class World{
...
vector<GameObject*> gameObjects;
...
}
//-------------------------------------------
class Level{
World levelWorld;
....
Level(){
levelWorld = new World();
}
...
Enemy* en = new Enemy();
levelWorld->visibleObjects.push_back(en);
levelWorld->visibleObjects.at(0)->update(0);
}
And I get, as I did before:
Error 25 error LNK2001: unresolved external symbol "public: virtual void __thiscall Enemy::update(float)" (?update#Enemy##UAEXM#Z)
Error 28 error LNK2001: unresolved external symbol "public: virtual void __thiscall GameObject::update(float)" (?update#Object3DS##UAEXM#Z)
....
Any suggestions?
As stated in Effective C++(Scott Meyers), you should not create arrays of polymorphic types. In your case, using a vector<GameObject*>will work. But I would advise using auto_ptr or any other smart pointer.
Use Vector<GameObject*> (or something that involves references/pointers) instead of Vector<GameObject>. You aren't allowed to instantiate abstract base classes.
If you want subtype polymorphism, you need a vector of pointers. Otherwise, your objects will get sliced. Since a vector of raw pointers is not exception safe, I recommend a vector of shared pointers:
#include <vector>
#include <memory>
// ...
std::vector<std::shared_ptr<GameObject>> gameObjects;
gameObjects.push_back(std::make_shared<Enemy>());