I have a struct like so defined like so: - //vector3.h
-
struct Vector3
-
{
-
float x, y, z;
-
Vector3();
-
Vector3(float _x, float _y, float _z);
-
Vector3(const Vector3 &v);
-
};
-
-
//vector3.cpp
-
#include "vector3.h"
-
-
Vector3::Vector3()
-
{
-
x = y = z = 0.0f;
-
}
-
-
Vector3::Vector3(float _x, float _y, float _z)
-
{
-
x = _x;
-
y = _y;
-
z = _z;
-
}
-
-
Vector3::Vector3(const Vector3 &v)
-
{
-
x = v.x;
-
y = v.y;
-
z = v.z;
-
}
-
But when I try to define a global array of the structs all the values are lost and instead it is treated as if I didn't give it any values.
I define the array like this: - const Vector3 octagon[] = { Vector3(-0.5f, -1.5f, 0.0f),
-
Vector3(0.5f, -1.5f, 0.0f),
-
Vector3(-1.5f, -0.5f, 0.0f),
-
Vector3(-1.5f, 0.5f, 0.0f),
-
Vector3(-0.5f, 1.5f, 0.0f),
-
Vector3(0.5f, 1.5f, 0.0f),
-
Vector3(1.5f, 0.5f, 0.0f),
-
Vector3(1.5f, -0.5f, 0.0f),
-
Vector3(0.5f, -1.5f, 0.0f),
-
Vector3(-0.5f, 0.5f, 0.0f),
-
Vector3(-0.5f, -0.5f, 0.0f),
-
Vector3(-1.5f, -0.5f, 0.0f) };
-
However if I create a Vector3 outside of an array everything remains intact.
6 3920
It looks like everything is there in my debugger.
In my debugger the values are there until I call the constructor for another function but that function doesn't change the variable at all, it just reads from it. It couldn't change the values because they're defined as const, also if I output the values they're all 0, but I put that in the first for loop. Here's the function in which the values are set to 0. - Octagon::Octagon(Vector3& pos, Vector3& rot)
-
{
-
for(int i = 0; i < 12; i++)
-
{
-
vertices.push_back(octagon[i]);
-
}
-
-
for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
-
{
-
indices.push_back(octagon_index[i]);
-
}
-
-
position = pos;
-
rotation = rot;
-
}
I'm soory but this looks odd: - Octagon::Octagon(Vector3& pos, Vector3& rot)
-
{
-
for(int i = 0; i < 12; i++)
-
{
-
vertices.push_back(octagon[i]);
-
}
-
-
for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
-
{
-
indices.push_back(octagon_index[i]);
-
}
-
-
position = pos;
-
rotation = rot;
-
}
octagon_index is a pointer (4 bytes) and a const_int is 4 bytes. So octagon_index)/sizeof(const int) is always 1.
The loop goes one cycle.
Maybe that's where your array has disappeared.
That line works fine, it returns 30 like expected, octagon_index is a global array like octagon but I understand what you're saying.
Here's the entire relevant part: - //main.cpp
-
#include "octagon.h"
-
-
Octagon oct(Vector3(0.0f, 0.0f, -5.0f));
-
-
//octagon.h
-
#include <vector>
-
#include "vector.h"
-
-
struct Octagon
-
{
-
Octagon(Vector3& pos, Vector3& rot = Vector3());
-
void draw();
-
-
std::vector<Vector3> vertices;
-
std::vector<int> indices;
-
-
Vector3 position, rotation, colour;
-
};
-
-
//octagon.cpp
-
#include "octagon.h"
-
-
const Vector3 octagon[] = { Vector3(-0.5f, -1.5f, 0.0f),
-
Vector3(0.5f, -1.5f, 0.0f),
-
Vector3(-1.5f, -0.5f, 0.0f),
-
Vector3(-1.5f, 0.5f, 0.0f),
-
Vector3(-0.5f, 1.5f, 0.0f),
-
Vector3(0.5f, 1.5f, 0.0f),
-
Vector3(1.5f, 0.5f, 0.0f),
-
Vector3(1.5f, -0.5f, 0.0f),
-
Vector3(0.5f, -1.5f, 0.0f),
-
Vector3(-0.5f, 0.5f, 0.0f),
-
Vector3(-0.5f, -0.5f, 0.0f),
-
Vector3(-1.5f, -0.5f, 0.0f) };
-
-
const int octagon_index[] = { 0, 10, 2,
-
2, 10, 3,
-
3, 10, 9,
-
3, 9, 4,
-
0, 1, 4,
-
4, 1, 5,
-
5, 8, 6,
-
6, 8, 7,
-
8, 11, 7,
-
1, 7, 11 };
-
-
Octagon::Octagon(Vector3& pos, Vector3& rot)
-
{
-
for(int i = 0; i < 12; i++)
-
{
-
std::cout << octagon[i] << "\n";
-
vertices.push_back(octagon[i]);
-
}
-
-
std::cout << sizeof(octagon_index)/sizeof(const int) << "\n";
-
for(int i = 0; i < sizeof(octagon_index)/sizeof(const int); i++)
-
{
-
indices.push_back(octagon_index[i]);
-
}
-
-
position = pos;
-
rotation = rot;
-
}
-
-
void Octagon::draw()
-
{
-
glTranslatef(position.x, position.y, position.y);
-
glRotatef(rotation.x, 1.0f, 0.0f, 0.0f);
-
glRotatef(rotation.y, 0.0f, 1.0f, 0.0f);
-
glRotatef(rotation.z, 0.0f, 0.0f, 1.0f);
-
-
glBegin(GL_TRIANGLES);
-
for(int i = 0; i < 24; i++)
-
{
-
glNormal3f(0.0f, 0.0f, 1.0f);
-
glVertex3f(vertices[indices[i]].x, vertices[indices[i]].y, vertices[indices[i]].z);
-
}
-
glEnd();
-
}
The output from the constructor being:
0 0 0 (for 12 lines)
30
When you run a simple main with the following: -
int main ( )
-
{
-
Vector3 v1( 0.0f, 0.0f, -5.0f);
-
Vector3 v2( 0.0, 0.0, 0.0);
-
Octagon oct( v1, v2);
-
}
It will print out the correct values form the global structure.
It looks like something else is changing the values.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Scott Lyon |
last post by:
I'm maintaining (read: I didn't write it, nor do I have the time to spend to
rewrite it) an application that is suddenly giving me grief.
The reason I say suddenly, is because we're in the...
|
by: Bryan Parkoff |
last post by:
I know how to write "Pointer to Function" inside struct or class without
using static, but I have decided to add static to all functions inside
struct or class because I want member functions to be...
|
by: PCHOME |
last post by:
Hi!
I have questions about qsort( ). Is anyone be willing to help?
I use the following struct:
struct Struct_A{
double value;
...
} *AA, **pAA;
|
by: DaHool |
last post by:
Hi there !!!
I browsed around the Internet in search for a solution of a little difficult
problem i have in VB.NET....
However, i cannot find a suitable anwser anywhere, so i thought i'll give...
|
by: pengbsam |
last post by:
Hello All:
Having a problem with arraylist.copyto function. And here's a
sample of my code:
In global-->
public struct point
{
int x;
string y;
}
static public point point;
|
by: Bill |
last post by:
Hello All,
I am trying to pass a struct to a function. How would that best be
accomplished?
Thanks,
Bill
|
by: Zytan |
last post by:
This returns the following error:
"Cannot modify the return value of
'System.Collections.Generic.List<MyStruct>.this' because it is
not a variable"
and I have no idea why! Do lists return copies...
|
by: dreiko466 |
last post by:
(sorry about my english...)
I am a newbie in C (3 month expierience)
I have wrote a simple test programm in VS2005, what i do wrong?Please...
In this programm i create a double linked list.Then ...
|
by: teddysnips |
last post by:
Weird.
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |