accessing GetTransform() property - cocos2d-iphone

How do I access the GetTransform property of a body which was created in another class? This is what I did, I created a distance joint.
distanceJointBodies.Initialize(b, secBody, b->GetTransform(), secBody->GetTransform());
I get an error saying:
"No viable conversion from 'const b2Transform' to 'const b2Vec2' "
pointing to the distance joint parameter:
b->GetTransform()
where b is a body I created in another class.

I think you need GetPosition() function, not GetTransform(), since you have to pass b2Vec2 to your function

Related

Understand C++ const functions

I am new to c++ and im trying to write a simple c++ wrappers to integrate with this third party c++ library; which comes with bad documentation.
function to integrate with (this is all the documentation that came with this function):
virtual ImageCoord Raster::groundToImage(const XyzCoord & groundPt,
double desiredPrecision=0.001,
double* achievedPrecision=null) const
Text about function: This method converts the given groundPt (x,y,z in meters) to a returned image coordinate (line, sample in full image space pixels).
there is also some class documentation
Raster Class Ref
inherits from GeoModel
Public member functions:
virtual ImageCoord groundToImage (const XyzCoord &groundPt, double desiredPrecision=0.001, double *achievedPrecision=NULL) const =0
in my code i have:
//this is implemented correctly
const XyzCoord xyz(284971.17549099098, -126866.36533847413, 6350003.627515804)
double desiredPrecision = 0.000001;
double achievedPrecision = 0.0;
// not sure if this is what the documentation meant by "image coordinate" but it comes with the library
// ImageCoord(double line, double point)
ImageCoord imagePoints;
// the part im confused about, what is the proper way to invoke the above function, the below line was me trying out how to call the method
const Raster::groundToImage(&xyz, imagePoints);
Sorry for my ignorance in c++ but I've been baffled. I have lot of programing experience (8 plus years, just none with c++ so i understand programing terms, concepts and design patterns).
Im also trying to understand in the function defination what does this mean
const XyzCoord & groundPt
I was able to compile with
XyzCoord xyz(204971.17549099098, -106866.36533847413, 6350003.627515804);
Raster* service;
ImageCoord imagePoints = service->groundToImage(xyz); //segmentation error occurs on this line
but i then get a runtime error "Segmentation fault"
This is a non-static member function of a class named Raster.
You are supposed to invoke it via member access obj.groundToImage(/*args*/) where obj is an object of the class type to which the function belongs or a class type derived from that class.
Or, if the call happens inside another non-static member function of the same class or a derived class, it could just be groundToImage(/*args*/) which will call the function implicitly on the current object.
With virtual it may also be possible to invoke the function on an object of a base class of Raster, depending on where the virtual function has been declared first in the class hierarchy.
There are certain more specialized situations where a qualified call replacing groundToImage with Raster::groundToImage in either of the above could also be the intended behavior.
The const qualification of the function is irrelevant. It just means that the function can be called whether obj is const-qualified or not. Similarly the const-qualification on the function parameter is irrelevant. It just means that you can pass either a const- or non-const-qualified first argument. You don't have to make xyz const to be able to pass it. Only the other way around, passing a const-qualified expression to a non-const reference parameter is a problem. const is intended to signal that the function will not modify the argument and therefore doesn't care whether or not it is const-qualified.
const only makes sense when used in a declaration or type. Something like
const Raster::groundToImage(&ecef, imagePoints)
as a supposed function call doesn't make syntactical sense.
The function also expects up to three arguments of the specified types and returns a ImageCoord. You are not supposed to pass one as an argument. It is what the function returns.
The arguments should probably be xyz, desiredPrecision and &achievedPrecision given that you already declared them with the correct types.
It probably wants the last one as pointer because it is an out-parameter.
What the object on which the member function is called on is supposed to be is unclear from what you have shown. We don't know what ecef is though...
TL;DR:
ImageCoord imagePoint = someRasterObject.groundToImage(
xyz,
desiredPrecision,
&achivedPrecision
);
Or
ImageCoord imagePoint = somePointerToRasterObject->groundToImage(
xyz,
desiredPrecision,
&achivedPrecision
);
From the signature given:
virtual // A derived class's implementation of
// this function can be called via a
// pointer or reference to a parent
// class object
ImageCoord // This function returns an ImageCoord
// object
Raster:: // This is a member of the Raster class
groundToImage( // This function is named groundToImage
const XyzCoord & groundPt, // The first argument to this function is a
// reference to a constant XyzCoord
// object
double desiredPrecision=0.001, // The second argument is a double with a
// default value of 0.001 if not provided
double* achievedPrecision=null // The third argument is a pointer to a
// double with a default value of null if
// not provided
)
const // This function can be called on a
// constant Raster object
That means you need 2-4 things to call this function:
A (possibly const-qualified) Raster object to call the function on
An XyzCoord object to pass as the first parameter
(Optional) A double to pass as the second parameter
(Optional) A pointer to a double to pass as the third parameter
While nothing in your question explicitly states it, I would assume the function uses the 3rd parameter as an output. I would assume it writes the actually achieved precision to the double pointed to by the pointer you pass it, so you'll probably want to just pass it the address of a local double variable.
Each non-static method in a class is called on behalf of some object of that class (or some derived class), and the object is accessible within a method by an implicitly defined this pointer.
The const qualifier appended after the parameters' list of the method applies to that this value. In other words, it declares this of a type classname const* instead of classname*.
As a result the compiler will reject any attempts to modify the *this object from within the const-qualified method, so the method can be safely used on non-modifiable objects.
For example, the length() method of the std::string class is declared as
size_t length() const;
so when you use it like, say:
std:string s;
....
size_t len = s.length();
you can be sure the s variable will not be modified during calculation of len value.

Bizarre error on address of class with templated member

I encountered a weird error when modifying my class structures, which I couldn't reproduce with simpler classes.
Situation:
I have decided to have a class Input have a reference (a plain old pointer) to the object to be manipulated by the user's key input, Camera. Previously I linked them via Input's constructor: Input in(&cam); but after including a templated member in Camera: Buffer<Layout> buffer my whole main function went berserk.
Previously this was fine:
Camera cam();
Input in(&cam);
cam.setProj(glm::mat4(1.0f)); // Example of function call
The headers are of course in separate files, and the implementations in .cpp-files.
Input has a member Camera *cam and the constructor for Input is:
Input::Input(Camera *camera){
this->cam = camera;
}
Problem:
It no longer is fine. The error message is:
Input::Input(Input &&): cannot convert argument 1 from
'Camera(__cdecl *)(void)' to 'Camera *'
Additionally, every attempt to call Camera's functions gives two additional errors: expression must have class type and left of '.func' must have class/struct/union.
Attempt to create a minimal example:
Tl;dr: didn't quite get there. Or at least the issue isn't directly in taking the address of a class with a template member. The following code works.
#include <stdio.h>
#include <stdlib.h>
template <typename T>
class Member{
public:
Member(T variable):var(variable){};
T var;
};
class Owner{
public:
Owner(int m):member(m){};
Member<int> member;
};
class Referencer{
public:
Referencer(Owner *o){this->op = o;};
Owner *op;
};
int main(){
Owner o(1);
Referencer r(&o);
printf("%d\n", r.op->member); // Output is 1.
return 0;
}
As the above example works, I'm not sure whether it actually affects the situation to have a template member. I'm really lost with this one. Any help is greatly appreciated!
Camera cam();
It is a simple function declaration. You declare a function returning Camera and accepting zero arguments. And your MSVC compiler complains about it!
Input::Input(Input &&): cannot convert argument 1 from 'Camera(__cdecl *)(void)' to 'Camera *'
Just use:
Camera cam;
and let the default constructor to be called.

glut - Request for member which is non-class type

I have a class MouseController. There is a method - update.
void MouseController::update(int x, int y) {
this->mX = x;
this->mY = y;
this->diffX = mX - 650;
this->diffY = mY - 350;
calculateAngle();
}
I'm using glut. I want to make glutPassiveMotionFunc and to put that update function.
glutPassiveMotionFunc(mouse.update);
I get following error :
D:\projects\cpp\glut\main.cpp|129|error: request for member 'update'
in 'mouse', which is of non-class type 'MouseController()'
Problem
By defining mouseas
MouseController mouse();
you define a function taking no argument, returning a MouseController, called mouse. Then, when you call
glutPassiveMotionFunc(mouse.update);
you try to access the member update of the function mouse. Hence the error message.
Solution
MouseController mouse;
(would only work if MouseController::update(int,int) werestatic, which it is not.)
Real solution
MouseController mouse;
glutPassiveMotionFunc([&](int x, int y){mouse.update(x, y)});
Here is the signature for glutPassiveMotionFunc
void glutPassiveMotionFunc(void (*func)(int x, int y));
As you can see it takes in a function pointer with two ints as arguments. In C++, you cannot get a pointer to a non-static member function because non-static member functions have the implict first parameter this which is unique to each instance of the class.
What you could try is storing a function pointer within MouseController that points to the update function and then pass that into glutPassiveMotionFunc
This would give you a way to get a pointer to the function but would still not match the signature of the function pointer needed to be passed in to glutPassiveMotionFunc. Look at the other responder's solution for a lambda to do that.

How to properly pass member function as argument in this situation in C++?

I want to pass a member function of my C++ class to another member function of the same class. I did some research and found these similar questions on SO.
Passing a member function as an argument in C++
Function pointer to member function
They don't cover my specific case in an identical manner, but I wrote my code and would think that I adjusted the right parts to make it work in my situation. However, the compiler seems to disagree with me on that...
I have the following setup in my C++ class:
CutDetector.h
class CutDetector {
double thresholdForFrameIndex(int frameIndex, vector<double> diffs, int steps, double (CutDetector::*thresholdFunction)(vector<double>diffs)); // should take other functions as args
double calcMean(vector<double> diffs); // should be passed as argument
double calcMeanMinMax(vector<double> diffs); // should be passed as argument
double calcMedian(vector<double> diffs); // should be passed as argument
}
CutDetector.h
double thresholdForFrameIndex(int frameIndex, vector<double> diffs, int steps, double (CutDetector::*thresholdFunction)(vector<double>diffs)) {
vector<double> window = ... init the window vector ;
double threshold = thresholdFunction(window);
return threshold;
}
However, passing the thresholdFunction as an argument like this doesn't work. The compiler complains with the following error:
error: called object type 'double (CutDetector::*)(vector<double>)'
is not a function or function pointer
Can anyone see why my setup doesn't work and suggest how I can make it so that it works? Basically what I want is to be able to pass any member function that calculates a threshold (i.e. calcMean, calcMeanMinMax, calcMedian) to the other member function thresholdForFrameIndex.
To invoke a pointer to member function, you need to supply an object:
double threshold = (this->*thresholdFunction)(window);
^^^^^^^^ ^
You can't call a member function without an instance of the class. You need to do something like this:
CutDetector cd;
double threshold = (cd.*thresholdFunction)(window);
Or if you have a CutDetector pointer somewhere:
double threshold = (pcd->*thresholdFunction)(window);
Or if thresholdForFrameIndex is a member function:
double threshold = (this->*thresholdFunction)(window);
I think it would be easier for you here to make calcMean, calcMeanMinMax and calcMedian static functions and treat like all others non-member functions. Others answers are correct, but in your case i guess that would be better for class design.

How to give an event handler access to member data?

myWebClient->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler( &Form1::DownloadProgressCallback );
gives the error:
1>.\Form1.cpp(26) : error C3352: 'void Form1::DownloadProgressCallback(System::Object ^,System::Net::DownloadProgressChangedEventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::Net::DownloadProgressChangedEventArgs ^)'
The documentation uses a static non member function as this delegate parameter, but I want to update a progress bar member of the Form1 class. What am I doing wrong here?
I am using .NET 2.0 so please keep the language as antiquidated as possible.
The delegate for a member function would be declared (within the form):
myWebClient->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler(this, &Form1::DownloadProgressCallback );
Basically, the first argument is the object one which the member function is defined. In this case, this should work.
Pass the object to the delegate constructor.
gcnew DownloadProgressChangedEventHandler( this, &Form1::DownloadProgressCallback );