C++ template problem

Member
Posts: 61
Joined: 2007.01
Post: #1
Hi,

I'm trying to convert some Win32 and GLUT code to run on my MBP, after working my way through 49 bugs in the code, I'm now down to 6 - and they are all of the same type. Annoyed

Being predominantly a Java ME/ J2SE programmer with about 8 weeks of C++, my knowledge of templates is not great but I'm reading every tutorial I can find!

However, I'd be really grateful if someone could explain to me what the problem is or give me a bit of advice.

These are the errors:

Quote:GameObj.h:14: error: template argument required for 'class Point3'
GameObj.h:14: error: template argument required for 'class Point3'
GameObj.h:15: error: template argument required for 'class Sphere3'
GameObj.h:15: error: template argument required for 'class Sphere3'
Ship.h:8: error: template argument required for 'class Sphere3'
Ship.h:8: error: template argument required for 'class Sphere3'

Because this code is from a book I'm not sure if I can put it all online so I'll put in what I think is relevant.

From GameObj.h

Code:
#ifndef __GAMEOBJ
#define __GAMEOBJ
#include <math.h>
#include "Point3.h"
#include "Sphere3.h"
#pragma warning(disable: 4786)
#include <vector>
#include <list>

#define NO_LIFE_TIMER 99999

using namespace std;

class Point3;                // compiler error here
class Sphere3;             // compiler error here

class GameObj
{
   ...
}

From Point3.h

Code:
#pragma once
#include <assert.h>

template <class FLTYPE> class Point3
{
    public:

              FLTYPE v[3];
              typedef Point3<FLTYPE> Point3_FT;
        
        ...

From Ship.h

Code:
#ifndef __SHIP
#define __SHIP

#include "GameObj.h"

class Bullet;
class Control;
class Sphere3;        // compiler error here

#define MAX_SHOT_LEVEL    3
#define MAX_SHIP_SPEED    120
#define MAX_AG_SHIP_SPEED 120
#define MAX_TRACTOR_DIST  180
#define MAX_TRACTOR_POWER 300


class Ship : public GameObj

From Sphere3.h

Code:
#pragma once
#include <assert.h>
#include "Point3.h"

template <class FLTYPE> class Sphere3
{
  public:

    Point3<FLTYPE> c;

    FLTYPE r;

Thanks in advance for any help!

Anthony
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Just delete the lines that are causing the errors; they don't appear to be necessary.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
When you just acknowledge the existence of the classes like that, you also need to include the template parameters.
Quote this message in a reply
Member
Posts: 61
Joined: 2007.01
Post: #4
Thanks OneSadCookie,

I tried that and it got rid of the 6 compile errors but it has resulted in two odd errors.

Quote:Ast.cpp:21: error: 'glTranslate' was not declared in this scope
Bullet.cpp:41: error: 'glTranslate' was not declared in this scope

m_position is a variable that all Game Objects use to keep track of their position in the game.

From GameObj.h

Code:
...
Point3f        m_position;    
...

This is the code from Ast.cpp

Code:
void Ast::Draw()
{
    //just a circle
    glPushMatrix();
    glDisable(GL_LIGHTING);

    glTranslate(m_position);    // compiler error here

    glRotatef(m_angle,0,0,1);
    glScalef(m_size,m_size,m_size);
    glBegin(GL_LINE_LOOP);
    glColor3f(.7,.6,.6);

    const int nside=18;

    for(int i=0;i<=nside;i++)

        glVertex3f(cos(float(i)*M_PI*2.0/nside),sin(float(i)*M_PI*2.0/nside),0.0);

    glEnd();

    glEnable(GL_LIGHTING);
    glPopMatrix();
}

And from Bullet.cpp

Code:
void Bullet::Draw()
{
    glPushMatrix();
    glDisable(GL_LIGHTING);

    glTranslate(m_position);    // compiler error here

    glRotatef(m_angle,0,0,1);
        glScalef(m_size,m_size,m_size);

    glBegin(GL_LINES);
    glColor3f(.5, 1, 1);
    glVertex3f(4.0,0,0);
    glColor3f(0.,.3,.3);
    glVertex3f(0.0,0,0);

    glEnd();

    glEnable(GL_LIGHTING);
    glPopMatrix();

}

If it comes to it I'll just re-write the draw methods, but if anyone can say what exactly is causing this scope error I'd really appreciate it. I've included glut.h and the other variables of the GameObject class in Ast.cpp and Bullet.cpp don't cause any errors.

I'm puzzled as to how a function can be out of scope!

Sad
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #5
Doesn't it need to be glTranslatef or glTranslated?
Quote this message in a reply
Member
Posts: 61
Joined: 2007.01
Post: #6
Quote:Doesn't it need to be glTranslatef or glTranslated?

Sorry i should have mentioned that there are 2 functions written that take care of translations for Point3f variables.

Code:
inline void glTranslate(Point3<float> const & p) { glTranslatef(p.v[0],p.v[1],p.v[2]);};
inline void glTranslate(Point3<double> const & p){ glTranslated(p.v[0],p.v[1],p.v[2]);};

I'm wrecked tired at this point, i'll slog through all this tomorrow.

Thanks for all your help guys!
Quote this message in a reply
Yomoyomo
Unregistered
 
Post: #7
Hey Anthony,
it'd be glTranslatef, or glTranslated that you're looking for, as maximile said, as for your original template problem:

using namespace std;

class Point3; // compiler error here
class Sphere3; // compiler error here
etc,

You've got to write the template directive:

template<class T>
class Point3;
template<class T>
class Sphere3;

etc.

Just replace T with whatever the template uses
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #8
anthony Wrote:Sorry i should have mentioned that there are 2 functions written that take care of translations for Point3f variables.

Code:
inline void glTranslate(Point3<float> const & p) { glTranslatef(p.v[0],p.v[1],p.v[2]);};
inline void glTranslate(Point3<double> const & p){ glTranslated(p.v[0],p.v[1],p.v[2]);};

I'm wrecked tired at this point, i'll slog through all this tomorrow.

Thanks for all your help guys!

Your Bullet and AST draw methods don't know about your glTranslate functions. Just include the header/cpp file with those functions at the top of Bullet.cpp and AST.cpp and it should work. It's kind of hard to tell without the code however.
Quote this message in a reply
Member
Posts: 61
Joined: 2007.01
Post: #9
Yomoyomo and Blacktiger thanks for your replies.

Quote:Just include the header/cpp file with those functions at the top of Bullet.cpp and AST.cpp and it should work.

I tried this and I'm still getting the same errors, I'm beginning to tear my hair out at this point with frustration and I'm close to giving up Mad

Here is the code for the Point3 class where the functions that aren't being found are declared:

Code:
#pragma once

#include <assert.h>

template <class FLTYPE> class Point3
{
    public:

    FLTYPE v[3];

    typedef Point3<FLTYPE> Point3_FT;

       ...

       ...

      ...

      ...

}; // end class definition


typedef Point3<short>  Point3s;
typedef Point3<int>       Point3i;
typedef Point3<float>  Point3f;
typedef Point3<double> Point3d;

#ifdef ___GL_H__
#define ___GL_H__
inline void glVertex(Point3<int> const & p)   { glVertex3iv(p.v);};
inline void glVertex(Point3<short> const & p) { glVertex3sv(p.v);};
inline void glVertex(Point3<float> const & p) { glVertex3fv(p.v);};
inline void glVertex(Point3<double> const & p){ glVertex3dv(p.v);};
inline void glNormal(Point3<int> const & p)   { glNormal3iv(p.v);};
inline void glNormal(Point3<short> const & p) { glNormal3sv(p.v);};
inline void glNormal(Point3<float> const & p) { glNormal3fv(p.v);};
inline void glNormal(Point3<double> const & p){ glNormal3dv(p.v);};
inline void glTexCoord(Point3<int> const & p)   { glTexCoord3iv(p.v);};
inline void glTexCoord(Point3<short> const & p) { glTexCoord3sv(p.v);};
inline void glTexCoord(Point3<float> const & p) { glTexCoord3fv(p.v);};
inline void glTexCoord(Point3<double> const & p){ glTexCoord3dv(p.v);};
inline void glTranslate(Point3<float> const & p) { glTranslatef(p.v[0],p.v[1],p.v[2]);};
inline void glTranslate(Point3<double> const & p){ glTranslated(p.v[0],p.v[1],p.v[2]);};
inline void glScale(Point3<float> const & p) { glScalef(p.v[0],p.v[1],p.v[2]);};
inline void glScale(Point3<double> const & p){ glScaled(p.v[0],p.v[1],p.v[2]);};
#endif

If I understand the directives being used, it's saying that if _GL__H_ is defined (does that mean GL.h? the Windows library? then the functions below are included by the compiler - right?

I tried changing it to _GLUT__H_ to see what would happen but it made no difference I still got the same scope errors.

If anyone is kind enough to take the time to look I'll mail them the code (PM me your address), I'd be spectacularly grateful for any sort of help like that - as I've run out of ideas with these f**king errors!
Quote this message in a reply
Member
Posts: 61
Joined: 2007.01
Post: #10
Grin

I finally got it to work!

It took a lot of hacking, copying and pasting but it all compiled and ran.

I think there are some bugs in the way the game plays or maybe they could be due to the way I sliced and diced the code.

I'm glad i stuck with it in the end!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Problem Instantiating Template Class in C++ brockwoolf 2 3,239 Jan 13, 2009 11:54 PM
Last Post: bmantzey