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
ADVERTISEMENT
Aug 2, 2014
I have the following:
int Allocate(int, int *);
main() {
int *Pointer;
int Elements = 25;
// this works just fine - as expected.
Pointer = (int *) malloc(Elements, sizeof(int));
// This DOES NOT - The value of Pointer never changes.....
[code]....
View 2 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
Mar 18, 2014
The program I'm working on is a very basic relational database. I've isolated my problem for simplicity. I get a segfault right here when I try to access db->relationCount. I tried printing db->relationCount from within loadDB and that worked,
[code]
loadDB(db, configFile);
printf("%d",db->relationCount);
fflush(stdout);
View 5 Replies
View Related
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
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
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
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
Sep 18, 2013
How can a member function in my derived class call the same function from its base class?
View 1 Replies
View Related
May 19, 2013
I'm trying to store an array of vectors using a double pointer as a 2D array, but after the first "vector" (column) has been filled, I get a segfault. I correctly included all the libraries needed, and declared all the variables; I am using this function to allocate the space for the array:
double **vec_array(int rows,int clms)
/*allocates a double matrix with -rows- rows and -clms- columns */
{
int i;
double **m;
[Code].....
I get no error while compiling, but when I run the program I get a segfault right after having read the first vector: switching on the check I get in my output just the components of the first vector and then the segfault:
Terminal:
$ ./myprog input_data.dat
0.000000 0.512468 -0.152468
Segmentation fault (core dumped)
View 2 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
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
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
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
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
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
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
Apr 15, 2014
I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?
Code:
class myclass {
public:
int member ;
} ;
template <typename T> void myclass::func( T& arg )
[Code] .....
View 4 Replies
View Related
Apr 9, 2014
I have encountered following lines in base class and I do not comprehend its meaning of "= 0" at the end of the member functions;
distance_list intersect(ray & r) = 0;
appearance get_appearance(vector & pt) = 0;
where distance_list is a list of doubles and appearance is properties.
In general, what does this "equal sign and 0 " mean for the member functions in the base class?
View 3 Replies
View Related
May 19, 2012
I keep getting an error saying ui.h:30: error: 'class BTree<Word>' has no member named 'prntInOrder'
I have no line 30 in my ui.h but if i count the lines from the .cpp as if they were attached to the .h i find the call to the BTree printInOrder()
here is my ui.h
Code:
#pragma once
#include "btree.h"
#include <fstream>
#include <iostream>
using namespace std;
[Code].....
As you can see the printInOrder() function is there so would it not see it?
Error:
Code:
ui.h: In member function 'void UI::go(std::string)':
ui.h:30: error: 'class BTree<Word>' has no member named 'printInOrder'
View 6 Replies
View Related
Feb 10, 2015
I have a class I am building called date and I've built all my functions to run and done all the necessary error checking in each function. However, my last and final function I have to write is where the # of days passed in as a parameter and then I have to increment the days by that many. And if the user does not increment any days at all in the parameter and leaves it blank, then it automatically increments by one day. I am having trouble with this. So for example if the user was to do this:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950);// June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5);// d2 is now July 4, 1950
The function starts out looking like this
void Date::Increment(int numDays = 1) {
}
I know I have to use a for loop to accomplish this. I just don't know how to get it to where the days passed in will go to the next month and then days passed in would go to the next year.
View 2 Replies
View Related
Mar 26, 2014
Some background: I have a class, A, with members, B and C and D; I also have an array of A objects; I want to be able to have a function which takes said array and performs a certain calculation on either the B, C, or D members of each of the A objects, depending upon certain circumstances; I want to perform the same calculation regardless of which member is to be used in said calculation, such as always assigning the value 3 or multiplying the member's value by a cofactor of some sort.
My question, therefore, is: how I might do this using only one function be it a template or not?
View 1 Replies
View Related
Apr 13, 2012
In C++, how do i call a method member of class A from a class B, using a pointer. By the way Class A and B are of different types.
I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????
for example
class A {
public:
int add(int x) {
return x+x;
[Code] .....
View 1 Replies
View Related
Oct 2, 2012
Usually we use the following statements to export a class or member functions,
Code:
#ifdef DLLMICRO
#define DLLIO __declspec(dllexport)
#else
#define DLLIO __declspec(dllimport)
#endif
I understand that the files using the exported class or function need to call this class or function with dllimport and the file containing the exported class or function needs to call this class or function with dllexport. But I tried to use __declspec(dllexport) only instead of the statements above. It still works. Is there anything I am missing?Why'd we have to switch between dllexport and dllimport?
View 12 Replies
View Related
Jan 16, 2014
I have a simple question about inheritance. Consider the following code:
Code:
Class Base {
int type;
Base(){};
};
Class Derived1 : public Base
[Code] ....
I get the following error: Class "Base" has no member "Function1";
That makes sense - as Base has not declared Function1. But how can I loop through a vector of Bases, and then if the object is of type Derived1, call the function Function1?
View 11 Replies
View Related