C++ :: Calling Member Function By Object Pointer
Nov 26, 2013
I have the following piece of code.
Code:
#include<iostream>
using namespace std;
class Test {
public:
Test(){cout<<"Test"<<endl;}
void fun() {
int i=5;
[Code] ...
Compiled with g++.
Executing this give output fun5.
It is correct? I have not allocated any object and so this pointer is not created. Then how it is able to run and call the function.
View 4 Replies
ADVERTISEMENT
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Mar 14, 2013
I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.
Code:
position = hash->(*funcHash)(idNmbr);
The function will return an int, which is what position is a type of. When I compile this code,
I get the error: error: expected identifier before ( token.
Is my syntax wrong? I'm not sure what would be throwing this error.
View 3 Replies
View Related
Apr 25, 2013
I am doing fixed point implementation in c++ which is done by the following code
#ifndef __fixed_point_header_h__
#define __fixed_point_header_h__
#include <boost/assert.hpp>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <limits>
[Code] ....
I am getting the error ambiguous overload for âoperator<â in âbeta < ((-5.0e-1) * pi)â
I know the problem the static member function cannot access the members and objects of structs. am i right?
and how to solve this problem, do i need to implement the cossin_cordic function as a non member function.
View 14 Replies
View Related
Dec 4, 2013
I have this code:
#include "stdafx.h"
#include <iostream>
using namespace std;
void square_cube(float *x,float *y) {
[Code] .....
The function should pass the square of the first input parameter and the cube of the second input parameter back to the calling routine. I thought I could use a recursion but its not going to work.
View 8 Replies
View Related
Nov 6, 2013
Right now I have code
#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double length;
[Code] .....
it gives error ...
View 1 Replies
View Related
Jun 6, 2012
I am trying to use "remove_if" with a predicate function inside a class. The code intends to remove the grid cells which an agent cannot move into (from among all possible cells).
Code:
void classname::function1()
{
vector<MoorePoint> neighbors;
....
[Code]....
That code would work if it was not in a class and the predicate was not a member function. However, now I receive long error messages which I guess refer to incompatibility of remove_if template with the predicate parameter (one error includes : error C2064: term does not evaluate to a function taking 1 arguments).
View 2 Replies
View Related
May 15, 2013
I have a class with member functions and a pointer like this:
Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};
Now in a regular function I create an instance of the class and try to assign the pointer:
Code:
MyClass mc;
mc.fnptr = &mc.fn1;
The compiler does not like it and suggests this instead:
Code:
mc.fnptr = &MyClass::fn1;
This seems to work but what if I have two instances of the class:
Code:
MyClass mc1, mc2;
How does the compiler know to distinguish between
Code:
mc1.fn1
and
Code:
mc2.fn1
when the assignment now looks identical:
Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;
View 14 Replies
View Related
Aug 19, 2014
I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:
double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);
[Code]....
Now I want to make the function objective a member of a class:
class Foo {
public:
double objective(...){..}
};
How can I give this method to opti.optimize? If I make objective static I can use
opti.optimize(Foo::objective,NULL);
but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?
View 1 Replies
View Related
Jan 6, 2013
I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:
class Region {
public:
/* game values are here, they are public so
they can be accessed from outside of the class
inside of packet-handling functions and such */
int value;
void *Function();
[Code] ....
The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".
View 11 Replies
View Related
Jan 3, 2015
I am trying to create a callback system for input events in my game engine.
Here is a cut down version of the EventManager.h file
#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };
[Code].....
This works if the function pointer being passed to the event manager is not a member function.
I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.
For example, if I want to make a player object it would be something like
class PlayerObject : public Object{...};
Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before
This is how I intend to use this
class OtherScene : public Scene{
void p_pressed(void){
//pause
}
[Code].....
View 6 Replies
View Related
Apr 12, 2013
i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.
main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"
[Code].....
View 7 Replies
View Related
May 30, 2013
I'm making a code that uses a Function pointer. The problem is, when I try to compile appears an error like:
error: no matching function for call to 'rnVector::rnVector()'
Here's part of the code:
phiFunction::phiFunction(double (*f)(rnVector), rnVector (*df)(rnVector)) {
//... Here comes the code stuff...
}
View 12 Replies
View Related
May 20, 2014
I have a Qt classes as follow:
Code:
class Vehicle {
public:
void AddData(QString str, Data* data) {
_myDataMap.insert(str,data);
} virtual void Init();
[code].....
My questions are:
After the main function called d1->Modify; the data stored in _myDataMap will get modified too.
What is the more appropriate way of passing the Data through AddData in such case?
If i do AddData(const Data & data), i will not be able to use inheritance of Data, i.e passing a subclass of Data to AddData.
View 3 Replies
View Related
Jul 29, 2014
I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.
Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.
Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?
///////////////////////////////////////////////////////////////////////////
"MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info
188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114
189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128
190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128
[Code] .....
View 4 Replies
View Related
Aug 12, 2013
I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code
I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class
[Code] ...
Here is the lines of code i want to understand.
Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);
I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.
View 3 Replies
View Related
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related
Oct 17, 2013
In the following code example of the State Design Pattern, in the main code at the bottom it defines an instance of class Machine, and then calls Machine::off;. Why doesn't it instead call fsm.off;?
Machine fsm;
Machine::off;
Then I tried imitating that by adding a class Abba, and then doing:
Abba a;
Abba::DoStuff();
but that didn't work. Why?
Full code example:
// StatePattern.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Machine {
class State *current;
[Code] ....
View 3 Replies
View Related
May 28, 2013
So I am wondering how I would go about calling a function for all instances of an object. I tried googling it, but all I saw was solutions like making an array of pointers to the objects. What if I don't know how many objects there will be? Isn't there an easier way?
View 19 Replies
View Related
May 10, 2014
class abc {
public:
int i;
abc * foooo;
};
How do you call * foooo? Say I create:
abc a;
* foooo would have some values for int i.
To get int i of *foooo, I tried a.foooo.i, which doesn't work. How do you call it?
View 5 Replies
View Related
Sep 22, 2014
Code:
std::ostrstream oss;
oss << "path for " << unit << "
" << path;
puts(oss.str());
[Code] .....
Today, I just received this new fresh error, I was constantly using them, but just come to know it is a private access violation as the last error of my program. Did I use it in the wrong way?
View 3 Replies
View Related
Dec 16, 2012
In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.
void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}
View 4 Replies
View Related
Mar 22, 2013
I am posting this simplified piece of code that is a bit confusing for me. There are two functions that I call. One shows the expected results but the result of the other one rather puzzles me.
//#define defineVecTyp Vec3f
#define defineVecTyp float
template <typename vecTyp>
vecTyp buildLaplacianPyramid(cv::Mat inputmat) {
vecTyp lapPyr;
[Code].....
Calling the function sum1 does not change the values stored in the variables val1 and val2. The output of the program is as follows:
val1= 1 ## val2= 10 // before the call of function sum1
val1= 1 ## val2= 10 // after the call of function sum1
sumOfVals= 22
This is quite obvious and as expected and I just pasted this piece of code as an example for better clarification.
However, if I call the function buildLaplacianPyramid and apply a function for Gaussian Blurring, this also effects the cv::Mat passed to the function. The line imshow("M1, after buildLaplacianPyramid",M1); therefore shows an image that is blurred. Since I am not passing a pointer to the cv::Mat I do not understand why this should be happening. I was assuming that there would be a copy of the cv::Mat M1 to be used within the function. Therefore I was expecting the cv::Mat M1 to retain its original value. I was expecting that all changes applied to cv::Mat inputmat within the function would not have any influence on the cv::Mat M1. Just like in my other example with the sum.
View 3 Replies
View Related
Jan 21, 2013
The case is like
class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}
in somefunction i want a pointer to current object of class A m new to c++
View 2 Replies
View Related
Feb 23, 2014
I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file. I have made sure to include the header file and have tried to access it through an object.
This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:
RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous'
This is the code inside randomiser.h:
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class RandomG {
[Code] ....
This is the error inside xcode: [URL] ....
I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?
I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.
View 3 Replies
View Related
Mar 21, 2013
If I wanted to call a member function inside another member function, how would I do that, if it's possible?
For example, if I have Find(int key) defined already and wanted to call it while i was overloading operator+.
View 2 Replies
View Related