PDA

View Full Version : Error: Experession cannot be evaluated within draw method :S



TimOgunmakin
04-03-2013, 12:01 PM
within my draw method in the planet class I'm using glTranslated to move the object in a circle (calculated in another method within the class) however I am receiving a run time error "Error: experession cannot be evaluated". I have messed around with this and what seems to allow the program to run is having -
"circle *loop = new circle[600];" within the draw method.
However when it compiles my planets do not appear. Please help!

Below is the Class.cpp file:


#include "Planet.h"
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"



Planet::~Planet()
{
cout << "Planet Destroyed.";
}

string Planet::getname()
{
return name;
}

position Planet::getPosition()
{
return pos;
}


float Planet::getRot()
{
return rot;
}

float Planet::getradius()
{
return radius;
}

float Planet::getslices()
{
return slices;
}

float Planet::getstacks()
{
return stacks;
}

float Planet::getRotIndex()
{
return rotIndex;
}

void Planet::getCalcPlanetCircle()
{
circle *loop = new circle[600];
for (int i = 0; i < 600 ; i++)
{
loop[i].x = radius * sin(i/100.0);
loop[i].y = 0;
loop[i].z = radius * cos(i/100.0);
}
}

void Planet::update()
{
speed = 0;
speed+= rotIndex;
if (speed >= 600)
{
speed = 0;
}
}

void Planet::draw()
{
glPushMatrix();
//circle *loop = new circle[600];
glColor3f(r,g,b);
glTranslated(loop[(int)(ceil (speed))].x, loop[(int)(ceil (speed))].y, loop[(int)(ceil (speed))].z);
//glTranslated(speed,speed,speed);
glRotated(60, 0, 0, 1);
glScalef(2, 2, 2);
glutSolidSphere(radius, slices, stacks);
glPopMatrix();
}


below is the class header file:


#ifndef PLANET_H
#define PLANET_H
#include <iostream>
#include <string>
#include "Structures.h"

using namespace std;

class Planet
{



private:
string name;
position pos;
circle *loop;
int slices;
int stacks;
int r;
int g;
int b;
float rot;
float radius;
float rotIndex;
float speed;


public:



Planet(string Pname, float Pradius, int Pslices, int Pstacks, position Ppos, float ProtIndex, int Pr, int Pg, int Pb)
: name(Pname), radius(Pradius), slices(Pslices), stacks(Pstacks), pos(Ppos), rotIndex(ProtIndex), r(Pr), g(Pg), b(Pb)
{
cout << "Planet " << Pname << " created" << endl;
}
~Planet();

string getname();
position getPosition();
float getRot();
float getradius();
float getslices();
float getstacks();
float getRotIndex();
void getCalcPlanetCircle();
void update();
void draw();
};
#endif

mobeen
04-03-2013, 06:12 PM
This is more a c++ thing. There is a very big memory leak going on here. Currently, you are creating a new local loop array variable in the getCalcPlanetCircle function and you never deallocate it. At the same time, you are accessing the uninitialized member loop variable in the draw function. I suggest you move the allocation/deallocation of your loop array that is
circle *loop = new circle[600]; into the Planet class constructor/destructor like this


Planet::Planet()
{
loop = new circle[600];
}
Planet::~Planet()
{
delete [] loop;
}

This way you don't allocate each frame. Then your getCalcPlanetCircle can directly use the loop array as follows


void Planet::getCalcPlanetCircle()
{
for (int i = 0; i < 600 ; i++)
{
loop[i].x = radius * sin(i/100.0);
loop[i].y = 0;
loop[i].z = radius * cos(i/100.0);
}
}

See if this helps.

TimOgunmakin
04-04-2013, 01:15 PM
Thanks for the reply mobeen, I have corrected my mistakes however I still seem to have the runtime error Experession cannot be evaluated. This error points me to the line "loop[i].x = radius * sin(i/100.0);" within my "Planet::getCalcPlanetCircle()" Method. Any ideas? :S

mobeen
04-04-2013, 05:54 PM
Could u share how the whole class and the other code looks now (I mean the new updated code so far) seems to me that there are still some uninitialized variables used.

TimOgunmakin
04-04-2013, 06:12 PM
I have moved the For loop to my constructor and removed the need to call the "getCalcPlanetCircle()" method from the main class. That resolved the problem however when I now call the Draw method within the Planet.cpp class Im getting a run time error on glTranslated. It complains about the Loop which is the same error as before. I cant do the same as I did to the getCalcPlanetCircle method :S


Main class:


// Draw planets
for (int i = 0; i < planetNum; i++)
{
planet[i]->draw();

}


Planet.h:


#ifndef PLANET_H
#define PLANET_H
#include <iostream>
#include <string>
#include "Structures.h"

using namespace std;

class Planet
{



private:
string name;
position pos;
circle *loop;
int slices;
int stacks;
int r;
int g;
int b;
float rot;
float radius;
float rotIndex;
float speed;


public:



Planet(string Pname, float Pradius, int Pslices, int Pstacks, position Ppos, float ProtIndex, int Pr, int Pg, int Pb)
: name(Pname), radius(Pradius), slices(Pslices), stacks(Pstacks), pos(Ppos), rotIndex(ProtIndex), r(Pr), g(Pg), b(Pb)
{
cout << "Planet " << Pname << " created" << endl;
circle *loop = new circle[600];
int derp = 5;
for (int i = 0; i < 600; i++)
{
loop[i].x = radius * sin(i/100.0);
loop[i].y = 0;
loop[i].z = radius * cos(i/100.0);
}



}

~Planet();
string getname();
position getPosition();
float getRot();
float getradius();
float getslices();
float getstacks();
float getRotIndex();
void getCalcPlanetCircle();
void update();
void draw();
};
#endif

Planet.cpp:


#include "Planet.h"
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"



Planet::~Planet()
{
cout << "Planet Destroyed.";
delete [] loop;
}

string Planet::getname()
{
return name;
}

position Planet::getPosition()
{
return pos;
}


float Planet::getRot()
{
return rot;
}

float Planet::getradius()
{
return radius;
}

float Planet::getslices()
{
return slices;
}

float Planet::getstacks()
{
return stacks;
}

float Planet::getRotIndex()
{
return rotIndex;
}

void Planet::getCalcPlanetCircle()
{

for (int i = 0; i < 600 ; i++)
{
loop[i].x = radius * sin(i/100.0);
loop[i].y = 0;
loop[i].z = radius * cos(i/100.0);
}
}

void Planet::update()
{
speed = 0;
speed+= rotIndex;
if (speed >= 600)
{
speed = 0;
}
}

void Planet::draw()
{
glPushMatrix();
glColor3f(r,g,b);
cout << loop[4].x << endl;
glTranslated(loop[(int)(ceil (speed))].x, loop[(int)(ceil (speed))].y, loop[(int)(ceil (speed))].z);
glRotated(60, 0, 0, 1);
glScalef(2, 2, 2);
glutSolidSphere(radius, slices, stacks);
glPopMatrix();
}

TimOgunmakin
04-04-2013, 06:16 PM
I have pinpointed the problem and found that I am not able to use Loop outside of the constructor :S how do I get around this?

mobeen
04-04-2013, 07:30 PM
You are still allocating a local variable



Planet(string Pname, float Pradius, int Pslices, int Pstacks, position Ppos, float ProtIndex, int Pr, int Pg, int Pb) : name(Pname), radius(Pradius), slices(Pslices), stacks(Pstacks), pos(Ppos), rotIndex(ProtIndex), r(Pr), g(Pg), b(Pb) {
cout << "Planet " << Pname << " created" << endl;
//circle *loop = new circle[600];//this is local to constructor
loop=new circle[600]; //this will initialize the loop member variable
int derp = 5;
for (int i = 0; i < 600; i++) {
loop[i].x = radius * sin(i/100.0);
loop[i].y = 0;
loop[i].z = radius * cos(i/100.0);
}
}

TimOgunmakin
04-04-2013, 07:44 PM
Ahhhhh that was my problem! Thanks alot :3