2012-02-16 107 views
0

我有类以下几点:初始化自定义类的向量?

//Rectangle.h 
#include "Point.h" 
class Rectangle{ 

public: 
    Rectangle(); 

    void setWidth(int); 
    void setHeight(int); 

    int getWidth(); 
    int getHeight(); 

    void draw(Point); 

private: 
    int m_width; 
    int m_height; 

}; 

//Rectangle.cpp 
#include <Windows.h> 
#include <iostream> 
#include <GL/Gl.h> 
#include <GL/Glu.h> 
#include <GL/Glut.h> 

#include "Rectangle.h" 
#include "Point.h" 

//constructor 
Rectangle::Rectangle(){ 

} 

int Rectangle::getHeight(){ 
    return m_height; 
} 

int Rectangle::getWidth(){ 
    return m_width; 
} 

void Rectangle::setHeight(int a_height){ 
    m_height = a_height; 
} 

void Rectangle::setWidth(int a_width){ 
    m_width = a_width; 
} 

void Rectangle::draw(Point center){ 

    int pointWidth = m_width/2; 
    int pointHeight = m_height/2; 

    std::cout << "drawing rectangle at" << center.getX() << ", " << center.getY() << std::endl; 
    glColor3f(1, 1, 0); 

    glBegin(GL_POLYGON); 

    glVertex2i(center.getX() - pointWidth, center.getY() - pointHeight); 
    glVertex2i(center.getX() + pointWidth, center.getY() - pointHeight); 
    glVertex2i(center.getX() + pointWidth, center.getY() + pointHeight); 
    glVertex2i(center.getX() - pointWidth, center.getY() + pointHeight); 

    glEnd(); 
} 

然后,我已经中的main.cpp以下。问题是与线vector<Rectangle> rectangles;我得到以下错误:

std::vector : Rectangle is not a valid template type argument for parameter '_Ty'

#include <Windows.h> 
#include <math.h> 
#include <string> 
#include "Rectangle.h" 
#include "Point.h" 
#include "Pentagon.h" 
#include "Star.h" 
#include "StringManip.h" 
#include <iostream> 
#include <fstream> 
#include <vector> 
#include <GL/Gl.h> 
#include <GL/Glu.h> 
#include <GL/Glut.h> 

using namespace std; 

// Function prototypes for callbacks 
void myDisplay(void); 
void myInit(); 

void openFile(string); 

Point point; 
vector<Point> points; 
vector<Rectangle> rectangles; 
vector<Pentagon> pentagons; 
vector<Star> stars; 

我在什么是一个损失。我想我已经正确地完成了我的课程。如果有人能告诉我什么是错的,我会很感激。

这里的Point.h和Point.cpp

//Point.h 
#ifndef POINT_H 
#define POINT_H 

class Point { 

public: 

    //default constructor 
    Point() { 
     m_xCoord = 0; 
     m_yCoord = 0; 
    } 

    //set x and y 
    void setX(int a_x); 
    void setY(int a_y); 

    //get x and y 
    int getX(); 
    int getY(); 

    //change x and y by a specified value. 
    void changeX(int a_x); 
    void changeY(int a_y); 

private: 
    int m_xCoord; 
    int m_yCoord; 
}; 


#endif 

//Point.cpp 
#include "Point.h" 

void Point::setX(int a_x){ 
    m_xCoord = a_x; 
} 

void Point::setY(int a_y){ 
    m_yCoord = a_y; 
} 

int Point::getX(){ 
    return m_xCoord; 
} 

int Point::getY(){ 
    return m_yCoord; 
} 

void Point::changeX(int a_x){ 
    m_xCoord += a_x; 

    if(m_xCoord < 0){ 
     m_xCoord = 0; 
    } 
} 

void Point::changeY(int a_y){ 
    m_yCoord += a_y; 

    if(m_yCoord < 0){ 
     m_yCoord = 0; 
    } 
} 
+0

你可以显示'Point.h'(你包括它的方式,这是不必要的两次)。 – pmr 2012-02-16 22:54:58

+0

好的。刚刚编辑我的文章 – Cuthbert 2012-02-16 22:58:19

+0

看起来你实际上比''Rectangle'获得别的东西。你会遇到这个错误,例如如果'Rectangle'是一个模板。 – 2012-02-16 23:02:56

回答

1

你包括<Windows.h>,这会污染很多很多的声明,其中包括一个名为Rectangle功能,与你的类相冲突的全局命名空间。

如果您确实需要Windows API,那么您最好的选择可能是将所有东西放入命名空间中。

+0

普通程序员怎么知道这一点?谁是如此邪恶,并在全局命名空间中放置一个名为“Rectangle”的东西? – pmr 2012-02-16 23:27:24