Sign in to follow this  

Self Contained Classes

This topic is 3665 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

Can anyone tell me what i'm doing wrong? I was told if I declare my Textures class[Textures Textures;] in my source file, and then externally declare it AFTER my class definition [extern Textures Textures;] in the header file, all I have to do to link to the Textures Class in other header files is add #include "textures.h", and I won't have to redeclare the the class externally. textures.cpp
#include "textures.h"

Textures  Textures;

AUX_RGBImageRec *LoadBMP(const char *Filename)
{
	FILE *File=NULL;

	if (!Filename)
	{
      MessageBox(NULL,"Bitmap Loading Failure\nNo Filename\nFunction: AUX_RGBImageRec()\nLocation: textures.cpp\n","Window.ClassName",MB_ICONEXCLAMATION|MB_OK);
      return false;
	}

	File=fopen(Filename,"r");

	if (File)
	{
		fclose(File);
		return auxDIBImageLoad(Filename);
	}
	return NULL;
}

bool Textures::LoadTextures()
{
    AUX_RGBImageRec *TextureImage[textures];
	memset(TextureImage,0,sizeof(void *)*(textures));

    if ((TextureImage[0]=LoadBMP("textures/grass.bmp")) &&
        (TextureImage[1]=LoadBMP("textures/sky.bmp")))
        {
		glGenTextures((textures), &texture[0]);

		for (int loop=0; loop<textures; loop++)
		{
   	      glBindTexture(GL_TEXTURE_2D, texture[loop]);
		  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
		  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        }

		for (int loop=0; loop<textures; loop++)
		{
	      if (TextureImage[loop])
	      {
		    if (TextureImage[loop]->data)
		    {
        	  free(TextureImage[loop]->data);
		    }
		    free(TextureImage[loop]);
	      }
        }
    }
    else
	{
      MessageBox(NULL,"Texture Loading Failure\nTextureImage[]\nFunction: LoadTextures()\nLocation: textures.cpp\n","Window.ClassName",MB_ICONEXCLAMATION|MB_OK);
      return false;
	}
}

textures.h
#ifndef TEXTURES_H
#define TEXTURES_H

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>

AUX_RGBImageRec*  LoadBMP(const char*);

class Textures
{
  private:
  public:
    Textures()
    {
          texture[2];
          textures             = 2;
    }
          GLuint	  texture[];
          int         textures;
          bool        LoadTextures();
};

extern Textures Textures;

#endif

generic.h
#include "textures.h"

// I NOW SHOULD BE ABLE TO USE Texture CLASS VARIABLES IN THE SOURCE FILE?


Any ideas? I'm using C++/OpenGL on Dev-cpp

Share this post


Link to post
Share on other sites
I don't think you can have a variable with the exact same name (including case) as a type. Try putting the variable in lowercase, for example.

That said:

Quote:

// I NOW SHOULD BE ABLE TO USE Texture CLASS VARIABLES IN THE SOURCE FILE?


Not variables of the class; members of the object. Your class defines a type. You make a thing, 'textures', which is of the type 'Textures'. When you write 'int x = 5;', you set the value of x to 5, not "the value of int to 5".

Share this post


Link to post
Share on other sites
Completely wrong.

Start with Textures Textures. In C++ (and C, and most other languages), each symbolic identifier must be unique. If you have a type (class) called Textures, you can't also have a variable called Textures.

You don't need extern here, anyway. You need two files: Textures.h and Textures.cpp. In Textures.h, you provide your class definition, with member function declarations only:


class Textures
{
public:
Textures(); // declaration
~Textures(); // declaration

void SomeFunction(); // declaration
void SomeOtherFunction(int a); // declaration

private:
// member variables go here
};



In Textures.cpp:


#include "Textures.h"

Textures::Textures()
{
// definition
}

Textures::~Textures()
{
// definition
}

Textures::SomeFunction()
{
// definition
}

Textures::SoemOtherFunction(int a)
{
// definition
}



Now, you can include Textures.h in any other file, header or implementation, and as long as you compile Textures.cpp along with the other implementation files and link in the resulting object file, it'll work.

Read this for more information.

Share this post


Link to post
Share on other sites

This topic is 3665 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this