gpt4 book ai didi

c++ - 初始化自定义类的 vector ?

转载 作者:行者123 更新时间:2023-11-28 08:10:20 24 4
gpt4 key购买 nike

我有以下类(class):

//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;

我不知道发生了什么事。我想我已经正确地完成了我的类(class)。如果有人能告诉我出了什么问题,我将不胜感激。

这是 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;
}
}

最佳答案

您包括 <Windows.h> ,这会用很多很多的声明污染全局命名空间,包括一个名为 Rectangle 的函数。这与你的类(class)冲突。

如果您确实需要 Windows API,最好的选择可能是将所有内容放在命名空间中。

关于c++ - 初始化自定义类的 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com