.h file
public:
Class(int x, int y); //constructor for this question
private:
char (*1dArrayObjectPtr)[size] = nullptr;
char nameof2dArray[notImportantX][size];
What is the difference between initializing Class (*1dArrayObjectPtr)[size] = nullptr; then assigning by:
cpp file
Class::Class(int x, int y) : x(x), y(y) {1dArrayObjectPtr = nameOf2dArray;};
or:
Class::Class(int x, int y) : x(x), y(y), 1DArrayObjectPtr(nameof2dArray) {};
Why does the top option result in segmentation faults and the bottom does not when I access as:
*(*(1DArrayObjectPtr+i)+j)
or
1DArrayObject[i][j]
If I pass 1DArrayObjectPtr to a new class will I be able to iterate the same?:
newClass::newClass(char* 1DArrayObjectPtr) : newClassPtr(1DArrayObjectPtr) {};
iterate as *(*(newClassPtr+i)+j) or newClassPtr[i][j]
Or am I changing the 1dpointer from the 2D array into something else and not realizing?
Scheff has confirmed semantically, there is no difference. I will look to my constructors for the base class to see if they could be culprit. Thanks Scheff. I tried to mark your comment as a solution, but I may have flagged it by mistake.... :D
Related
In C++ how make a class variable in one line?
For example:
I have a class:
class point{
public:
int x;
int y;
};
How to make a variable in one line like java you can do new point(x, y), currently I do make a tmp and then push back to vector or something, are the simply way like java can do what I do in one line?
For creating a variable of type point on the stack you can use:
point myVariable{5,6};//this creates a point type variable on stack with x=5 and y=6;
So the complete program would look like:
#include <iostream>
class point{
public:
int x;
int y;
};
int main()
{
point myVariable{5,6};
return 0;
}
The output of the above program can be seen here.
If you want to create a vector of point objects and then add objects into it, then you can use:
//create point objects
point p1{5,6};
point p2{7,8};
//create a vector
std::vector<point> myVector;
//add p1 and p2 into the vector
myVector.push_back(p1);
myVector.push_back(p2);
Build a constructor Point(int x, int y) : x(x), y(y) {}
And then push to vector as usual vec.push_back(Point(x,y))
I get the error "expected identifier" for the following code. How do I use initilization lists properly in constructors?
tanVec::tanVec(const int x, const int y, const int z): this->x(x), this->y(y), this->z(z)
{
}
You can safely remove this - it is not needed for disambiguation, because the names in the initialization list are resolved to members of your class, even if your argument list has parameters with names that would require disambiguation in the body of the constructor.
// Compiler will not confuse members x, y, and z with constructor arguments x, y, and z
tanVec::tanVec(const int x, const int y, const int z): x(x), y(y), z(z) {}
Small demo on ideone.
Could not find a clear solution for this problem.
I have two classes Point and Vector. Vector is a child of Point In one of the methods of class Point I want to use an object of class Vector. I do it like this:
class Point
{
double x, y, z;
public:
// constructor from 3 values
Point(double x, double y, double z)
: x(x), y(y), z(z)
{}
// method move point
Point move(Vector vect, double dist)
{
Vector vectU = vect.unit();
return sum(vectU.multiplyScalar(dist));
}
};
class Vector: public Point
{
double x, y, z;
public:
// constructor from 3 values
Vector(double x, double y, double z)
: Point(x, y, z), x(x), y(y), z(z)
{}
// create unit vector
Vector unit()
{
double len = length();
return Vector(x / len, y / len, z / len);
}
};
When I compile this it gives me an error in line Point move(Vector vect, double dist) "Vector" has not been declared. I cannot find any useful answer for this error. How do I do this initialisation?
In C++ a class needs to be declared before it is defined. In your example with everything in one file, it has no idea what a Vector is when you define your Point::move function.
Typically, we'd have a header file per class (MyClass.h etc) and put the function definitions in a cpp file per class (MyClass.cpp)
So you need to restructure to something like:
Point.h:
#ifndef _POINT_H
#define _POINT_H
class Vector; // Forward declaration so you don't need to include Vector.h here
class Point
{
double x, y, z;
public:
// constructor from 3 values
Point(double x, double y, double z);
// method move point
Point move(Vector vect, double dist);
}
#endif // _POINT_H
Point.cpp
#include "Point.h"
#include "Vector.h"
// constructor from 3 values
Point::Point(double x, double y, double z)
: x(x), y(y), z(z)
{}
// method move point
Point Point::move(Vector vect, double dist)
{
Vector vectU = vect.unit();
return sum(vectU.multiplyScalar(dist));
}
Vector.h
#ifndef _VECTOR_H
#define _VECTOR_H
#include "Point.h"
class Vector: public Point
{
double x, y, z;
public:
// constructor from 3 values
Vector(double x, double y, double z)
: Point(x, y, z), x(x), y(y), z(z);
// create unit vector
Vector unit();
}
#endif // _VECTOR_H
Vector.cpp
#include "Vector.h"
// constructor from 3 values
Vector::Vector(double x, double y, double z)
: Point(x, y, z), x(x), y(y), z(z)
{}
// create unit vector
Vector Vector::unit()
{
double len = length();
return Vector(x / len, y / len, z / len);
}
(disclaimer, No guarantees that this will compile and work straight away, this is just to demonstrate how the code should be split up!)
Put a forward declaration:
class Vector;
at the beginning of the file.
Also, put a ; after the definition of each class.
If your class Vector
class Vector: public Point
inherits from Point, then you should not be using Vector in the base class Point (the base class shouldn't know anything about the derived class).
Also you are redefining x, y, z in your derived class Vector, which defeats the point of inheritance and can lead to very nasty behaviour when using polymorphism.
A virtual function may do the trick for you.
ie
move() Stub in the base
move() declaration in the derived.
Use pointers for dynamic binding.
eg point *x = new vector(...)
x.move()
etc etc.
Been banging my head against this all day with many trips to google.
I have a master object that needs to create several other objects in its constructor the main object gets variables in its constructor that are passed on to the objects it creates.
class WorldManager{
public:
WorldManager(int x, int y, int z){
//do stuff
}
}
class GameManager{
public:
WorldManager world;
GameManager(int x, int y, int z){
world(x,y,z);
}
}
I get error
error: no matching function for call to `GAMEMANAGER::GraphicsManager(HWND__*&, int&, int&)'
it works untill I ask for args in the constructors of the world class
I think that you want:
class GameManager{
public:
WorldManager world;
GameManager(int x, int y, int z) : world(x, y, z) { }
};
The weird colon thing is called an initialization list, and it does construction of member objects and parent classes and a bunch of other things.
If you have more than one object that you want to construct, add them to the list:
class GameManager{
public:
WorldManager world1, world2;
GameManager(int x, int y, int z) : world1(x, y, z), world2(x, y, z) { }
};
I have a template for a 3D array however I'm having problems compiling when a variable of type Chunk (see typedef below) tries to do an index access.
template <typename T, int xMax, int yMax, int zMax>
class Volume {
public:
class HelperIndex3 {
public:
HelperIndex3(Volume& p, int xx, int yy) : parent(p), x(xx), y(yy) {}
T operator[] (int z) const {return parent.data[x][y][z];}
T& operator[] (int z) {return parent.data[x][y][z];}
private:
Volume& parent;
int x;
int y;
};
class HelperIndex2 {
public:
HelperIndex2(Volume& p, int xx) : parent(p), x(xx) {}
HelperIndex3 operator[] (int y) const {return HelperIndex3(parent, x, y);}
HelperIndex3 operator[] (int y) {return HelperIndex3(parent, x, y);}
private:
Volume& parent;
int x;
};
HelperIndex2 operator[] (int x) const {return HelperIndex2(*this, x);} //problem at this line
HelperIndex2 operator[] (int x) {return HelperIndex2(*this, x);}
private:
T data[xMax][yMax][zMax];
};
typedef Volume<unsigned char, 64, 64, 64> Chunk;
The problem line appears to want a const Volume& p in the HelperIndex2 constructor which I guess makes sense since I'm trying to use a const Chunk elsewhere - but I don't know how to fix this. If I add a second constructor am I supposed to maintain two parent references one that is constant and another that is not? I fear this is going to balloon into a mess. If someone could explain the right way of handling this situation I would greatly appreciate it.
You won't get around maintaining two sets of proxy classes. This is completely analogous to the iterator and const_iterator nested classes of standard library containers.
Since you are returning a copy of the proxy class, the proxy class that contains a mutable reference to the original object cannot possibly also be used for constant-only access to the original object, so you cannot get around some sort of two-fold approach. The semantics of the two are different: If all you have is an instance of the proxy class, you have to be able to know whether it allows mutating or only read-only access.