How to implement an interface in managed c++? - c++

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'

Related

I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ?
My production code : prod_code.cpp
int TargetTestClass::targetFunc()
{
if(externalCFunc() == True)
{
statement1; statement2; statement3; /// and so on
}
}
My testcode.cpp generally contains tests like this
//Fixture for Target Test class
class TargetTestClassFixture : public testing::Test {
TargetTestClass* targetTestClassPtr;
void SetUp() {
targetTestClassPtr = new TargetTestClass();
}
void TearDown() {
delete targetTestClassPtr;
}
};
TEST_F (unitTest, test_001)
{
targetTestClassPtr->targetFunc(); //Need to do something here so that externalCFunc() returns true on call
}
What you can do is to create a source file like my_c_stubs.c where you rudimentary implement your C function. For example, the implementation can just return true. Then don't link original source file with the external C function but rather use your stub file. You should still use the original C header. In this way you won't be able to stub inline functions though. If it is required, some more sophisticated approach is needed.
I found 2 solutions to my problem so I am going to answer the same here.
Solution 1 : This involved changing the target source code. Basically you need to write a wrapper that calls the external C functions like below
Class TargetTestClass{
protected:
int targetFunc();
virtual int externalCFuncWrapper(); // Wrapper
};
//call the external C function from the wrapper
int TargetTestClass::externalCFunctionWrapper(){
return(externalCFunc());
}
//Definition of targetFuc in original question
//Now write a mock class for Target Test Class as usual and mock the wrapper function to return what you want to
class MockTargetTestClass : public TargetTestClass{
public: MOCK_METHOD0(externalCFunctionWrapper, int());
};
//Now use the Mock class as needed
TEST_F ( TargetUnitTest, TestingExternalCFuctionCall)
{
MockTargetTestClass mockTargetTestClassObj;
using ::testing::Return;
using ::testing::_;
Expect_Call(mockTargetTestClassObj, externalCFunctionWrapper())
.WillOnce(Return(1));
Assert_EQ(mockTargetTestClassObj.targetFunc(), 1);
}
Solution 2 : Thanks to #kreynolds, I have looked into Fake Function Framework and implemented as follows :
Class TargetTestClass{
protected:
int targetFunc();
//No Code change needed in target source code
};
//In testcode.cpp
#include <gmock-global/gmock-global.h>
MOCK_GLOBAL_FUNC0(externalCFunc, int());
TEST( Unittest, test002){
using ::testing::Return;
using ::testing::_;
EXPECT_GLOBAL_CALL(externalCFunc, externalCFunc()).WillOnce(Return(1));
TargetTestClass targetFunc; //This does not contain any wrapper
EXPECT_EQ(targetTestClassObj.targetFunc(), 1);
}
I am using the second solution as this does not require any change in my source code and easier to use.
Once again thank you everyone for giving your time.

Cannot get override method in derived (googlemock-fixture) class with fallback to base fixture class to work

I'm fairly new to C++, more used to C and python, but google was not my friend this time.
I have a baseclass which implements some googlemock fixtures implemented in 2 generic classes (a baseclass and a parameterized class which derives from that baseclass) and a TEST_P function using the parameterized fixture.
Since the parameterized fixture is used to test 2 implementations I put the fixture-classes and the TEST_P in a baseclass cpp-file (+ hpp file) and the specific parameter-tables in 2 specific cpp files.
This works like a charm.
But... now we want to extend one of the implementations so that it needs a specific implementation of a function which is defined in the generic baseclass. For the other implementation the generic implementation is still correct.
So what I want is to override the generic implementation for only 1 of the 2 test-cpp files.
Initially (without override) I had the following in the hpp:
class MyBaseClass : public ::testing::Test
{
protected:
// boring fixture setup stuff
virtual void test( void );
}
class MyParametrizedBaseClass:
public MyBaseClass,
public::testing::WithParmInterface< std:tr1::tuple< /* parameter definition */ > >{
protected:
// some custom assert method definitions
}
baseclass.cpp file:
MyBaseClass::test( void )
{
cout << "This is the baseclass implementation" << std::endl;
}
...
TEST_P( MyParametrizedBaseClass, TestName )
{
test();
// other googlemock checks not needed for this question
}
And in the 2 specific implementations I have just the parameter tables:
std:tr1::tuple< /* parameter definition */ > const TableName[] = {
//table implementation
}
INSTANTIATE_TEST_CASE_P( Name, MyParameterizedBaseClass, ::testing::ValuesIn( TableName ) );
Again up to now this works.
Now for ... lets call it impl_1 ... we want test to do the following (this code does not work since the definition for MyParameterizedBaseClass::test is not found):
impl_1.cpp:
void MyParameterizedBaseClass::test( void )
{
cout << "This is the impl_1 implementation" << std::endl;
}
The first thing I did after seeing that the compiler misses the definition of MyParameterizedBaseClass::test was replace MyParameterizedBaseClass with MyBaseClass, but then it sees it as a redefinition (which it indeed is).
So next I googled and found the 'override' key word and adapted the MyParameterizedBaseClass definition so it looks like this:
class MyParametrizedBaseClass:
public MyBaseClass,
public::testing::WithParmInterface< std:tr1::tuple< /* parameter definition */ > >{
protected:
// some custom assert method definitions
void test( void ) override;
}
But when I do that I get the error:
Baseclass.hpp:[linenumber]: error: expected ';' before 'override'
And in the specific implementation for impl_1 it still says that no test function is declared in MyParameterizedBaseClass
And if I (knowing up front that it is wrong) put the ';' before 'override' as suggested, I get: error: ISO C++ forbids declaration of 'override' with no type
Next I removed the 'override' keyword and recompiled.
Now the compiler is satisfied, but the linker fails on impl_2 since there I don't have an implementation of MyParameterizedClass::test.
And that is true, since I want to have the default implementation of test for that.
(To complete the description: Yes it works again if I also implement test for MyParameterizedBaseClass in impl_2.cpp)
So my question is: what am I doing wrong?
Environment: Linux with GCC 4.4.2 (pretty old I know, but that is the version my company has)
Adding declaration in MyParametrizedBaseClass was a good move.
Problem is that you have used C++11 feature override and based on code like this:
std:tr1::tuple< /* parameter definition */ >
(tr1) indicates that you are using C++03 with "technical report one", so keyword override is unknown. That is why compiler complains: error: expected ';' before 'override'
So just remove override keyword and it should be fine.

How to add texture to the "basicshapes" exampler from qt3d demo

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.

STK callback oscillator issue with virtual function

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;

Source SDK Compile Errors

I am making a Counter Strike mod and during compiling I am getting some errors:
Panel.cpp(715): error C2248: 'CInput::CVerifiedUserCmd' : cannot access private class declared in class 'CInput'
1> \SDK\\game\\client\\input.h(238) : see declaration of 'CInput::CVerifiedUserCmd'
1> \SDK\\game\\client\\input.h(39) : see declaration of 'CInput'
Line 715:
CInput::CVerifiedUserCmd* ver = NULL;
Declaration:
class CVerifiedUserCmd
{
public:
CUserCmd m_cmd;
CRC32_t m_crc;
};
How do I fix this?
You're probably trying to use a private inner class:
class A
{
class B
{
};
};
Simply make the class public if you wish to use it outside:
class A
{
public:
class B
{
};
};
EDIT:
If the class is private and it's part of a 3rd party lib, you're probably doing it wrong. Look for a different solution to your problem, it was made private for a reason.
Assuming that was your code put class
CVerifiedUserCmd
to public section of the outer class. Otherwise you cannot use CVerifiedUserCmd since it is private inner class.
You probably can't (unless you want to edit the engine itself) - look for a better solution to your problem. Basically, don't try to manually instantiate CInput::CVerifiedUserCmd.