#include "Vector.h"
#include <stddef.h>
#include <gl\gl.h>
#ifndef PART3DH_NMB
#define PART3DH_NMB
const float DEG2RAD = 3.14159/180;
class Mass
{
public:
Vector pos;
Mass (radius);
void setPosition (Vector p)
{
pos=p;
}
};
Mass::Mass (radius)
{
glPushMatrix();
glColor3f(4.0, 1.0, 4.0);
glTranslatef(pos.x ,pos.y,pos.z);
glBegin(GL_LINE_LOOP);
for (int i=0; i < 360; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
}
glEnd();
glPopMatrix();
}
#endif
🔒 Locked
Problem with creating object array
Advertisement
Original Post
Hello I have this object : this is a very basic cyrcle in opengl. now I included the header to my main program and tried to create an array of my mass object with this line : mass = new Mass[300]; I got this errors: [C++ Error] Unit1.h(82): E2303 Type name expected [C++ Error] Unit1.h(82): E2233 Cannot initialize a class member here what would be wrong with my object ? I pressed F1 in Borland help and it told me below options: E2303 Type name expected Compiler error ---------------------------------------------- One of these errors has occurred: In declaring a file-level variable or a struct field, neither a type name nor a storage class was given. In declaring a typedef, no type for the name was supplied. In declaring a destructor for a C++ class, the destructor name was not a type name (it must be the same name as its class). In supplying a C++ base class name, the name was not the name of a class. Please help, I thank you so much in advance.
Mass *mass=new Mass[666];
- You should specify the type for the radius in your Mass constructor, i.e. Mass(float radius).
- You cannot create an array of Mass because Mass does not have a default constructor. Either provide a default constructor (probably by providing a default value for radius, i.e. Mass(float radius = 0)) or use a std::vector< Mass > (or other container) and push_back/insert as and when you have valid Mass objects.
Enigma
Quote:
Original post by bargasteh
Mass (radius);
... I think you mean Mass(float radius);, also investigate constructor initializer lists to initialize data members, try use them when ever possible.
Quote:
Original post by vNistelrooy
Mass *mass=new Mass[666];
Once he has corrected that mistake this still wont work because he does not have a default constructor:
#include <vector>....std::vector<Mass> masses;masses.reserve(num_of_elems);masses.push_back(some_radius);.....Besides he should generally prefer using C++ dynamic arrays over C-style dynamic arrays.
Thank you, is it correct now?
I used Mass mass = new Mass[666]; and it didnt work.
How do I have to use vector?
is it all :
#include <vector>
....
std::vector<Mass> masses;
masses.reserve(num_of_elems);
masses.push_back(some_radius);
Thank you
#include "Vector.h"#include <stddef.h>#include <gl\gl.h>#ifndef PART3DH_NMB#define PART3DH_NMBconst float DEG2RAD = 3.14159/180;class Mass{public: Vector pos; Mass (); void Draw (float radius);};Mass::Mass():pos(0,0,0){}void Mass::Draw (float radius){ glPushMatrix(); glColor3f(4.0, 1.0, 4.0); glTranslatef(pos.x ,pos.y,pos.z); glBegin(GL_LINE_LOOP); for (int i=0; i < 360; i++) { float degInRad = i*DEG2RAD; glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius); } glEnd(); glPopMatrix();}#endifI used Mass mass = new Mass[666]; and it didnt work.
How do I have to use vector?
is it all :
#include <vector>
....
std::vector<Mass> masses;
masses.reserve(num_of_elems);
masses.push_back(some_radius);
Thank you
Pay attention to vNistelRooy and snk_kid, they know what they're talking about.
vN mentioned, and snk_kid affirmed, that you need to have:
Note the '*' symbol.
Cheers,
Twilight Dragon
P.S. - Don't forget to call "delete[] mass;" when you're finished with it.
vN mentioned, and snk_kid affirmed, that you need to have:
Mass *mass = new Mass[555];Note the '*' symbol.
Cheers,
Twilight Dragon
P.S. - Don't forget to call "delete[] mass;" when you're finished with it.
When I used :
I got this error:
[C++ Error] Unit1.h(83): E2303 Type name expected
[C++ Error] Unit1.h(83): E2139 Declaration missing ;
or when I use them at the top after #include <vector>, I get:
[C++ Error] Unit1.h(23): E2238 Multiple declaration for 'masses'
[C++ Error] Unit1.h(21): E2344 Earlier declaration of 'masses'
#include <vector>std::vector<Mass> masses;masses.reserve(10);masses.push_back(0.1);I got this error:
[C++ Error] Unit1.h(83): E2303 Type name expected
[C++ Error] Unit1.h(83): E2139 Declaration missing ;
or when I use them at the top after #include <vector>, I get:
[C++ Error] Unit1.h(23): E2238 Multiple declaration for 'masses'
[C++ Error] Unit1.h(21): E2344 Earlier declaration of 'masses'
#include <vector>class Mass{// *** mass definition ***};std::vector<Mass*> masses;for (int i = 0; i < NUMBER_OF_MASSES; i++) masses.push_back(new Mass(0.1));should create a vector with NUMBER_OF_MASSES mass objects in it. Each of the masses will have a radius of 0.1. The push_back call tells the vector to add a new object to the end of the vector. Accessing a particular element can be done using the standard [] operator. So, getting the 10th mass in the vector would be: masses[10].
Tom
EDIT: to prevent the first error you got, you must declare the Mass class before the Mass vector is created, or the compiler won't know what a 'Mass' is.
I think I am getting close:
I used the below code at the top:
#include "vector/Mass.h"
#include <vector>
std::vector<Mass> masses;
then I used :
the error now is :
[C++ Error] Unit1.cpp(4215): E2285 Could not find a match for 'vector<Mass,allocator<Mass> >::push_back(Mass *)'
I used the below code at the top:
#include "vector/Mass.h"
#include <vector>
std::vector<Mass> masses;
then I used :
masses.reserve(10); for (int i = 0; i <= 10; i++) masses.push_back(new Mass());the error now is :
[C++ Error] Unit1.cpp(4215): E2285 Could not find a match for 'vector<Mass,allocator<Mass> >::push_back(Mass *)'
Why use a pointer when you don't need to?
Enigma
// Mass.h#ifndef PART3DH_NMB#define PART3DH_NMB #include "Vector.h"const float DEG2RAD = 3.14159/180;class Mass{ public: Vector pos; Mass(float radius); void Draw(); private: float radius_;};#endif// Mass.cpp#include "Mass.h"#include <cmath>//#include <windows.h> // if on windows#include <gl\gl.h>Mass::Mass(float radius) : pos(0,0,0), radius_(radius){}void Mass::Draw(){ using namespace std; glPushMatrix(); glColor3f(4.0, 1.0, 4.0); glTranslatef(pos.x ,pos.y,pos.z); glBegin(GL_LINE_LOOP); for (int i = 0; i < 360; ++i) { float degInRad = i * DEG2RAD; glVertex2f(cos(degInRad) * radius_, sin(degInRad) * radius_); } glEnd(); glPopMatrix();}#endif// whatever.cpp#include <algorithm>#include <functional>#include <vector>#include "Mass.h"void someFunction(){ using namespace std; vector< Mass > masses; masses.reserve(100); masses.push_back(Mass(10.0f)); // add a mass with radius 10 masses.insert(masses.end(), 33, Mass(5.0f)); // add 33 masses each with radius 5 masses.push_back(Mass(10.0f)); // add another mass with radius 10 masses.insert(masses.end(), 65, Mass(2.5f)); // add 65 masses each with radius 2.5 for_each(masses.begin(), masses.end(), mem_fun_ref(&Mass::Draw)); // draw all the masses}Enigma
Quote:
Original post by bargasteh
I think I am getting close...
@dimebol your just confusing him now lol
@bargasteh: he's storing pointers instead, you don't need to do that and i recommend you don't unless you need to which in this case you don't.
The reason why it didn't work the last time when you tried to do it like me was you changed your constructor back to a default constructor. To make it work now its just:
#include <vector>std::vector<Mass> masses(number_of_elements);Makes a vector with "number_of_elements" number of elements where all elements are default constructed.
Quote:
Original post by bargasteh
the error now is :
[C++ Error] Unit1.cpp(4215): E2285 Could not find a match for 'vector<Mass,allocator<Mass> >::push_back(Mass *)'
Lol, this was my fault. I coplicated matters and introduced a nasty little '*' in the vector creation. Go with enigma and forget about the pointer (by ignoring the '*' in my code, and removing the 'new', see enigma's post).
Tom
EDIT: snk_kid beat me :) I guess we all managed to get this thread much longer than it aught have been...
Thank you so much everybody, the problem is solved now.
The only problem which is left now is not relevent to C++ its more relevent to OpenGl I think, but you guys might know how to solve them.
When I creat the mass objects I dont want to draw them on screen in my openGl initialisation, I would rather prefer to creat and draw them while my openGl is up and running, not before, in this way I can use the power of dynamic array.
Oh, sorry about this question, but what is the differece between C and C++ dynamic array?
Thank you so much
The only problem which is left now is not relevent to C++ its more relevent to OpenGl I think, but you guys might know how to solve them.
When I creat the mass objects I dont want to draw them on screen in my openGl initialisation, I would rather prefer to creat and draw them while my openGl is up and running, not before, in this way I can use the power of dynamic array.
Oh, sorry about this question, but what is the differece between C and C++ dynamic array?
Thank you so much
Quote:
Original post by bargasteh
When I creat the mass objects I dont want to draw them on screen in my openGl initialisation, I would rather prefer to creat and draw them while my openGl is up and running, not before, in this way I can use the power of dynamic array.
Well you don't, you just call your Mass::draw method when you want its still better (if you can) to create all you Mass instances at app initialization time.
Quote:
Original post by bargasteh
Oh, sorry about this question, but what is the differece between C and C++ dynamic array?
C-style dynamic arrays have a lot of thorny issues in C++ (a whole topic in and itself) to overcome those issues requires very messy low-level code (its not as simple as new[] then delete[] as some people may think).
std::vector almost completely eliminates those issues all nicely wrapped up with virtually no lose of efficiency/generality, std::vector is just as if not more efficient than typical usage of C-style dynamic arrays in C++.
All good C++ programmers and C++ Gurus will tell you to in general prefer std::vector over C-style dynamic arrays and prefer std::basic_string (and its type alias std::string) over C-style mutable strings.
To find out more about some of the C++ standard library components (like std::vector) check here it is also worth investigating boost libraries aswell.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement