multiple definition's
Hello, i am using the nehe basecode(i think) with devc++, i tryed to add some OOP code to it, so that i can create objects and whatnot(Using one of the articles that i cant find anymore :P ) Anyway, so i have 2 new files, zoomer.h and zoomer.cpp, along with the functions
CZoomer* NewCZoomer();
DeleteAllCZoomer();
RenderCZoomer();
UpdateCZoomer();
All in the .h along with the class, then I have all the method functions inside the .cpp
but my problem is, I want use the functions inside the .h and .cpp in main.cpp(were main loop is) but the problem is when i add #include "zoomer.h" it gives an error saying its allready defined...and if i remove the line it says the functions arent defined...any ideas?
Try to put this in your zoomer.h file BEFORE any other code:
#IFNDEF __ZOOMER_H__
#DEFINE __ZOOMER_H__
And this AFTER all other code:
#ENDIF
That should make sure that the code is only defined in memory once.
Hope it helps...
#IFNDEF __ZOOMER_H__
#DEFINE __ZOOMER_H__
And this AFTER all other code:
#ENDIF
That should make sure that the code is only defined in memory once.
Hope it helps...
Quote: Original post by Leoncoeur
Try to put this in your zoomer.h file BEFORE any other code:
#IFNDEF __ZOOMER_H__
#DEFINE __ZOOMER_H__
And this AFTER all other code:
#ENDIF
That should make sure that the code is only defined in memory once.
Hope it helps...
In file included from main.cpp:19:
zoomer.h:2:2: invalid preprocessing directive #IFNDEF
zoomer.h:3:2: invalid preprocessing directive #DEFINE
zoomer.h:25:2: invalid preprocessing directive #ENDIF
Hmm...dont seem to work :S...thanks for your help
try making the prprocessor directives all lowercase:
#ifndef
#define
#endif
#ifndef
#define
#endif
--my site
try posting some code that has firstCZoomer so we can see how it's declared
--my site
umm ok, heres the files
kc.mindcasterstudios.com/files/zoomer.cpp
and
kc.mindcasterstudios.com/files/zoomer.h
kc.mindcasterstudios.com/files/zoomer.cpp
and
kc.mindcasterstudios.com/files/zoomer.h
In the .h file,
Change
to
and put
into the .cpp file.
Generally, if you want to share variables over the project, do not put them into the header, put them into the implementation (cpp) file and use an extern declaration like above in the header file.
Change
CZoomer * firstCZoomer =NULL;CZoomer * lastCZoomer =NULL;
to
extern CZoomer * firstCZoomer;extern CZoomer * lastCZoomer;
and put
CZoomer * firstCZoomer =NULL;CZoomer * lastCZoomer =NULL;
into the .cpp file.
Generally, if you want to share variables over the project, do not put them into the header, put them into the implementation (cpp) file and use an extern declaration like above in the header file.
STLport | Lua | Squirrel | Doxygen | NASM | bochs | osdev | Ruby | FreeBSD | Zend Framework 2 | YUI 3 | VP UML| ZFS | Linux Mint (Cinnamon)
I'd actually go further than that. The article that you're working from is using "C with objects" rather than C++ and is not truely object-oriented. This is a common mistake made by people moving from C to C++. There's nothing inherently wrong with it, but you must recognise that you are missing out on a lot of the benefits that C++ and object-oriented programming bring.
It looks like you're trying to write a class which provides a linked list of particles which can be updated rendered and deleted. The problems with it as written are that you can only have one particle system going at a time, you can create particles which are not part of the particle system without it being clear that that is what you are doing and it is very easy to leak particles. Grab your favourite C++ reference and I'll give you a whistle-stop tour of how I would write such a class:
Particle.h:
ParticleSystem.h:
Particle.cpp:
ParticleSystem.cpp:
Note that there is no need for a DeleteAllCZoomer equivalent. Each ParticleSystem will clean itself up automatically when it is destructed thanks to the use of std::list.
Enigma
It looks like you're trying to write a class which provides a linked list of particles which can be updated rendered and deleted. The problems with it as written are that you can only have one particle system going at a time, you can create particles which are not part of the particle system without it being clear that that is what you are doing and it is very easy to leak particles. Grab your favourite C++ reference and I'll give you a whistle-stop tour of how I would write such a class:
Particle.h:
// replace UNIQUE and IDENTIFIER with something likely to be unique to you, like your initials// do not use double underscores or an underscore followed by a capital// letter because such names are reserved for your compiler writers.// I prefer #if !defined to #ifndef since it more closely mimics the C/C++ if#if !defined(iUNIQUE_iIDENTIFIER_cPARTICLE) #define iUNIQUE_iIDENTIFIER_cPARTICLE// name your classes appropriately. Don't prefix classes with 'C'.// The user should not need to know whether he is using a class, a typedef or a builtin type.class Particle{ public: Particle(float size, float zoom); bool dead() const; void render() const; // be const correct void update(); // encapsulate data. External code should not care _HOW_ Particle does what it does, only that it does it. private: // append underscores to member variables to identify them. // This allows you to have easily named accessor functions, if required. bool dead_; float size_; float zoom_;};#endif
ParticleSystem.h:
#if !defined(iUNIQUE_iIDENTIFIER_cPARTICLE_cSYSTEM) #define iUNIQUE_iIDENTIFIER_cPARTICLE_cSYSTEM // use the standard c++ list class #include <list> #include "Particle.h"class ParticleSystem{ public: // use typedefs typedef std::list< Particle >::size_type size_type; void addParticle(Particle particle); size_type count() const; void render() const; void update(); private: std::list< Particle > particles_;};#endif
Particle.cpp:
#include "Particle.h"#include <windows.h>#include <GL/gl.h>Particle::Particle(float size, float zoom) : dead_(false), size_(size), zoom_(zoom){}bool Particle::dead() const{ return dead_;}void Particle::render() const{ glColor3f(1.0f,0.0f,0.0f); glEnable(GL_TEXTURE_2D); //glBindTexture(GL_TEXTURE_2D,::BlankTxt); //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); float width; float height; width = zoom_; height = zoom_; glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(-width,-height); glTexCoord2f(1,0); glVertex2f(+width,-height); glTexCoord2f(1,1); glVertex2f(+width,+height); glTexCoord2f(0,1); glVertex2f(-width,+height); glEnd(); glDisable(GL_TEXTURE_2D);}void Particle::update(){ //Add your code here //This is where you define the behaviour of your Particles}
ParticleSystem.cpp:
#include "ParticleSystem.h"#include <algorithm>#include <functional>void ParticleSystem::addParticle(Particle particle){ particles_.push_back(particle);}ParticleSystem::size_type ParticleSystem::count() const{ // trivial implementation compared to CountCZoomer return particles_.size();}void ParticleSystem::render() const{ // prefer std::for_each to explicit loops // call the render function of the Particle class for every particle in the system // trivial implementation compared to RenderCZoomer std::for_each(particles_.begin(), particles_.end(), std::mem_fun_ref(&Particle::render));}void ParticleSystem::update(){ // call the update function of the Particle class for every particle in the system std::for_each(particles_.begin(), particles_.end(), std::mem_fun_ref(&Particle::update)); // remove all dead particles particles_.remove_if(std::mem_fun_ref(&Particle::dead));}
Note that there is no need for a DeleteAllCZoomer equivalent. Each ParticleSystem will clean itself up automatically when it is destructed thanks to the use of std::list.
Enigma
okays,thanks for your help. well i got everything working fine, but for some reason using your particle system brings the FPS down to around 20 (from about 2000 without it) but olny when it render from the Particle system object...like here is my render function
I have one particle in the system...but as soon as i press space it drops the fps down ALOT...why is this? it stays up at 2000 normaly when space isent pressed....any ideas as to why this is?(and if i remove the if space is down check, it does lag. so its not that)
void Draw (DWORD milliseconds){ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); //glTranslatef(Winwidth/2,Winheight/2,0); //RenderCZoomer(); if(g_keys->keyDown [VK_SPACE]) PS.render(); glColor3f(1.0f,0.0f,0.0f); float width; float height; width = 100; height = 100; glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(-width,-height); glTexCoord2f(1,0); glVertex2f(+width,-height); glTexCoord2f(1,1); glVertex2f(+width,+height); glTexCoord2f(0,1); glVertex2f(-width,+height); glEnd(); glFlush ();}
I have one particle in the system...but as soon as i press space it drops the fps down ALOT...why is this? it stays up at 2000 normaly when space isent pressed....any ideas as to why this is?(and if i remove the if space is down check, it does lag. so its not that)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement