Jump to content
  • Advertisement
Sign in to follow this  
bargasteh

Problem with creating object array

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I have this object :
#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

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.

Share this post


Link to post
Share on other sites
Advertisement
  1. You should specify the type for the radius in your Mass constructor, i.e. Mass(float radius).

  2. 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thank you, is it correct now?



#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 ();
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();
}

#endif



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

Share this post


Link to post
Share on other sites
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:

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.

Share this post


Link to post
Share on other sites
When I used :


#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'

Share this post


Link to post
Share on other sites

#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.

Share this post


Link to post
Share on other sites
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 :

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 *)'

Share this post


Link to post
Share on other sites
Why use a pointer when you don't need to?
// 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!