-
classes and vectors
i guess C++ is always the best source of problems, when you don't have any.. :D
i've finished my polygon classes, after checking examples and several idea.
i'm trying to make now a polygon manager class which will handle all polygons in the world, and have two vectors: one for all of them, and one only for the current view (it's kind of like 3D space partitioning).
but, i'm having a problem right at the beginning. I don't get it what am i doing wrong:
Code:
class PolyMan
{
PolyMan();
std::vector<Poly> plist;
};
throws me
Code:
In file included from Lesson1.cpp:16:
polym.h:28: error: using-declaration for non-member at class scope
make.exe: *** [Lesson1.o] Error 1
Execution terminated
where 28 is the line where i'm declaring plist.
what is it wrong?
-
What are some lines before that?
Generally, when you get an error that seems to make no sense at all, check the code before that - often, the compiler got confused by some earlier thing.
-
Change using namespace::std; to using namespace std; just above the class definition.
Im obviously shooting in the dark here. But it is so common :)
-
Because you are prefixing the vector with std:: anyway (which is the right thing to do), you shouldn't need any using declarations at all. At least not in header files.
-
well
here's the main part
Code:
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include "poly.h" //polygon class
#include "polymanager.h" //polygon manager
#include <vector>
using namespace std;
polymanager.h
Code:
class PolyMan
{
public:
PolyMan();
vector<Poly> plist;
};
(polymanager.cpp only has a null constructor)
poly.h
Code:
//the three dimensional point, so usefull for 3D stuff :)
struct p3d
{
float x;
float y;
float z;
void Set(float mx, float my, float mz);
};
class Poly
{
public:
int cnum; //corner count
p3d *corners; //the actual corners
Poly(); //constructor
void Set(p3d *p, size_t size);
void List();
void Render();
void Move(float x, float y, float z);
~Poly();
};
i can also post poly.cpp, but it's rather big.
-
I assume that's where you define everything ;)
-
mm.. yes :)
what exactly should i post?
sorry.. i'm pretty noob in c++.
here's everything in main, before functions begin:
Code:
/*
* This Code Was Created By Jeff Molofee 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
* And Optimizing This Code, Making It More Flexible!
* If You've Found This Code Useful, Please Let Me Know.
* Visit My Site At nehe.gamedev.net
*/
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include "poly.h" //polygon class
#include "polymanager.h" //polygon manager
#include <vector>
using namespace std;
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
float ang=0;
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
Poly patrat[4];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
it's actually NeHe's framework for opengl.
and here's my class: (poly.cpp)
Code:
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include "poly.h"
void p3d::Set(float mx, float my, float mz)
{
x=mx;y=my;z=mz;
}
Poly::Poly ()
{
};
void Poly::Set(p3d *p, size_t size)
{
cnum=size;
corners=new p3d[cnum];
for (int i=0;i<size;i++) {
corners[i]=p[i];
}
}
void Poly::Render()
{
//a bit of optimisation
if (cnum==3) {glBegin(GL_TRIANGLES);}
if (cnum==4) {glBegin(GL_QUADS);}
if (cnum>4) {glBegin(GL_POLYGON);}
for (int i=0;i<=cnum;i++) {
glColor3f(corners[i].x*50,corners[i].y*50,corners[i].z*50);
glVertex3f(corners[i].x,corners[i].y,corners[i].z);
}
glEnd();
}
void Poly::Move(float nx, float ny, float nz)
{
for (int i=0;i<=cnum;i++) {
corners[i].x+=nx;
corners[i].y+=ny;
corners[i].z+=nz;
}
}
Poly::~Poly() {};
-
poly.h and polymanager.h are missing header include guards, but I doubt that caused the error you are currently trying to fix. You use vector inside polymanager.h, so you need to #include <vector> inside that header before the class is declared. You also need to refer to it as std::vector, or provide a using declaration or using directive inside that file so the compiler knows that vector is part of the std namepsace. The error message is confusing, but that might be all it needs.
-
hm, well.
Code:
//poly manager
#pragma once
#include <vector>
#include "polymanager.h"
using namespace std;
PolyMan::PolyMan()
{
};
and .h
Code:
class PolyMan
{
public:
PolyMan();
vector<Poly> plist;
};
i get
Code:
In file included from Lesson1.cpp:16:
polymanager.h:5: error: ISO C++ forbids declaration of `vector' with no type
polymanager.h:5: error: expected `;' before '<' token
make.exe: *** [Lesson1.o] Error 1
Execution terminated
mrr.. this is making me dizzy.
-
solved. 'twas the darn namespaces.
-
You should put the #pragma once, #include <vector> and the using directive inside the header file, not the cpp file. That is where vector is being used. When the polymanager.h file is included in other places (like Lesson1.cpp) it won't have those includes there. The polymanager.cpp file is completely ignored when compiling Lesson1.cpp.