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.
Related
.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
I get the error message Call to implicitly-deleted default constructor of 'std::array' when I try to compile my C++ project.
Header file cubic_patch.hpp
#include <array>
class Point3D{
public:
Point3D(float, float, float);
private:
float x,y,z;
};
class CubicPatch{
public:
CubicPatch(std::array<Point3D, 16>);
std::array<CubicPatch*, 2> LeftRightSplit(float, float);
std::array<Point3D, 16> cp;
CubicPatch *up, *right, *down, *left;
};
Source file cubic_patch.cpp
#include "cubic_patch.hpp"
Point3D::Point3D(float x, float y, float z){
x = x;
y = y;
z = z;
}
CubicPatch::CubicPatch(std::array<Point3D, 16> CP){// **Call to implicitly-deleted default constructor of 'std::arraw<Point3D, 16>'**
cp = CP;
}
std::array<CubicPatch*, 2> CubicPatch::LeftRightSplit(float tLeft, float tRight){
std::array<CubicPatch*, 2> newpatch;
/* No code for now. */
return newpatch;
}
Could someone tell me what is the problem here, please ? I found similar topics but not really the same and I didn't understand the explanations given.
Thanks.
Two things. Class members are initialized before the body of the constructor, and a default constructor is a constructor with no arguments.
Because you didn't tell the compiler how to initialize cp, it tries to call the default constructor for std::array<Point3D, 16>, and there is none, because there is no default constructor for Point3D.
CubicPatch::CubicPatch(std::array<Point3D, 16> CP)
// cp is attempted to be initialized here!
{
cp = CP;
}
You can get around this by simply providing an initializer list with your Constructor definition.
CubicPatch::CubicPatch(std::array<Point3D, 16> CP)
: cp(CP)
{}
Also, you might want to have a look at this code.
Point3D::Point3D(float x, float y, float z){
x = x;
y = y;
z = z;
}
x = x, y = y, z = z doesn't make sense. You're assigning a variable to itself. this->x = x is one option to fix that, but a more c++ style option is to use initializer lists as with cp. They allow you to use the same name for a parameter and a member without the use of this->x = x
Point3D::Point3D(float x, float y, float z)
: x(x)
, y(y)
, z(z)
{}
I cannot seem to find why I am getting the build error
expected primary-expression before "float"
In this implementation...
using namespace std;
class Point{
public:
Point(float X = 0.0, float Y = 0.0);
void set(float X, float Y);
void setX(float X);
void setY(float Y);
void get(float * P_x, float * P_y);
float getX();
float getY();
float * pX();
float * pY();
SDL_Point returnSDL();
private:
float x;
float y;
};
class Vector : public Point{
public:
Vector(float X = 0.0, float Y = 0.0);
};
///The errors occur in this constructor...
Vector::Vector(float X, float Y) : Point(float X, float Y){
}
Im still learning about the finer points of classes and would appreciate any help. I know it has something to do with the inheritance because when Vector doesn't inherit Point the program builds normally. As far as i can tell this is the correct syntax and implementation of inheritance. Web help i have found cannot answer so far.
///The errors occur in this constructor...
Vector::Vector(float X, float Y) : Point(float X, float Y){
}
There are two similar constructions in this fragment of code:
Vector::Vector(float X, float Y) and : Point(float X, float Y):
the first one (Vector::Vector(float X, float Y)) is the declaration of the constructor of class Vector;
the other one (: Point(float X, float Y)) is a function call; a call of the constructor of class Point; notice the colon (:) that introduce the list of member initializers.
Now, if you see the difference between the two (function/method declaration or definition vs. function/method call) you can find the error yourself: the compiler expects expressions and not types in the arguments list of the call to the Point::Point() constructor.
// Look, ma! No errors!
Vector::Vector(float X, float Y) : Point(X, Y) {
}
For more information take a look at the documentation page about constructors and member initializer lists.
You are confusing declaring a function and using a function. When you declare a function you need to tell the compiler what the types of the parameters are.
Vector::Vector(float X, float Y)
Now in the member initialization part you have
: Point(float X, float Y)
Here you are adding types to the function call which is not what you want to do. When you call a function you just pass the values/variables to it.
: Point( X, Y)
^ ^ no type here as we just pass X and Y to the Point constructor.
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) { }
};