C++ :: Two Dimensional Pointer To Object?
May 24, 2012
I am migrating a scientific code from Java to C++.
What's wrong with the two functions? I can use the int** like a two dimensional array but not the Agent**.
I receive this error: "No operator = matches this operand".
(I have allocated two dimensional arrays for objectSpace and agentSpace somewhere else).
Code:
int** objectSpace;
Agent** agentSpace;
void Space::removeAgentAt(Point p) {
agentSpace[p.x][p.y] = NULL;
}
void Space::putAgentTo(Agent agent, Point newP) {
agentSpace[newP.x][newP.y] = agent;
}
View 8 Replies
ADVERTISEMENT
Jul 16, 2013
This is a sample program that declares a Matrix as a structure with an array as a pointer to a pointer. The following program is supposed to store a matrix in the structure "_Matrix" and later print the matrix just entered but it fails giving me a "segmentation fault". The sample code is given below
Code:
#include <stdio.h>
#include <stdlib.h>
struct _Matrix {
int row_size;
int col_size;
int **mat;
[Code] ......
View 1 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
Apr 1, 2013
int sum(int (*ar2)[4], int size);
// I dont know what the ar2 is going on
int main(){
int data[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int total = sum(data, 3)
return 0;
}
View 4 Replies
View Related
Feb 12, 2013
dynamically allocated arrays. int (*ttt)[2][10]; If I'm not mistaken this declares a pointer (that's all). This pointer has an intrinsic shape that makes it easier to access row, column without doing my own math to calculate the offset.
ttt = malloc(2 * 10 * sizeof(int));
So I should be able to access elements like this: ttt[i][j].
ttt[i][j] = 123;
But the compiler says: error C2440: '=' : cannot convert from 'int' to 'int [10]'...how to go about accessing a monolithic block of allocated memory using 2 or more dimensions, using some sort of casting to a known shaped array if possible. As opposed to doing i * cols + j type stuff.
View 2 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
Aug 4, 2013
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
struct bop {
string realname; //real name
[Code] ....
Okay, so first thing's first. The program will not compile due to lines 39-45. If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?
I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.
View 6 Replies
View Related
Jul 23, 2013
I have pointer object and i have to assign to another variable ( new object ). But if i change any value in new object should not reflect in old object. I did in the below way, it changes the value.
class Test {
public:
int num;
};
Test* obj;
obj.num=1;
Test obj_n=*obj;
obj_n.num=2;
Now both object's num have value as 2;
i.e
obj.num // has 2
obj_n.num // has 2
Actually my expected result is:
obj.num - should have value 1
obj_n.num - should have value 2
My actual scenario is different where Test obj is pointer object, so obj should be pointer object. i have given sample scenario.
View 5 Replies
View Related
Aug 16, 2013
I have the following code.
StackElement *StackElementArray;
StackElementArray = new StackElement[iMaximumDepth];
I want to assign one element of this array StackElementArray the address of another object. For example,
voidStackMem::push(StackElement &iStackElement) {
CurrentDepth++;
StackElementArray[0] = iStackElement;
}
The StackElement class contains pointers to some dynamic arrays. When I use the assignment, StackElementArray[0] = iStackElement;, it doesn't copy the complete contents and I have to define an 'assignment operator' function to copy all the contents. I am wondering if there is a way I can assign StackElementArray[0] the pointer to the StackElement object. In that case, I will not need to copy the contents of iStackElement into StackElementArray[0] and will just copy the address.
View 3 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
Mar 20, 2013
Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). My question is : is it possible to overcome that without memory leaks ?
1 #include<iostream>
2 using namespace std;
3
4 class A {
5 public:
6 ~A() {
7 cout<< "~A()" <<endl;
[Code] ....
View 5 Replies
View Related
Aug 27, 2013
I have a program that has a base class 'control' and there are 2 dervied classes 'button' and 'textbox'. How do i make a constructor in the 'button' or 'textbox' that initializes a pointer of the data type 'control' to point to the object that invokes the constructor. the code should look like this
class control {
//data
} class button:public control {
buton() {
//code for the constructor
}
}
actually i have an array of pointers of the type 'control' and as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance
View 7 Replies
View Related
Feb 14, 2013
lets say I have a pointer p_unit of type c_unit* (c_unit is an a.b.c.)
I have a function that returns a pointer to a new c_unit object:
c_unit * man_add_unit() {
c_unit * local_p_unit;
unsigned short int local_run_code;
print_man_add_menu();
local_run_code = get_a_run_code(); // this bit just gets user input
switch (local_run_code)
[code]....
I assign that to p_unit, then add it to a vector v_units:
p_unit = man_add_unit();
v_units.push_back(p_unit);
cout << "New unit added.
";
The whole program runs on a loop, and another thing the user can do is to print out data on c_unit objects pointed to by v_units. The problem is, in that function up there ^ I give the user the option to go back to main menu without creating a unit.
Since "local_p_unit" is declared but not assigned an initial value, I'm guessing the function would return a "null" pointer (which is what's hanging me up). If I just let this run with the above code, and go to print out the unit data, the program crashes.
I tried to make an if thing with p_unit == 0 but this always returns false and doesn't catch the "bad" unit that will subsequently cause a crash.
Btw, I have considered assigning a reference to a generic c_unit object to that there local_p_unit so it won't return null, then remove pointers to that object from v_units at the end of the loop.. But I know there's got to be a better way.
View 4 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
Jan 14, 2015
Is there a way to copy a derived class object thru a pointer to base?
For example:
class Base { public: Base( int x ) : x( x ) {}
private: int x; };
class Derived1 : public Base { public: Derived( int z, float f ) : Base( z ), f( f ) {}
private: float f;};
class Derived2 : public Base { public: Derived( int z, string f ) : Base( z ), f( f ) {}
[Code] ....
The question is whether *B[0] would be a Derived1 object and *B[1] a Derived2 object?If not, how could I copy a derived class thru a pointer to the base class?
View 1 Replies
View Related
Feb 19, 2015
I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration
Code:
SensorBase* const sensor(mSensors[i]);
It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??
View 6 Replies
View Related
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
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
Apr 12, 2013
[URL] ....
Which object's draw is being called?
View 6 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
Jul 31, 2013
How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());
and for the one dimensional array as:
int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);
But I do not know how to make a 3-dimensional array or vector.
View 3 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
Jan 17, 2014
I had a hard question in my C++ final exam and I'm trying to solve it for the last 3 days. I haven't succeded yet! Here is the question: You have a one-dimensional array A[20]={1,2,3,4,...,20} and B[5][4] you have to assign the A array's elements to the B array but there is an order which is: B[5][4] = { { 12, 9, 11, 10 }, { 14, 7, 13, 8 }, { 16, 5, 15, 6 }, { 18, 3, 17, 4 }, { 20, 1, 19, 2 } } and there is a restriction: you can only use ONE for statement, nothing else!
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.
[code]....
I can't narrow the statements to one,This program works perfectly but it shouldn't be that long, we need ONLY ONE FOR statement, not two!
View 2 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
Mar 16, 2013
will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?
View 10 Replies
View Related