Access variables in a struct? - c++

I'm building a small vector engine and I'm having some trouble accessing the variables I have made in my struct.
Here's my struct:
struct vectorVariables {
float v = 0, a = 0;
float u = 20;
float deltaT = 0.01;
float posNew = 0 , posOld = 0;
float gravity = -9.81;
};
All I want to be able to do for example, is print out one of these variables to the console.

Those variables are available if you create an object. With that declaration in scope, and with the appropriate headers included, try this:
int main() {
vectorVariables vv;
std::cout << vv.v << " " << vv.a << "\n";
}

Related

Creating an object and storing it in vector messes up the values

So I am trying to make a particle system and I need to create some Particle objects and store them in a vector to use them later.
The function that does that is:
void spawnLoop( std::vector<Particle*> &particleVector ){
for (int i=0; i < 5; i++) {
particleVector.emplace_back( new Particle(400.0, 400.0, 1.0, 1.0) );
}
}
The particle class constructor looks like this:
Particle::Particle(float xPos= 400,float yPos= 400,float xVel= 0,float yVel= 0) {
float xPosition = xPos;
float yPosition = yPos;
float xVelocity = xVel;
float yVelocity = yVel;
bool dead = false;
std::cout<< "We have " << xPosition << " "<< yPosition << " "<< xVelocity << " "<< yVelocity << std::endl;
//This prints the values and they look correct
}
But if I try to loop over the vector exactly after I finish to store it , it gives me values as:1.81063e+13.
I did try to research it for quite a bit but couldn't find any solution to it.
EDIT:
void loopOver( std::vector<Particle*> const vec ){
for (auto i = vec.begin(); i != vec.end(); i++){
std::cout << "avem " << (*i)->getXPos() << " " << (*i)->getYPos() << std::endl;
}
}
The problem is in the constructor of Particle. You are assigning the parameters to local variables, not to instance variables. So, later, when you try to print out the values of those variables, they have not been saved:
This is the problem:
float xPosition = xPos;
float yPosition = yPos;
float xVelocity = xVel;
float yVelocity = yVel;
It should be something like this:
_xPosition = xPos;
_yPosition = yPos;
_xVelocity = xVel;
_yVelocity = yVel;
Where all those variables are defined as instance variables of the class, and getXPos(), etc. return them.
Even better would be to not assign the values in the constructor, but to initialize them an initialization list. Here's a decent question/answer about initialization lists: C++ initialization lists for multiple variables

Making Terrain Strip in c++ & returning 2d array

I'm screwing around with opengl in c++, this is my first time really writing anything in c++, so I'm having troubles with pointers and arrays. I'd like to make a function that makes a strip of terrain (represented by an array of vertices, each vertex being an array of 3 floats) and returns it. This 2d array (unknown sized array of 3 sized arrays) can then be rendered with gl triangles. I'm not having any trouble with rendering it, but when my code runs currently, when I log the values of the vertices in the array local to the function, everything is fine, but in the main source, the values aren't logged correctly.
tl;dr:
How can I generate an array of arrays in a c++ function.
Here's my function right now:
float** makeTerrainStrip(float tile, unsigned int tiles)
{
float** strip = new float*[(tiles + 1) * 2];
for (unsigned int i = 0; i < tiles; i++) {
float x = i * tile;
float y = 0;
float z1 = 0;
float z2 = tile;
float v1[3] = {x, y, z1};
float v2[3] = {x, y, z2};
strip[i * 2] = v1;
strip[i * 2 + 1] = v2;
}
std::cout << strip[0][0] << std::endl;
std::cout << strip[0][1] << std::endl;
std::cout << strip[0][2] << std::endl;
return strip;
}
and it gets called from the main source here:
strip = makeTerrainStrip(1, 5); // strip is a global defined earlier
std::cout << strip[0][0] << std::endl;
std::cout << strip[0][1] << std::endl;
std::cout << strip[0][2] << std::endl;
Here's the output:
// in the function
4
0
0
// in the main source
4
1.26749e+029
7987.1

How to use a function c++

We are starting to learn functions and I've copied the first example we were given below. What is the purpose of float a after int main()? Also could someone give another example of using a function? Thanks
#include <iostream>
using namespace std;
float rectArea (float h, float w)
{
float area;
area = h * w;
return area;
}
int main()
{
float a, h, w;
h = 3.0;
w = 4.0;
cout << "area = " << rectArea(h, w) << endl;
return 0;
}
type specifier float in the first line after main has nothing common with functions because this line defines scalar objects.
Statement
float a, h, w;
defines objects a, h, and w as having type float that is these objects can store float numbers.
I think your professor means the following
int main()
{
float a, h, w;
h = 3.0;
w = 4.0;
a = rectArea(h, w);
cout << "area = " << a << endl;
return 0;
}
Otherwise variable a is defined but not used in the program.
The code would be more clear if he used more meaningful names. For example
int main()
{
float area, height, width;
height = 3.0f;
width = 4.0f;
area = rectArea(height, width);
cout << "area = " << area << endl;
return 0;
}
What is the purpose of float a after int main()?
Shortly speaking the purpose of variable a is to store the result of calculating the area of a rectangle. In fact it is redundant if you are going simply to output the area on console. In this case you could remove the definition of a and write in the output statement
cout << "area = " << rectArea(h, w) << endl;
as it is shown in the original code.
'a' is the variable intended to store the area of the rectangle after multiplying width * height.
'float' describes what type the variable 'a' is, and float is a decimal to 7 digits of precision. So the area of the rectangle is a decimal to 7 digits of precision.
The line:
float a, h, w;
defines 3 variables called a, h and w of type float.
Variable a doesn't have a purpose in this example because it's not used.

what is wrong with my cross product code

I am a C++ beginner. I would like to get the normal vector of a surface, which is determined by three point a,b and c. I have the following code, but I do not know what is wrong with it. Thanks for the help.
#include <iostream>
using namespace std;
class point
{
public:
double x,y,z;
};
class crproduct:point
{
public:
double x1,x2,x3,y1,y2,y3,z1,z2,z3,Ax,Ay,Az,Bx,By,Bz;
point crproduc(point *a,point *b,point *c)
{
//point a
x1 = (*a).x;
y1 = (*a).y;
z1 = (*a).z;
//point b
x2 = (*b).x;
y2 = (*b).y;
z2 = (*b).z;
//point c
x3 = (*c).x;
y3 = (*c).y;
z3 = (*c).z;
//Vector A
Ax = x1-x2;
Ay = y1-y2;
Az = z1-z2;
//vector B
Bx = x2-x3;
By = y2-y3;
Bz = z2-z3;
//cross product
point vector;
vector.x = (Ay*Bz)-(By*Az);
vector.y = -(Ax*Bz)+(Bx*Az);
vector.z = (Ax*By)-(Ay*Bx);
return vector;
}
};
int main ()
{
point *pp, *p1, *p2;
point cd;
crproduct cr1,cr2,cr3,cr4;
(*pp).x = 12;
(*pp).y = 13;
(*pp).z = 15
(*p1).x = 10;
(*p1).y = 10;
(*p1).z = 10;
(*p2).x = 8;
(*p2).y = 5;
(*p2).z = 2;
cd = cr1.crproduc(pp,p1,p2);
cout << cd.x << " " << cd.y << " " << cd.z << endl;
system("pause");
return 0;
}
This is incorrect:
point *pp,*p1,*p2;
point cd;
crproduct cr1,cr2,cr3,cr4;
(*pp).x=12;
(*pp).y=13;
(*pp).z=15
(*p1).x=10;
(*p1).y=10;
(*p1).z=10;
(*p2).x=8;
(*p2).y=5;
(*p2).z=2;
Apart from the missing semicolon, the point *pp,*p1,*p2 line establishes three typed pointers. It doesn't create any objects or instantiate the pointers. So at that point, using the pointers will have undefined results.
You then go on to access the pointers.
If you want them on the stack, just declare the objects directly as:
point pp, p1, p2;
... and then access appropriately. If you need them on the heap then you should use new to create objects that the pointers can point to, e.g.
pp = new point;
And don't forget to delete later.

How to access a variable defined in a struct

struct POINT3DID
{
unsigned int newID;
float x, y, z;
};
typedef std::map<unsigned int, POINT3DID> ID2POINT3DID;
ID2POINT3DID m_i2pt3idVertices;
Can someone please tell me how can I access the variables x,y and z using m_i2pt3idVertices
m_i2pt3idVertices is a container for storing POINT3DID objects. Alone, it doesn't have member variables x, y, or z. You can put a POINT3DID inside of it though:
m_i2pt3idVertices[0] = POINT3DID(); // Put a POINT3DID into key 0
m_i2pt3idVertices[0].x = 1.0f; // Assign x for key 0
m_i2pt3idVertices[0].y = 2.0f; // Assign y for key 0
m_i2pt3idVertices[0].z = 3.0f; // Assign z for key 0
ID2POINT3DID is map container. You can access single element by some unsigned int key:
m_i2pt3idVertices[42].x
Or you can iterate over elements in container:
for(ID2POINT3DID::iterator it=m_i2pt3idVertices.begin();it!=m_i2pt3idVertices.end();++it) {
cout << it->second.x << " " << it->second.y << " " << it->second.z << endl;
}
You need to use iterator. Here is a sample:
std::map<unsigned int, POINT3DID>::iterator it;
it = m_i2pt2idVertices.find(5);
it->second.x = 0;
it->second.y = 1;
it->second.z = 2;