typedef and switch - why this doesn't work?

Hello

This is more of a C++ question: I want to draw a group of cubes twice, once as solids, and once as wireframes. I have created a typedef type called drawMode, and now I am trying to use the switch block so that the application decides when to use glutSolidCube() and when to use glutWireCube().
But apparently the switch block complains “solid(my typedef variable) cannot appear in a contan-expression”.
Why?

Definition of my typedef variables (testApp.h)

  
typedef unsigned int drawMode;  
            drawMode lines;  
            drawMode solid;  
  
        void drawCubes(drawMode dMode);  
  

Using these variables to determine draw Mode (switch block)

  
void testApp::drawCubes(drawMode dMode){  
  
      
    for (int i=0; i<ARRAYSIZE; i++){  
          
            ofSetColor(myCube[i].col.r,   
                       myCube[i].col.g,   
                       myCube[i].col.b,   
                       128);  
              
            ofPushMatrix();  
  
            ofTranslate(myCube[i].pos.x,   
                        myCube[i].pos.y,   
                        myCube[i].pos.z);  
                          
            ofRotateX(myCube[i].rot.x);  
            ofRotateY(myCube[i].rot.y);  
            ofRotateZ(myCube[i].rot.z);  
              
            switch(dMode) {  
                case solid:  
                glutSolidCube(myCube[i].size);  
                break;  
                  
                case lines:  
                myCube[i].col.r -= 20;  
                myCube[i].col.g -= 20;  
                myCube[i].col.b -= 20;  
                glutWireCube(myCube[i].size);  
                myCube[i].col.r += 20;  
                myCube[i].col.g += 20;  
                myCube[i].col.b += 20;  
                break;  
              
            }  
                  
        ofPopMatrix();  
          
        if(rotOnOff) {  
        myCube[i].rot.x += myCube[i].incRot;  
        myCube[i].rot.y += myCube[i].incRot;  
        myCube[i].rot.z += myCube[i].incRot;  
        };  
          
    };  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofSetWindowTitle("Cubes");  
  
	drawCubes(solid);  
	drawCubes(lines);  
  
	ofSetColor(253, 253, 253);  
	ofDrawBitmapString("FPS :: "+ ofToString(ofGetFrameRate()), 50, 50);  
}  
  
  
  

Thanks in advance!

Beatriz :mrgreen:

I will answer myself to the question:

This is because the case statement in the switch block requires constants, like e.g.

  
switch (mode) {  
case  '1' : //string literal   
    blahblah;  
    break;  
case 'q' : //character constant  
    blahblah;  
    break;  
}  
  

Also you can define a constant in the preprocessor with #define. That’s what I did, and it works, though I wonder why the if, else if else if sequence didn’t work for me…

  
/*  
testApp.cpp  
*  
*  
*  
*/  
#define SOLID 1  
#define LINES 2  
  
#include "testApp.h"  
  
  
void testApp::drawCubes(const int dMode){  
  
      
    for (int i=0; i<ARRAYSIZE; i++){  
          
            ofSetColor(myCube[i].col.r,   
                       myCube[i].col.g,   
                       myCube[i].col.b,   
                       128);  
              
            ofPushMatrix();  
  
            ofTranslate(myCube[i].pos.x,   
                        myCube[i].pos.y,   
                        myCube[i].pos.z);  
                          
            ofRotateX(myCube[i].rot.x);  
            ofRotateY(myCube[i].rot.y);  
            ofRotateZ(myCube[i].rot.z);  
              
            switch(dMode) {  
                case SOLID:  
                glutSolidCube(myCube[i].size);  
                break;  
                  
                case LINES:  
                myCube[i].col.r -= 20;  
                myCube[i].col.g -= 20;  
                myCube[i].col.b -= 20;  
                glutWireCube(myCube[i].size);  
                myCube[i].col.r += 20;  
                myCube[i].col.g += 20;  
                myCube[i].col.b += 20;  
                break;  
              
            }  
                  
        ofPopMatrix();  
          
        if(rotOnOff) {  
        myCube[i].rot.x += myCube[i].incRot;  
        myCube[i].rot.y += myCube[i].incRot;  
        myCube[i].rot.z += myCube[i].incRot;  
        };  
          
    };  
  
}  
  
  

Cheers

Beatriz :mrgreen:

i think what you need are enums. Easier than #defines. #defines are evil and not recomended.

  
  
  
enum DrawMode {SOLID, LINES};  
  
void drawCubes(DrawMode draw_mode);   
  
  

and later

  
  
  
void drawCubes(DrawMode draw_mode) {  
  
  switch(draw_mode) {  
    case DrawMode::SOLID:  
       ...  
       break;  
    case DrawMode::LINES:  
       ...  
       break;  
  }  
}  
  

HTH, Stephan

LOL

thanks a lot Stephan! This helps indeed

Beatriz :mrgreen: