Thread: classes and vectors

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    29

    classes and vectors

    i guess C++ is always the best source of problems, when you don't have any..

    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?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    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.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I assume that's where you define everything
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    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() {};
    Last edited by izuael; 11-27-2006 at 03:26 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    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.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    solved. 'twas the darn namespaces.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 07:19 AM
  2. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05:11 AM
  3. Vectors + Classes = Confusing
    By Epo in forum C++ Programming
    Replies: 59
    Last Post: 12-18-2004, 04:42 PM
  4. vectors and classes
    By jimothygu in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 07:53 PM
  5. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 10:04 PM