Инициализировать вектор пользовательского класса? - PullRequest
0 голосов
/ 17 февраля 2012

У меня есть следующий класс:

//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 не является допустимым аргументом типа шаблона для параметра _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; 
    }
}

1 Ответ

1 голос
/ 17 февраля 2012

Вы включаете <Windows.h>, который будет загрязнять глобальное пространство имен множеством объявлений, включая функцию с именем Rectangle, которая конфликтует с вашим классом.

Если вына самом деле нужен Windows API, ваш лучший вариант, вероятно, поместить все ваши вещи в пространство имен.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...