# Problem Using Object Oriented c++

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

## Recommended Posts

I have been trying to learn object oriented programming in c plusplus I seem to do ok with console programs, but I can't seem to make anything work when I try to use it with glut/Open GL. I can use glut and open GL by themselves ok although I still have a lot to learn about it. Below is the code where I am getting 2 compiler errors. I have added comments where I am getting the errors. I can't see any problem but it won't compile. It is always possible that even when I get past that, more errors could pop up. Much Thanks up front to anyone who will help me with this. L.J. Bush
void circle::draw(){
int i;

glPushMatrix();
glEnable(GL_LINE_SMOOTH);
glLineWidth(2);
glBegin(GL_LINE_LOOP);
for(i=0; i <= pnts; i++){
float angle = 2 * PI * 0.5 / pnts; // expected primary-expression before '=' token                glNormal3f(cos(angle), 0, -sin(angle));
angle = 2*PI * i / pnts;          // expected primary-expression before '=' token
glVertex3f(Cx,Cy * -cos(angle),Cz * sin(angle));
}
glEnd();
glPopMatrix();
}


##### Share on other sites
Please verify that angle isn't defined as a macro.

##### Share on other sites
Your problem lies in the for loop, perhaps you've not defined i? You need to add 'int'.

##### Share on other sites
Quote:
 Original post by ShakedownYour problem lies in the for loop, perhaps you've not defined i? You need to add 'int'.

The i is defined at the very beginning of the routine body as an int variable (unfortunately the OP shows a blank line between it and the remaining body as well as some confusing indentation, so it can be overlooked simply).

##### Share on other sites
No I don't think angle is a macro, as far as I know it is just a variable.
I tried using int inside with i=0 and got rid of the indentations,
but that did no good.
I am using cmath and defining pi in my header file.Thanks Jody Bush

##### Share on other sites
Try using a GLfloat instead of a float.

##### Share on other sites
Quote:
 Original post by haegarrPlease verify that angle isn't defined as a macro.

yes.

##### Share on other sites
I'd guss you wrote something like this in your PI macro:
#define PI = 3.141562654;
when it should be just:
#define PI 3.141562654

Either that or 'angle' is a macro. Try renaming it to 'angle2' and see if that fixes it.

##### Share on other sites
Thanks guys. thanks iMalc, you were right I did define PI with an assignment operator,even I should have known better than that. The program did compile
but it has a bug. I get a window that has whatever was behind it in it.
I have two more member functions,one for setting the x,y,z position of the circle,the radius,and the number of circle points, and one to set the color. I must have something wrong there I don't know. I hacked over this several days thinking I could figure it out before I came over here for help,but I have not been able to get it.I did try the other suggestions but they did not help.Here is the code for the other two functions.All of you guys were right about the macro, I had no idea that PI was a macro. Much thanks L.J. Bush
void circle::set(GLfloat x, GLfloat y, GLfloat z, float r, int pnts){          Cx = x;      Cy = y;      Cz = z;      radius = r;            circlePoints = pnts;    }     void circle::setColor(GLfloat r,GLfloat g,GLfloat b){     glColor3f(r,g,b);     }

##### Share on other sites
Seeing the now covered screen section as background in an OpenGL view hints at using double buffering but not having swapped the background (a.k.a. render) buffer to the foreground after rendering. Also don't forget to initialize and invoke glClear(...).

There is another problem I see with your circle::setColor(...) routine. A routine with such a signature is commonly used as so-called "setter" and is normally expected to store the overhanded values in member variables of the circle class, so that they are associated with the particular circle instance. Instead, your implementation only invokes glColor3f(...). So that color is set as current OpenGL color, but that will be active only up until the next invocation of glColor. There is especially no definite association of the color with the circle instance.

Hence I suggest the following: Let circle::setColor(...) store the color in member variables. Add belonging getters to the circle class. Let the render routine circle::draw(..) invoke glColor with the member variables as arguments.

Furthur, I'm unhappy that C++ does not introduce more restrict rules on spellings. That hat lead to a half dozen of variations. People often tend to adopt the writing seen in the book learning C++ from. However, it seems me that most people prefer to start class names with uppercase letters. Please consider to do so, too; e.g. let the class be named "Circle."

##### Share on other sites
Well I finally got this thing to compile with the get functions but the program is still not drawing anything, all I am getting is a black screen which is the color I set the background to. I really don't think my get functions are correct because from what I remember reading about functions is that you could only return 1 thing and mine are returning the three parameters that I am asking for. I had my formula wrong inside my for loop for my circle so I changed that and it works properly if I don't use the class.I was already using member variables as pivate.I declared my 3 circle objects in the main file on a global scope,and did the setting and drawing operations in the display function in the main file. I had already cleared the screen and swapped the buffers. Below is the code I have now. If you would like I can post all of the code from each of the 3 files, it is not really a great deal of code because this was just a practice program for me to try to learn OOP with Glut/Open GL.

Here is the complete class info from my circle.hpp file
class Circle                     {   private:      GLfloat x, y, z;           //coordinates of center      float Cx, Cy, Cz;       float radius;      int circlePoints,pnts;      GLfloat r,g,b;             //color         public:                          void set(GLfloat x, GLfloat y, GLfloat  z, float r, int pnts);      void setColor(GLfloat r, GLfloat g, GLfloat b);      float get(GLfloat x, GLfloat y, GLfloat z, float r, int pnts);      float getColor(GLfloat r, GLfloat g, GLfloat b);      void draw();         };

Here is the complete code from my circle.cpp file
 #include "circle.hpp"void Circle::setColor(GLfloat r, GLfloat g, GLfloat b){  glColor3f(r, g, b);   }void Circle::set(GLfloat x, GLfloat y, GLfloat z, float r, int pnts){          Cx = x;      Cy = y;      Cz = z;      radius = r;            circlePoints = pnts;     }float Circle::get(GLfloat x, GLfloat y, GLfloat z, float r, int pnts){      return  x, y, z, r, pnts;      } float Circle::getColor(GLfloat r, GLfloat g, GLfloat b){      return  r, g, b;      }   void Circle::draw(){      float get();   float getColor();    glColor3f(r, g, b);   glPushMatrix();      glEnable(GL_LINE_SMOOTH);   glLineWidth(2);                             glBegin(GL_LINE_LOOP);   for(int i=0; i <= pnts; i++){   float angle = 2 * PI * r / pnts;   glNormal3f(cos(angle), 0, -sin(angle));   angle = 2*PI * i / pnts;   glVertex3f(Cx+r * cos(angle),Cy+r * sin(angle),Cz);   }   glEnd();   glPopMatrix();    }

If you like I can post all or part of the code fom the main file
Thank all of you for all of the help you have given me so far, I really do appreciate it. Much Thanks, Jody Bush

##### Share on other sites
You have several ways of returning the color:

(Notice 1st please that you should use more elaborate names. E.g. you use the member variable r for both the radius and the red color channel. Use e.g. radius, colorR, ... Notice 2nd the use of the const specifier.)

(1) One getter for each element:
float Circle::getColorR() const {   return this->colorR;}float Circle::getColorG() const {   return this->colorG;}float Circle::getColorB() const {   return this->colorB;}

(2) References:
void Circle::getColor(float& red,float& green,float& blue) const {   red = this->colorR;   green = this->colorG;   blue = this->colorB;}

(AFAIK this isn't a getter in the strict sense, but it works fine.)

(3) Compound structure:
struct RGB {   float R,G,B;};RGB Circle::getColor() const {   return this->color;}RGB Circle::color;

I've omitted initializers and similar stuff of RGB here. Usually RGB isn't nested with Circle but available by its own.

(4) Member access:
struct RGB {   float R,G,B;};const RGB& Circle::color() const {   return this->color;}RGB Circle::color;

what is also not a getter in the common sense. Especially this version requires the RGB class itself to be written more carefully than I did for this examples.

Some combinations of the above are possible, too.

Coming to the drawing routine, the belonging accesses were

(1)
void Circle::draw() {      glColor3f(getColorR(), getColorG(), getColorB());   ...}

(2)
void Circle::draw() {      float red, green, blue;   getColor(red, green, blue);   glColor3f(red, green, blue);   ...}

(3)
void Circle::draw() {      RGB rgb = getColor();   glColor3f(rgb.R, rgb.G, rgb.B);   ...}

(4)
void Circle::draw() {      const RGB& rgb = getColor();   glColor3f(rgb.R, rgb.G, rgb.B);   ...}

or directly (and with least effort, but not strict OOP style like)
void Circle::draw() {      glColor3f(this->colorR, this->colorG, this->colorB);   ...}

##### Share on other sites
Quote:
 Original post by bushimportsThanks guys. thanks iMalc, you were right I did define PI with an assignment operator,even I should have known better than that.

Yes, you should.

const double PI = 3.141592654;

##### Share on other sites
Hi Guys, Well it turns out that the program was drawing everything. I used a call to gl translatef() to push everything back 15 units and it showed up on the screen. Now I have a different problem. Even though my member variables are all private I was able to translate,scale,and rotate my circle objects with standard OpenGL calls,so my private member variables are not very encapsulated. I can set the parameters with either my class constructor or a setter,no getters are necessary. I have tried declaring my draw function as private but the only way the compiler will accept it is if I call it from the constructor. Do any of you have any clue what I could be doing wrong? Much Thanks, Jody Bush