C++ :: Pointer To Member Function?

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


ADVERTISEMENT

C++ :: Member Function Pointer Assignment?

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

C++ :: Function Pointer To Non-static Class Member

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

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 View Related

C++ :: Using A Member Within Pointer Function (that Is Located In Same Class) - Segfault

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

C++ :: Pass Pointer To Member Function (polymorphic Structure)?

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

Visual C++ :: Access Member Function From Non-member Function In Same CPP File?

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

C++ :: Call To Member Function X Is Ambiguous - Overloaded Member From Header File

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

C++ :: Call Member Function Inside Another Member Function?

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

C++ :: How To Access Pointer To Class Member

Jan 10, 2015

I have the following scenario :

Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}

I need to access mem_ptr (pointer to a class member) through :

a. object (t1)

b. pointer to the object (ptr).

How can i do that ?

View 4 Replies View Related

C++ :: Non Constant Member Function Being Called Inside Constant Member Function?

Dec 28, 2012

#include <iostream>
class Hello {
public:
void Test() {

[Code].....

As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .

View 5 Replies View Related

C++ :: Returning A Pointer To Encapsulated Data Member

Jun 22, 2014

So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.

Find my code below:

#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {

[Code] .....

View 1 Replies View Related

C++ :: Delete Member Of Vector That Is Pointed By Pointer?

Apr 17, 2013

I have following:

struct Point {int* a; int b;};
vector<vector<Point> > numbers;
vector<int> example;

The numbers vector has a matrix of a sort and each of the members are pointing to one member in the example vector. A member numbers.at(2).at(3).a is pointing at example.at(3). Now, can I remotely delete a member in the example vector using the pointers? Like so:

delete (*(numbers.at(2).at(3).a));

I know there is a more convenient way to delete members, but this is a very specific case I'm working on.

View 3 Replies View Related

C++ :: Class Member Functions With Pointer Parameters?

Jan 30, 2013

Here is the assignment: (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (How the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!

class Album {
private:
char * artist; // band or singer’s name
char * title; // title of the album

[code]....

The input will be an array. My questions: First, am I on the right track?

When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?

also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?

what is the difference between *& and * as parameters?

View 5 Replies View Related

C :: Calling Function Via Function Pointer Inside Structure Pointer

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

C++ :: Prevent Destructor Delete Member Pointer From Constructor

Dec 3, 2013

I have little problem which causing memory leaks.

Parent > Multiple Child(Parent parent) > Child destructor deleting parent => next Child destructor crash

Example code: without using:

class Parent {
public:
Parent() {
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j)
childs[i][j] = new Child(this);

[Code] ....

If you read code, on Parent destructor i = 0 & j = 1 its going crash.

Parent will be deleted aswell, but it give me assert: _block_type_is_valid(phead- nblockuse)

View 3 Replies View Related

C++ :: Embedded Micro Controller - Pointer To Class Member

Sep 19, 2014

This project is for an embedded micro controller. In the project i wrote a class that generically services uarts. then i declare 6 objects of that class and hand them configurations for each specific uart.

internally all the objects have a send buffer of data that is still to be sent that gets populated by the object member function.

how can i make an array of function pointers that can point to the same member but of six different objects.

for example (not a working one)

class uart {
private:
struct myData {
unsigned char data[20]
int head
int tail
int count;

[Code] ....

View 9 Replies View Related

C++ :: Declaring Vector Of Pointer As Private Class Member

Mar 6, 2013

Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.

The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.

File 1: main.cpp (working fine)

#include <vector>
#include <iostream>
using namespace std;

[Code].....

View 19 Replies View Related

C++ :: Pointer-to-member Array Crashes With Virtual Inheritance?

Sep 30, 2014

I've got the following code which demonstrates a problem :

Code:
struct A {
double x[2];
double i;
};
struct B : virtual public A {
double y[2];

[code]....

I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?

View 4 Replies View Related

C++ :: Calling Defined Function Pointer From Another Pointer To Class Object?

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

C++ :: How To Use Structure Pointer Through A Structure Public Member Definition

Dec 7, 2014

Why doesn't this compile?

struct hi(){
void other();
}histructure;

void hi::other(){
std::cout << "Hi!" << std::endl;

[Code] ....

Makes no sense the structure is written before the structure member function is called so why is there compile errors ??...

View 3 Replies View Related

C++ :: For Each And Member Function

Apr 2, 2012

I can't get this code to compile (using VS2010 and gcc4.6.1):

Code:
#include <string>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
class X {
public:
void foo( const std::vector<std::string>& v ){

[Code] ....

VS2010 presents an error message like "member function already defined or declared" and gcc something like "... function can not be overloaded" (very cryptic error message).

If I change the vector to foo to std::vector<int> and let bar() take an int, it works perfectly fine. And if I use boost

Code:
std::for_each( v.begin(), v.end(), boost::bind( std::mem_fun(&X::bar), this, _1 ) );

The above code compiles as well.

While it is perfectly fine for me to use boost I would nevertheless like to understand what's happening here.

View 4 Replies View Related

C++ :: Using Non-function Member Into A Class

Oct 21, 2013

I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?

std::string func( const vector<int> vec ) {
//processamento
return result;
} class A {

[Code] ....

View 6 Replies View Related

C++ ::  member Function Of Class

Jun 16, 2013

whether i can define a member function inside a class or not in C++. Is the following code is legal?

#include<iostream> using namespace std;
class adder {
private:
int a;
int b;
int c;
int answer;
public:

[code]....

View 6 Replies View Related

C++ :: Constant After Member Function

Aug 27, 2013

#include<iostream>
using namespace std;
class Student{
public:
int age;
int rollNo,marks;
string name;
void AddEntry();

[Code] .....

error: non-member function 'void Display(Student*, int)' cannot have cv-qualifier|

why and how can I solve it?

View 7 Replies View Related

C++ :: OOP Compare Member Function

Feb 10, 2015

I have an assignment in my OOP c++ class and I had to create a class called date and one of the member functions is a compare function that compares two dates that are taken in. It is suppose to be something like this:

Date d1(12,25,2003);// Dec 25, 2003
Date d2(5,18,2002);// May 18, 2002

d1.Compare(d2);// returns 1 (since d2 comes first)
d2.Compare(d1);// returns -1 (calling object is d2, comes first)

Then if d1 and d2 are equal then it returns 0.

This is what he gave us to start with the function:

int Date::Compare(const Date& d) {
}

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved