C++ :: Pass Object As A Pointer In Function

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


ADVERTISEMENT

C++ :: Pass Object To Class Which Stores It In Container As Unique Pointer

Jul 24, 2013

class A (abstract)
class B : A
class C {
void add ( A(&*?) a )
std::vector<std::unique_ptr<A>> data; //unique_ptr<A> because A is abstract and therefore vector<A> isn't possible
}

upper situation. What is the best way to pass add an object of class B to C?

with C::add(A* a){ vector.push_back( unique_ptr<A>(a) ); }
and
int main() {
C c;
c.add( new B() );
}

This works, but i don't think it's very nice, because you could delete the pointer in main. What happens then with the unique_ptr? I could probably make C::add( std::unique_ptr<A> u_p ); but maybe it can be avoided that the "user" (in main() ) has to create the unique_ptr itself.

View 10 Replies View Related

C++ :: Pass Double Pointer Of Object Into A Node Of Self Similar Type?

Nov 30, 2014

The following code compiles and runs fine till it reaches line 16 and gets a sigsev violation which I'm not sure about as to why. I have no problem passing the object of type node** into the constructor of base and storing it into the double pointer node** copy;; but when I call the function void pass(node** temp) it crashes.

#include <iostream>
class base;
class node {
private:
node** data;
public:

[Code] .....

View 3 Replies View Related

C++ :: Retrieving File Object From Function Then Pass To Another Function?

Jun 27, 2014

I would like to have 2 functions. (FYI, I haven't even tested these because I don't have a compiler on this PC, so don't know what they'll do. I'm also new to C++, self-teaching.)

My question is, I'm sure that oFile should be type object (of some sort), not int, but I'm not sure how to reference it correctly so that it passes from FileOpen to main to FileClose.

Code:
#include <iostream> //I/O
using namespace std;
#include <fstream> //files
using namespace ios;
int FileOpen(string fileName) {
ifstream oFile (fileName); //attempt to open file

[Code] ....

View 1 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++ :: Pass A Pointer Into Function - Value Of Char?

Jan 2, 2014

I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like

char* charptr;
and then calling
myfunct(charptr);

and then inside the function i would set charptr equal to another char ptr, simply like

charptr = anothercharptr;

But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.

I solved this issue by calling the function like

myfunct(&charptr);

and declaring the function like
myfunct(char**);

and then I had to dereference the charptr in the function when assigning it to another ptr, so
*charptr = anothercharptr;

It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.

What is the difference between the value of the char* charptr written as either charptr and &charptr?

View 1 Replies View Related

C++ :: Pass Address Of Pointer To Function

Sep 25, 2014

#include <iostream>
using namespace std;
void myfunc(int* ); // what do i put in these parameters to accept a mem loc of a pointer
int main () {
int x = 5;

[Code] .....

SOLUTION:

#include <iostream>
using namespace std;
//Purpose to create a function that returns a pointer to a pointer
int** myfunc(int**);
int main () {
int x = 5;

[Code] ....

View 3 Replies View Related

C++ :: Pointer - Pass Reference Variable Into Function

Oct 4, 2013

I don't understand how my code not run.

#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;

[Code] ....

View 3 Replies View Related

C++ :: How To Pass Function Pointer As A Template Parameter

Jun 13, 2013

void extf(int a) { }
template<typename P>
struct A {
// 1
template< void (*F)(P) >
static void call(P arg) {

[Code]...

Why it is not working? What would be a proper way to pass function pointer as a template parameter?

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

C/C++ :: Const Pointer Pass By Reference In Print Function

Apr 21, 2014

I am trying use a print function to print out data in a struct. My questions are:

1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?

2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {

[Code] .....

View 9 Replies View Related

C++ :: Function Pointer With Object As Parameter?

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

C++ :: Passing Object By Pointer To Function?

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

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++ :: Virtual Function Table Pointer Misplaced In Object Memory

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

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

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

C++ :: Pass Matrix By Reference Using Pointer Of Pointer

Jun 20, 2014

i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):

#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)

[Code]...

View 6 Replies View Related

C++ :: Trying To Pass Array Of Object

Apr 24, 2013

I am trying to pass an array of objects however i m finding some problems.

pieces.h

Code: class Cpiece{
public:
Cpiece* board[8][8];// array of objects
virtual char getcolor() =0;
virtual char setcolor(char colour)=0;

[Code] .....

View 10 Replies View Related

C/C++ :: Pass Value From Object To Array

Apr 16, 2014

I am trying to write a poker program. I created a class to create a deck of cards..and then shuffle the deck..Now I am trying to pass value from the deck to an array to deal a hand..so 5 cards to player1[] array.. I tried doing it directly such as.

for (int j = 0; j < 5; j++) {
getCard=deck[j].toString();
}

But I would get error such as [Error] no match for 'operator=' in 'player1[j] = deck[j].Card::toString()'

So then I tried using a pointer..

char *getCard;
getCard = new char;
for (int j = 0; j < 5; j++) {
getCard=deck[j].toString();
}

but I would get this error [Error] void value not ignored as it ought to be. So how can I pass a value from the object deck..to an array? or should I be doing it another way?..I tried to think of a way to do it via a function but really got hung up there..

here is full code

#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const char* FaceString[13] = {"Ace", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};

[Code] ....

View 11 Replies View Related

C++ :: Pass Class Object By Reference

Jun 25, 2013

I am having trouble working with third party dll's, libs and header files. I am trying to call a function.here is the function that is suppose to be called.

bool COAuthSDK::GetRequestToken(CClientDetails &objClientDetails)

it has this info of what it needs :

Name IN/OUT Description
m_environment IN Optional. Possible values are SANDBOX (default) and LIVE.
m_strConsumerKey IN OAuth consumer key provided by E*TRADE
m_strConsumerSecret IN OAuth consumer secret provided by E*TRADE
m_strToken OUT Returned by the function if successful
m_strTokenSecret OUT Returned by the function if successful
m_strCallback IN Optional; default value is "oob"

here is the COAuthSDK header

#ifndef _OAUTHSDK_H_INCLUDED_
#define _OAUTHSDK_H_INCLUDED_
#include "ETCOMMONCommonDefs.h"
#include "ETCOMMONOAuthHelper.h"
using namespace std;

[code].....

when I try to build the function it says that its in the dll's so I know I have to call it.here is the link to the build site if needed URL....

View 1 Replies View Related

C++ :: Pass Class Object By Reference?

Mar 17, 2014

I'm trying to pass a class object by reference.

total = mathfunction(i);
}
double mathfunction(retirement& j)
{
double R = 0.00, m = 0.00, r = 0.00, t = 0.00, totaled = 0.00,
numerator = 0.00, denom = 0.00, temp = 0.00;

[Code]....

I only tried to pass by reference because I figured passing by value would be a larger pain in the neck.

View 1 Replies View Related

C++ :: How To Pass Same Instance Of Object In Two Functions

Feb 25, 2014

I need to send same instance of object of a class in two function (Main function and thread function). The class is something like this:

//The class need to have constructor.
Class ABC {
public:
DWORD *IdG;
ABC(int number) {
IdG = new DWORD[number];
}
}obj(32);

The obj(32) is called in following two function. Function F1 is called using thread in main function.

void F1() {
obj.test;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
obj.test1;
_beginthread(F1,0,(void*)number);
}

The code works well when the object of class ABC is created as shown above. My problem is the value that is passed in the object ('32') is to be read from the file.

If I read the file and create object separately in Main function and function 'F1' then , function 'F1' is not executed.

How to create same instance of object for Main function and function 'F1' with value passed in the object taken from the file.

View 1 Replies View Related

C# :: Determinate Type Of Object And Pass It Through Method

Sep 18, 2014

I got base class called Statistic and inherited classes from it : lsp, cpu, memory. Those methods inheriting some of methods from Statistic and implement also theirself methods. I prepared some new class called ExecuteRequest which is taking Statistic object in the constructor. What i want to achieve is after i put e.g LSP to this class i would like to determinate what specific object it is in this case LSP right? Then having that i would like to put this object to RunRequest method. How can i achieve that? See my code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SOAP_testing {
public class ExecuteRequest

[Code] .....

View 14 Replies View Related

C++ :: Object That Is Passed To Function Is Changed Although No Pointer Is Passed

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

C++ :: Unable To Pass Reference Of Object Ifstream To Constructor

Jan 30, 2013

I'm trying to pass an reference of the object ifstream to my constructor as such:

// myClass.cpp
int main(){
ifstream file;
myClass classobj = myClass(file);}

I am defining the constructor as follows, in myClass.cpp:

myClass::myClass(ifstream &file){
// do stuff
}

I get the error of "No overloaded function of myClass::myClass matches the specified type."

Also, when I build the program, it tells me "syntax error: identifier 'ifstream'"

Also, I made sure I included this:
#include <iostream>
#include <fstream>
#include <sstream>

What am I doing wrong? How can I pass an ifstream reference to my constructor?

View 3 Replies View Related

C++ :: Can't Pass To Functions With Pointer

May 25, 2013

I get a run time error saying something about memory locations when I run this program.How can I make this program work using pointers?

//this program converst miles to km
#include<iostream>
#include "std_lib_facilities.h"

[Code]....

View 1 Replies View Related







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