I'm to use C++ for a very small part of my project. I must be coding something wrong, but my knowledge of C++ is what it is and I can't get around this...
See both the AbstractContactListener.h and .mm files below. The problem is in isFixtureCollidingWithFixtureOfType(...) method, I can't access the _contact vector. What could I be doing wrong here?
header:
struct JRContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const JRContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
class AbstractContactListener : public b2ContactListener {
id contactHandler;
public:
std::vector<JRContact>_contacts;
AbstractContactListener(id handler);
~AbstractContactListener();
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type);
virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
};
Implementation:
AbstractContactListener::AbstractContactListener(id handler) : _contacts() {
contactHandler = handler;
}
AbstractContactListener::~AbstractContactListener() {
}
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){
std::vector<JRContact>::iterator ct;
// Next line is faulty... can't call _contacts.begin()
// xCode says: "Use of undeclared identifier _contacts"
ct = _contacts.begin();
}
void AbstractContactListener::BeginContact(b2Contact* contact) {
// ...
}
void AbstractContactListener::EndContact(b2Contact* contact) {
// ...
}
Undeclared? Hmm. I thought I was declaring it in the header, right after the "public:" keyword.
What could I be doing wrong here?
thanks a lot!
J.
You forget to add the scope of the function. Try:
void AbstractContactListener::isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){
Why is the error pointing you to that strange place? The compiler sees your function definition and thinks that this is a free function, as there is nothing that indicates otherwise and tries to handle it as such. It fails, because it tries to find the variable in the global scope. This can get even funnier (read: more confusing): Image that this function does not use a class member. It will be simply parsed and compiled as a free function. As soon as your try to call it on an object of that type you will get a linker error.
Also, I cannot see a declaration of the type id which is used in AbstractContactListener but that might just be because the code sample is incomplete.
You forgot the class name from
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type)
void AbstractContactListener::isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type)
In the implementation.
:)
Related
I need to put all my code in ram (I'm writing the flash). I'm using IAR 7.80 and everything works fine with the __ramfunc intrinsic on every function but not for the C++ constructors.
For example I have the following class:
class Os_Timer {
private:
os_tmrcnt_t tmr;
public:
__ramfunc Os_Timer() { reset(); }
__ramfunc void reset() { os_TimerStart( &tmr ); }
};
I haven't find a way to define the constructor Os_Timer in ram. The compiler complains
expected an identifier
and
object attribute not allowed
on the constructor line.
The IAR manual says that the __ramfunc has to be placed before the return value but the constructor doesn't have a return value.
I have tried without success to force the __ramfunc behaviour:
_Pragma("location=\"section .textrw\"")
and
_Pragma("location=\"RAM_region\"")
Someone know how to do it?
To apply __ramfunc to a C++ constructor you have to use _Pragma("object_attribute=__ramfunc") as in the example below.
class Os_Timer
{
private:
os_tmrcnt_t tmr;
public:
_Pragma("object_attribute=__ramfunc") Os_Timer() { reset(); }
__ramfunc void reset() { os_TimerStart(&tmr); }
};
Note that it for this to work os_TimerStart should also be declared as __ramfunc, otherwise os_TimerStart will be placed in flash and may be overwritten by your flash update. To help you detect this the compiler will issue a warning if you try to call a function that is not declared as __ramfunc from a function that is.
So, I want to know how I can call functions from header...
To explain it better, I have a header file, which is something similar to a game library.
I want to call certain functions in the Main part of the program(the dynamic one), but I want these functions not to be required.
So, if I click a hotkey combo, I'd call a function in Main part of the program called "hotkeyHit", but first I need to check if it's there. I'm implementing some kind of an "Event system".
And that's where I hit the wall...
First of all I don't know how to check if there is that function in the Main, I can't compile if there is no function called that and last of all, I can't declare the same function for second time, which leaves me with no obvious options.
So now I'd like to know how I can do that so it would work the way I want it to.
Thanks in advance
Some example clode below to hopefully explain my comment. In this, the methods in the CBaseHandler class are intentionally empty. Your Event Handler takes a reference to a CBaseHandler and in this example tries to handle a keypress and a mouseclick. However, the concrete implementation of the handler, CHandler only implements the Keypress handler (in this case just by printing a string, but you can do whatever you want)
When the Event Handler runs, only the Keypress will do anything. Presumably you'll be wanting to call that in a loop of some description in real life.
The event handler loop in your library can now take any implementation of an event handler for whatever purposes you need.
#include <iostream>
class CBaseHandler
{
public:
virtual void handleKeyPress(int i)
{
}
virtual void handleMouseClick(int i)
{
}
};
class CHandler : public CBaseHandler
{
public:
virtual void handleKeyPress(int i)
{
std::cout << "Derived - KP" << std::endl;
}
};
void DoHandle(CBaseHandler& handler)
{
handler.handleKeyPress(4);
handler.handleMouseClick(0);
}
int main() {
CHandler myhandler;
DoHandle(myhandler);
}
If you have a function declared in header file but not defined anywhere, this will compile fine. However if you attempt to use this function anywhere in the code, the linker will complain when you try to compile it as it won't be able to find the definition. So you could simply add dummy definition which does nothing.
header.h
void myFunction();
source.cpp (1)
int main()
{
myFunction(); // linker error on compile
return 0;
}
source.cpp (2)
void myFunction() {}; // dummy definition
int main()
{
myFunction(); // no problem
return 0;
}
Ok, so after getting some help from you guys and figuring it out online, I've found a solution that works for me.
I've set it up like so:
Main code:
void mouseClicked();
void main(){
setMouseClicked(mouseClicked);
}
void mouseClicked();
Header code:
void (*mc)() = NULL;
void setMouseClicked(void (*fun)(void)){
mc = fun;
}
and then in the event function I do this:
void handleMousePress(int button, int state, int x, int y){
mouseX = x;
mouseY = y;
mouseButton = button;
if(state == GLUT_DOWN){
if(mc != NULL)
(*mc)();
}
}
I've worked on a compromise by having a declaration of the function in the main part of the code and having a setter for that function, but with that I've added an ability for renaming functions that are called when the event is triggered.
So once again, thanks for your suggestions, but at the end I've found that pointers to functions work the best.
I have a C++ class using Core Audio structs on OS X.
My initial implementation was like this:
class MyClass
{
private:
AUNode _converterNode;
AURenderCallbackStruct _renderCBStruct;
public:
MyClass();
~MyClass();
inline AUNode* getConverterNode() { return &_converterNode; }
inline AURenderCallbackStruct* AURenderCallbackStruct() { return &_renderCBStruct; }
};
After reading the Poco style guides, I wanted to change the order of the private/public blocks. It then looked like this:
class MyClass
{
public:
MyClass();
~MyClass();
inline AUNode* getConverterNode() { return &_converterNode; }
inline AURenderCallbackStruct* AURenderCallbackStruct() { return &_renderCBStruct; }
private:
AUNode _converterNode;
AURenderCallbackStruct _renderCBStruct;
};
The compiler now tells me that the type AURenderCallbackStruct is unknown and tells me to replace the type name with ::AURenderCallbackStruct. When I do that, there are no compiler errors.
Weirdly, this only appears for the `AURenderCallbackStruct and not the AUNode.
The AURenderCallbackStruct is defined like this:
typedef struct AURenderCallbackStruct {
AURenderCallback inputProc;
void * inputProcRefCon;
} AURenderCallbackStruct;
and AUNode is defined like this:
typedef SInt32 AUNode;
Can anyone explain why the change of order of private/public block produces a compiler error and why the error disappears when adding a ::in front of the type?
First of all it is not clear why you named the member function as AURenderCallbackStruct that coincides with the corresponding structure's name and looks like the structure constructor.
The problem is this stupid name.
in the first case the compiler thinks that you indeed define the member function that hides the corresponding name of the structure.
In the second case the compiler thinks that you trying to call the constructor of the structure.
Simply rename the function that there would not be an ambiguity.
I am having some difficulties with a c++ program that I need to run. The problem itself is not mine and I have to make it compile. The algorithm is pretty huge so for my current error message I will demonstrate a much more simplified version of a code that I produced that gives me the exact same error. Here is the code:
class_1.h (class_1.cpp is empty)
class class_1 {
public:
class_1();
virtual ~class_1();
virtual void function() =0;
};
class_2.h (class_2.cpp is empty)
include"class_1.h";
class class_2 : public class_1{
public:
class_2();
virtual ~class_2();
virtual void function();
};
class_2a.h (class_2a.cpp is empty)
include"class_2.h";
class class_2a : public flos2{
public:
class_2a();
virtual ~class_2a();
};
class_3.h
include "class_2a.h"
include "class_1.h" //I tried unsuccesfully without including class_1.h as well
class class_3 {
public:
class_3();
virtual ~class_3();
virtual void function();
private:
class_2a my_class_2a;
};
class_3.cpp
#include "class_3.h"
class_3::class_3()
:my_class_2a()
{
}
class_3::~class_3()
{
this->function();
}
void flos3::function()
{
my_class_2a.function();
/***Main Body of function***/
}
};
The error I am getting is linker error:
undefined reference to `class_2::function()'
I know that in general the whole algorithm seems to be stupid, but more or less this is the what I was given and I am not allowed to change the structure itself, just to make it working. As you can see in class_1 function is defined as a pure virtual function, and then is called through the other classes. I really don't know how to make this thing work, so any help would be really appreciated...
You need to add:
class_1.cpp:
class_1::~class_1() = default;
class_2.cpp:
class_2::~class_2() = default;
void class_2::function() {
// add code here (or not)
}
... and so on.
You are getting linker error because your function class_2::function() does not have implementation. You need to add it, preferably in class_2.cpp file.
void class_2::function()
{
// Implementation goes here
}
Similar problem is with all virtual destructors. They need implementations as well.
I sincerely hope that people on here don't mind these sorts of questions.
After having searched plenty of times, perhaps with the wrong search terms - nevertheless - I do not seem to be able to find ANYTHING on this.
Yes, I have looked through cplusplus' documentation, yet I still could not find out the answer.
My question is simple, how do I make it so that a function can only be called via another function?
Here's an example
class someclas{
public:
void screen():
void setsomevariable (int somevariable){}
}clas;
And when I call the function, setsomevariable(), it should only be able to be used after having called the screen function. As such:
clas.screen().setsomevariable(..);
Again, how do I make this function a member of screen() alone?
This does match your requirements, but doesn't necessarily meet your needs. Would like to help more but you're stubbornly refusing to give details...
class Screen {
public:
void setsomevariable (int somevariable) {}
};
class someclas {
Screen s;
public:
Screen& screen() { return s; }
} clas;
A member function belongs to a class. You can't make a function a member of another.
You could keep a boolean in your class, and throw an exception if screen wasnt called before. But it's an ugly solution..
class someclas{
public:
someclas & screen() { // returns a reference to your class to enable the clas.screen().setsomevariable() syntax
screenCalled = true;
return *this;
}
void setsomevariable (int somevariable){
if (!screenCalled) {
throw std::exception();
}
// some code
screenCalled = false;
}
bool screenCalled;
}clas;