Simply put, I am trying to have textures and other semi-constant objects be prepared within one .h or .cpp file so that they can be used within other files. I want to be able to do something like this:
class Position
{
private:
int x;
int y;
public:
/* constructor, destructor, and other code */
void setPosition(int x, int y) { this->x = x;
this->y = y; }
};
Then, in another file, have something like this:
//otherfile.extension
// I want to be able to declare these objects here...
Position specialPosition1;
Position specialPosition2;
// ...and then be able to do this somewhere where it will keep
// this information for any other file that includes this.
specialPosition1.setPosition(25, 25);
specialPosition2.setPosition(-10, -10);
I want to be able to call their setPosition() method and prepare them within the same file to be used within other files if possible. Or at least be able to set it up so that the information will exist before it is used anywhere else.
If I recall, making them static would not solve this issue; plus I still have no (known) place to call the setPosition to prepare the objects. I have also read up a tad bit on extern, though my knowledge of it is only vague.
How might I be able to prepare these objects before main(), or, to be a bit more precise, what is the "best" way to prepare these objects before they are used in other files?
I don't think you want to call setPosition at all. I think it would be better to initialize them in the constructor. I assume these special positions are constant.
I think you want to declare them as extern in a .h file and then define them in a .cpp:
Position.h:
struct Position {
int x;
int y;
Position(int _x, int _y) : x(_x), y(_y) {}
//...
};
SpecialPositions.h:
#include "Position.h"
extern const Position specialPosition1;
extern const Position specialPosition2;
SpecialPositions.cpp:
#include "SpecialPositions.h"
const Position specialPosition1{25, 25};
const Position specialPosition2{-10, -10};
Main.cpp:
#include "SpecialPositions.h"
#include <iostream>
int main() {
std::cout << specialPosition1.x << "\n";
}
Or you could force compile time constants and use constexpr. Change the Position constructor to be constexpr:
constexpr Position(int _x, int _y) : x(x_), y(_y) {}
And then just define them in the SpecialPositions.h file (no need for SpecialPositions.cpp):
constexpr Position specialPosition1{25, 25};
constexpr Position specialPosition2{-10, -10};
Related
First of all, let me say I have read similar threads on how to initialize a global variable or how to properly implement global constants. Nevertheless, these questions did not really help me with my concrete problem, as also haven't any other ressources I've consulted. The problem is the following.
I (have to) declare in a header file header.h a variable that must be constant and used by the main function in main.cpp as well as other functions defined in a another file functions.cpp (that are previously declared in header.h). The problem is this constant is a runtime constant, whose value is given by the user. How should I proceed?
I think my best shot was to do as follows. In header.h
// Header guard
namespace n
{
// Some forward declarations
extern const double mu; // The constant that should be initialized by the user.
// Some other declarations
}
then in functions.cpp
#include "header.h"
namespace n
{
// Some definitions here
double function_that_uses_mu(double a, double b)
{
// Some code using mu
return somedouble;
}
// Some other definitions
}
finally main.cpp
#include "header.h"
#include <iostream>
int main()
{
// Some code
double value_of_mu{};
std::cin >> value_of_mu;
// Validity check
extern const double n::mu{ value_of_mu };
// More code
return 0;
}
The problem is that compilation fails due to the fact that error: ‘mu’ has both ‘extern’ and initializer. However, if I tried mu = value_of_mu I would obviously get an error because I would be assigning to a const value (not initialising it). So I don't know how to proceed, or what's wrong with my code. There are two mandatory facts I must respect:
Functions/global-consts definitions, functions/global-consts declarations and main must be split in the three aforementioned files.
mu is to be defined as a constant shared by all three files.
Is this possible? How?
EDIT:
I think my problem is that an exten variable can't be initialised inside a function, but if this is so, I don't see how I do what I'm trying to.
Think about it: If a value needs to be set during the lifetime of a program, it's not really a constant. So, you shouldn't try to pretend it's a constant by declaring it as const. If you don't want the program to accidentally change its value, you have to protect it in some other way, such as making it a private member variable of a class. That way, you can restrict access to only return the value of mu as a const.
// muholder.h
class muholder
{
private:
double m_value;
public:
muholder (double ivalue): m_value(ivalue) {}
double const &value() const { return m_value; }
};
// workflow_envelope.h
class workflow_envelope
{
private:
muholder m_mu;
public:
workflow_envelope (double imu): m_mu(imu) {}
bool validity_check();
double method_that_uses_mu (double a, double b) const { return a*m_mu.value()/ b; }
void run(); // any "more code" goes in here.
};
// main
#include "workflow_envelope.h"
#include <iostream>
int main()
{
// Some code
double value_of_mu;
if (std::cin >> value_of_mu)
{
// Validity check
workflow_envelope workflow(value_of_mu);
if (workflow.validity_check())
{
workflow.run();
return 0;
}
}
return 1;
}
When you initialize a variable, you can't specify extern. This should work instead:
int main()
{
// Some code
double value_of_mu{};
std::cin >> value_of_mu;
// Validity check
using namespace n;
const double mu = value_of_mu; // no extern specifier
// More code
return 0;
}
So I am trying to make snake clone.
I have made a BodyPiece class which has certain variables I would like to be const, as they should not change. This was not a problem when I simply created an instance of Bodypiece to test out. However I want to be able to add pieces as the snake grows so I made a vector body variable. As you can see from the code below:
In BodyPiece.h:
#pragma once
#include "D3DGraphics.h"
#include "Keyboard.h"
#include "Direction.h"
class BodyPiece
{
public:
BodyPiece(int xPos, int yPos, Direction* dir);
~BodyPiece();
void Draw(D3DGraphics& gfx);
void UpdateDirection(KeyboardClient& kbd);
void Move();
private:
Direction* dir;
int xPos;
int yPos;
int width = 5;
int height = 5;
int vx = 5;
int vy = 5;
};
In Game.h file
BodyPiece vector body declared:
std::vector<BodyPiece> body;
In Game.cpp file
vector initialised in initialiser list.
body({ {400, 300, &d } })
If I make any variables in BodyPiece const, it will generate an error:
error C2582 'operator=' function is unavailable in class BodyPiece.
My questions here are:
What causes that? I have looked online and have a little bit of an idea but it really is only a little bit.
How can I have const variables, use a vector container(or another type of container if that will help, does not have to be vector) and keep the compiler happy? Or should I just completely change my approach?
Thank you in advance.
The problem is that if you don't have an assignment operator, then the compiler will generate one for you, that does simple shallow copying of the members. However, if you have actual const member variables (and not only default-initialized like yours) then those can't be copied and the compiler can't create an assignment operator.
The simple solution is to make the constants actual constants and also static so they are members of the class and not part of a single object:
class BodyPiece
{
...
static int const width = 5;
static int const height = 5;
};
The only drawback with this is that you need to actually define these member variables, in a single source file:
int const BodyPiece::width;
int const BodyPiece::height;
The other solution is to explicitly create an assignment operator that copies the data needed:
class BodyPiece
{
public:
...
BodyPiece& operator=(BodyPiece const& other)
{
// Copy data from other to this
xPos = other.xPos;
...
return *this;
}
...
};
Trying to compile my code in Xcode, but I am obviously running into some problems as I get the error in the title. Here is the code for my header file called "myClasses.h":
#ifndef myClasses_h
#define myClasses_h
class Star
{
public:
Star(int x,int y)
{
int xPos = x;
int yPos = y;
}
};
#endif
So I obviously want a constructor for Star so I can declare a Star object like this:
Star sol(10,30);
Then the code in my "main.cpp":
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include "myClasses.h" //includes the Star class
bool checkOverlap(Star check, int a, int b)
{
double diffX = a - check.xPos;
double diffY = b - check.yPos;
double checkDist = sqrt( pow( diffX,2.0 ) + pow( diffY,2.0) );
if( checkDist > 20 )
return true;
else
return false;
};
int main()
{
//some other code here with no errors
};
Here in the main file I get the error, so I am lost as to what is going wrong? Am I using incorrect syntax to access the object's member variables? Shouldn't the code below print 10:
Star sol(10,30);
cout << sol.xPos
If not, how do I rewrite my class so it behaves like that or how do I properly access the member variables in the constructor?
You need to declare the variables in your class, you can't do that in the body of the constructor as those will be temporary variables not associated with the class instance. This will leave the xPos and yPos as their default values (which is 0) which is probably not what you want. Try something like this instead:
class Star
{
public:
int xPos;
int yPos;
Star(int x,int y):
xPos(x), yPos(y) //initializing the variables here
{
}
};
I've used a member initializer list here to initialize the members.
It's worth noting that this is likely not the best design for a class, you probably want to make xPos and yPos private along with some functions to change those values. You probably want to read up about encapsulation. Essentially you want to hide away information so that people don't need to know the internals of how your classes work in order to use them. This big benefit is that this lets people use your code without needing to worry about the internals of how your code works and lets them keep using your code without having to change their code even if some of those internal details happen to change over time. Imagine the hassle if you had to know exactly how your network card driver was programmed in order to write an application that used the network. It would be a big pain, you might have to change your code whenever you updated the other code, however because this driver code has (hopefully) been encapsulated you don't need to worry about these details in order to use that code. The code and classes you write are no different, think about the people who will use them, try to make it easy for them to use your code.
A possibly better design would be to do something like this:
class Star
{
private:
int xPos;
int yPos;
public:
Star(int x,int y):
xPos(x), yPos(y) //initializing the variables here
{
}
int get_xPos(){
return xPos;
}
int get_yPos(){
return yPos;
}
};
Now in your main code you change:
Star sol(10,30);
cout << sol.xPos;
To:
Star sol(10,30);
cout << sol.get_xPos();
The benefits of doing it this way really start to become more obvious when you get larger software or you have to deal with changes. For example later on the code changes and we decide to store the coordinates in a coordinates struct:
struct coords{
int xPos;
int yPos;
}
class Star
{
private:
coords Pos;
public:
Star(int x,int y):
Pos{x,y} //initializing the variables here
{
}
int get_xPos(){
return Pos.xPos;
}
int get_yPos(){
return Pos.yPos;
}
};
The original code would break:
Star sol(10,30);
cout << sol.xPos; //There's no xPos anymore
but with our new design this:
Star sol(10,30);
cout << sol.get_xPos();
Works just like before! We only needed to change the code in one place in the getter function get_xPos() and everthing will keep working just like it did before we made the changes.
Declare member variables:
class Star
{
public:
int xPos;
int yPos;
Star(int x,int y)
{
xPos = x;
yPos = y;
}
};
I've created a structure in a header file as follows:
typedef struct
{
GLfloat lgtR, lgtG, lgtB, lgtA;
GLfloat x, y, z;
bool islight;
GLfloat width, height, depth;
GLenum lightn;
particle prt;
int maxprt;
} emitter;
which works without a problem.
However, in that particular header file, I want to declare a global emitter that I can use in all the functions and isn't part of the main source file:
// header.h global declaration
emmiter currentEmit;
GLvoid glSetEmitter(emitter emitter)
{
currentEmit = emitter;
}
However, when I do try this, I get a whole lot of "error C2228: left of '.variable' must have class/struct/union, so I'm assuming it's not declaring my structure here at all.
Is there a way to declare that structure globally within a header file, and if so, is there also a way to keep it from being part of the other .cpp files as well?
emitter is not the same as emmiter.
Also, since this is C++ - just write struct {}; directly, there's no need for a typedef.
Your whole header is wrong, and will give multiple definitions if included in multiple translation units:
// header.h global declaration
extern emitter currentEmit; // <-- note extern
inline GLvoid glSetEmitter(emitter emitter) // <-- note inline
{
currentEmit = emitter;
}
currentEmit needs to be defined in a single implementation file, not a header. The function needs to be inline so it's not defined by all TU.
Last thing: pass the parameter by const reference:
inline GLvoid glSetEmitter(const emitter& emitter) // <-- note inline
{
currentEmit = emitter;
}
Otherwise an unnecessary copy will be created.
typedef struct
{
GLfloat lgtR, lgtG, lgtB, lgtA;
GLfloat x, y, z;
bool islight;
GLfloat width, height, depth;
GLenum lightn;
particle prt;
int maxprt;
} emitter;
should better be
struct Emitter
{
GLfloat lgtR, lgtG, lgtB, lgtA;
GLfloat x, y, z;
bool islight;
GLfloat width, height, depth;
GLenum lightn;
particle prt;
int maxprt;
};
Is there a way to declare that structure globally within a header file,
Yes, there are two main ways to avoid the variable being created in each compilation unit.
First there is the Meyers' singleton:
namespace g {
inline Emitter& emitter()
{
static Emitter theEmitter;
return theEmitter;
}
}
void foo() { g::emitter().x = 666; }
Then there is the templatization trick:
namespace detail {
template< class Dummy >
struct EmitterVariable
{
static Emitter v;
};
template< class Dummy >
Emitter EmitterVariable<Dummy>::v = {};
}
namespace g {
static Emitter& emitter = EmitterVariable<void>::v;
}
void foo{ g::emitter.x = 666; }
and if so, is there also a way to keep it from being part of the other .cpp files as well?
Yes, both of the above solutions do that.
The last one, however, injects a reference into each compilation unit, which in practice will be the size of a pointer.
That said, global variables tend to give very messy data flows. You don't know which part of the code put something there. You don't know if or when it's properly initialized. You don't know which other parts will be affected if you change data here. Or when. And so on. So it's absolutely not a good idea. Global constants, OK, but global variables, Just Say No™.
I'm learning c++. I have written a small program which should compute the energy
of a N particle system. Up to now I have three small files:
data.h:
class Particle {
public:
double mass;
double charge;
double posx,posy,posz;
};
Particle part[2];
main.cpp:
#include <iostream>
#include "data.h"
using namespace std;
double energy(Particle part );
int main ()
{
double sd;
part[0].mass = 10.0;
part[4].mass = 90.0;
cout << part[0].mass << "\n";
cout << part[4].mass << "\n";
sd = energy(part);
cout << "sd" << sd << "\n" ;
return 0;
}
energy.cpp:
#include <iostream>
using namespace std;
double energy(Particle part)
{
cout << part[0].mass << "\n";
double dummy;
dummy = 2.0;
return (dummy);
}
I have two questions:
1)I want to make visible the Class particle in the function "energy". In other words,
I want to use the variables of the class function (with the values given in "main")
in the energy function.
I have tried as you see energy(Particle part) but it seems Particle is not defined
in that scope.
2)As you see in "data.h" I declared "part" as an array with two members. However,
in "main" I can use more than two members, for instance part[3],part[4]... Why I
could use more members than those I declared?
I am compiling with g++ -o test energy.cpp main.cpp
thanks.
1)I want to make visible the Class particle in the function "energy". In other words, I want to use the variables of the class function (with the values given in "main") in the energy function. I have tried as you see energy(Particle part) but it seems Particle is not defined in that scope.
If I understand you right.. You want to have
Particle part[2];
to be use able in main.cpp and in energy.cpp ?
If yes.. change this to:
extern Particle part[2];
and in energy.cpp add this:
#include "data.h"
Particle part[2];
and you will be able to use
double energy()
{
//main.cpp will have same part
cout << part[0].mass << "\n";
double dummy;
dummy = 2.0;
return (dummy);
}
2)As you see in "data.h" I declared "part" as an array with two members. However, in "main" I can use more than two members, for instance part[3],part[4]... Why I could use more members than those I declared?
Because it's C/C++ ? no range checks. You can do what ever you want.
But if you do, the result will be unexpected.
1)I want to make visible the Class particle in the function "energy".
You should #include "data.h" in the file energy.cpp.
2)As you see in "data.h" I declared "part" as an array with two members.
You probably shouldn't have done that, for two reasons:
You will learn later to avoid declaring global objects. It is legal (and often correct) to do so, but until you learn, you probably want to declare it as a local variable in main.
You should not declare global objects in header files, since they will then be declared in every translation unit that includes the header file.
However, in "main" I can use more than two members, for instance part[3],part[4]... Why I could use more members than those I declared?
By indexing beyond the end of the array, you have invoked "undefined behavior". The system is free to do almost anything (for example, crash, send Bill Gates an email, start a global thermo-nuclear war, &c). Among the infinite variety of things included in "undefined behavior" is the most confusing one of all -- appear to work. That is what happened in your case. You should not count on it continuing to appear to work.
All sorts of things you can do with a class ...
struct Vector3
{
double m_x, m_y, m_z;
};
class Particle
{
public:
double ComputeEnergy() { // return answer }
double GetMass() const { return m_mass; }
double GetCharge() const { return m_charge; }
const Vector3& GetPos() const { return m_pos; }
void SetMass(double mass) { m_mass = mass; }
void SetCharge(double charge) { m_charge = charge; }
void SetPos(const Vector3& pos) { m_pos = pos; }
void SetPos(double x, double y, double z)
{
m_pos.m_x = x;
m_pos.m_y = y;
m_pos.m_z = z;
}
private:
double m_mass;
double m_charge;
Vector3 m_pos;
};
You need to #include "data.h" in energy.cpp. Includes are only processed on a per-file basis, so energy.cpp can't see the header without that.
EDIT: In your function, the parameter part, out-scopes the global definition of part, so the part in your function is not an array. You want:
cout << part.mass << "\n";
1>Include "data.h" in energy.cpp
2> C++ array is very primitive it doesn't has any bound checking so you were able to access part[4].But there is no guarantee that it will work every time.Most of the time program may crash during run-time complaining the memory is corrupt.
To answer the first question, you could simply include data.h in energy.cpp.
#include "data.h"
Unfortunately, you've created "part" in the global space and so each .cpp file will independently create it. When it goes to link the object files, it will see that multiple references to "part" exist. What you can do here is use the "extern" keyword and in data.h, you would simply declare it as an external variable. Or, because you only reference "part" in main.cpp, you could just move the global definition in there and the problem is solved.
Now, when it comes to what you have done in energy.cpp, you've create a separate variable also called "part". When scoping rules are applied, it means that the compiler is going to take the local definition over the global definition. Since you are passing a class object and not a class array, you'll get a compiler error when referencing it like "part[0].mass".
Instead, why are you concerned about the value of the other particle and not the particle you passed in? If you want the mass of a particular Particle object, then you should write it like "part.mass"
Of course, I would argue that what you really want to create is a member function for energy within Particle. Right now, you are currently using classes in a C style struct way. This means that you are missing the ability to use all the object oriented goodness that C++ has to offer. You could do it like this
class Particle
{
protected:
double mass;
double charge;
double posx,posy,posz;
public:
void SetMass(double newMass) { mass = newMass; }
void SetCharge(double newCharge) { charge = newCharge; }
void SetX(double newX) { posX = newX; }
void SetY(double newY) { posY = newY; }
void SetZ(double newZ) { posZ = newZ; }
double GetEnergy() { return 2.0; }
};
To answer your second question, C++ assumes you know more about the memory then it does. There is no bounds checking in GCC(AFAIK) because the memory could be allocated anywhere at any time and deallocated as well. However, unlike other languages, you have to manage the memory yourself. There's a lot of optimizations and C++ trickery that this enables(like declaring a zero sized array for unknown sizes and assigning a block of memory to it).
Cheers!