Declaring an object in header file - c++

I created a class called coordinate
I want to create a vector of coordinate objects called cooPos to use it in another class called ScribbleArea. (It is from a Qt Example).
For some reason I always get the error:
error: cooPos was not declared in this scope
coordinates.h
#ifndef COORDINATE_H
#define COORDINATE_H
#include <iostream>
#include <string>
class coordinate
{
public:
coordinate(float y, float x, bool isPressed);
float get_x();
float get_y();
std::string get_coordinateText();
private:
float x_pos;
float y_pos;
bool isPressedState;
};
#endif // COORDINATE_H
coordinate.cpp
#include "coordinate.h"
coordinate::coordinate(float y, float x, bool isPressed)
{
y_pos = y;
x_pos = x;
isPressedState = isPressed;
}
std::string coordinate::get_coordinateText()
{
std::string output;
output = "X: " + std::to_string(x_pos) + " Y: " + std::to_string(y_pos) +"\n";
return output;
}
float coordinate::get_x()
{
return x_pos;
}
float coordinate::get_y()
{
return y_pos;
}
scribblearea.h
#ifndef SCRIBBLEAREA_H
#define SCRIBBLEAREA_H
#include <QColor>
#include <QImage>
#include <QPoint>
#include <QWidget>
#include <iostream>
#include <vector>
#include "coordinate.h"
class ScribbleArea : public QWidget
{
Q_OBJECT
public:
ScribbleArea(QWidget *parent = 0);
private:
void addCoordinate(float x, float y, bool isPressed);
std::vector<coordinate> cooPos;
};
#endif
scribbleare.cpp
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#include <QPrintDialog>
#endif
#include "scribblearea.h"
ScribbleArea::ScribbleArea(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
}
void addCoordinate(float x, float y, bool isPressed)
{
coordinate *actPos = new coordinate(y,x,isPressed);
cooPos.push_back(*actPos);
}
I have really no idea...

Related

Trying to draw in Qt Creator, using an object as an input

I am just creating a simple program which displays a point where a car object is, and updates where this point is using a timer so that later it is possible for it to move.
My code is currently just displaying the point at (0, 0), and I can't work out why.
My mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include "twovector.h"
#include "car.h"
#include "vehicle.h"
#include <vector>
#include <QTimer>
#include <QStandardItem>
#include <QPainter>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fCar = new Car(TwoVector(150, 2), 4);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(paintEvent()));
// connect(timer, SIGNAL(timeout()), this, SLOT(Drive()));
timer->start(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Drive(){
fCar->Drive(fCar->GetVelocity());
}
void MainWindow::paintEvent(QPaintEvent *){
QPainter painter(this);
QPen DotPen(Qt::red);
DotPen.setWidth(10);
painter.setPen(DotPen);
//painter.drawPoint(0, 0);
painter.drawPoint((fCar->GetPosition().GetRadius())*(cos(fCar->GetPosition().GetAngle()))
, (fCar->GetPosition().GetRadius())*(sin(fCar->GetPosition().GetAngle())));
//Draw the dot, where the centre of the window is (0,0)
}
The mainwindow header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "car.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void Drive();
void paintEvent(QPaintEvent *);
private:
Ui::MainWindow *ui;
Car *fCar;
};
#endif // MAINWINDOW_H
vehicle.ccp (from which the car class inherits from)
#include "vehicle.h"
#include "twovector.h"
#include <iostream>
using namespace std;
Vehicle::Vehicle(){
}
Vehicle::Vehicle(TwoVector position, double velocity){
fPosition = position;
fVelocity = velocity;
}
Vehicle::~Vehicle(){
}
void Vehicle::SetValue(string ValueName, double Value) {
if(ValueName.compare("Radius") == 0)
fPosition.SetRadius(Value);
else{
if(ValueName.compare("Angle") == 0)
fPosition.SetAngle(Value);
else if(ValueName.compare("Velocity") == 0)
fVelocity = Value;
else
cerr << "Unknown field entered: " << ValueName << endl;
}
}
void Vehicle::Drive(int velocity){
fPosition.SetAngle(fPosition.GetAngle() + (velocity)/(fPosition.GetRadius()));
}
vehicle.h:
#ifndef VEHICLE_H
#define VEHICLE_H
#include "twovector.h"
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle();
Vehicle(TwoVector position, double velocity);
~Vehicle();
inline TwoVector GetPosition() {return fPosition;}
inline double GetVelocity() {return fVelocity;}
inline void SetPosition(TwoVector position) {fPosition = position;}
void SetValue(string ValueName, double Value);
void Drive(int velocity);
private:
TwoVector fPosition;
double fVelocity;
};
#endif // VEHICLE_H
Twovector.ccp:
#include "twovector.h"
TwoVector::TwoVector(){
fRadius = 0;
fTheta = 0;
}
TwoVector::TwoVector(double radius, double theta){
fRadius = radius;
fTheta = theta;
}
TwoVector::~TwoVector(){
}
TwoVector TwoVector::operator +=(TwoVector position1){
TwoVector position2;
//Creates a new object which is given a position
position2.fRadius = sqrt(((fRadius)*(fRadius))+((position1.fRadius)*(position1.fRadius))
+ 2*((position1.fRadius)*(fRadius))*cos((position1.fTheta)-(fTheta)));
position2.fTheta = fTheta + atan2((position1.fRadius)*(sin((position1.fTheta)-(fTheta))),
fRadius + (position1.fRadius)*(cos((position1.fTheta)-fTheta)));
return(position2);
//New position returned
}
TwoVector.h:
#ifndef TWOVECTOR_H
#define TWOVECTOR_H
#include <math.h>
class TwoVector {
public:
TwoVector();
TwoVector(double radius, double theta);
~TwoVector();
inline double GetX() {return fRadius*cos(fTheta);}
inline double GetY() {return fRadius*sin(fTheta);}
inline double GetRadius() const {return fRadius;}
inline double GetAngle() const {return fTheta;}
//Accessor functions, these simply return the value of the coordinates
inline void SetRadius(double radius) {fRadius = radius;}
inline void SetAngle(double theta) {fTheta = theta;}
inline void SetRadiusAndAngle(double radius, double theta) {
fRadius = radius, fTheta = theta;}
//Mutator function to change the position
TwoVector operator += (TwoVector);
//Operator overloading so that vectors can be added
private:
double fRadius;
double fTheta;
};
#endif // TWOVECTOR_H
car.h:
#ifndef CAR_H
#define CAR_H
#include "twovector.h"
#include "vehicle.h"
#include <iostream>
using namespace std;
class Car: public Vehicle {
public:
Car();
Car(TwoVector position, double velocity);
~Car();
private:
TwoVector fPositioncar;
double fVelocitycar;
};
#endif // CAR_H
car.cpp:
#include "car.h"
Car::Car(){
}
Car::Car(TwoVector position, double velocity){
fPositioncar = position;
fVelocitycar = velocity;
}
Car::~Car(){
}
Any help will be appreciated!
Do not call the paintEvent method directly, you must do it through the update() method and as in your case you want to update the position, it is advisable to make the following modifications:
[...]
QTimer *timer = new QTimer(this);
//connect(timer, SIGNAL(timeout()), this, SLOT(paintEvent()));
connect(timer, SIGNAL(timeout()), this, SLOT(Drive()));
timer->start(10);
[...]
void MainWindow::Drive(){
fCar->Drive(fCar->GetVelocity());
update();
}
[...]
Another problem is that you have not called the parent's constructor:
car.cpp
#include "car.h"
Car::Car():Vehicle(){
}
Car::Car(TwoVector position, double velocity):Vehicle(position, velocity){
fPositioncar = position;
fVelocitycar = velocity;
}
Car::~Car(){
}
Also velocity is a value of type double, on the other hand Drive is configured for integer, you must change it to:
vehicle.cpp
[...]
void Vehicle::Drive(double velocity){
fPosition.SetAngle(fPosition.GetAngle() + (velocity)/(fPosition.GetRadius()));
}
The complete project with the modifications can be seen in the following link.

Qt: Custom derived class not declared in scope

Qt Newbie here experiencing a problem where it claims that I did not declare a class which I'm pretty sure is declared.
I cannot for the life of me figure this out. Please help.
Commenting out the problematic lines in Connection.h and .cpp results in compilable output.
Here is a slimmed down version of my code where the problem still exists:
Btw, Map.ui only has a Graphics View dragged into it.
Error message:
In file included from ../Map/Map.h:17:0,
from ../Map/City.cpp:1:
../Map/Connection.h:14:11: error: 'City' was not declared in this scope
QPair<City*, City*> cities;
^
../Map/Connection.h:14:18: error: 'City' was not declared in this scope
QPair<City*, City*> cities;
^
../Map/Connection.h:14:23: error: template argument 1 is invalid
QPair<City*, City*> cities;
^
../Map/Connection.h:14:23: error: template argument 2 is invalid
../Map/Connection.h:19:22: error: 'City' was not declared in this scope
Connection(QPair<City*, City*> cities, int cost);
^
../Map/Connection.h:19:29: error: 'City' was not declared in this scope
Connection(QPair<City*, City*> cities, int cost);
^
../Map/Connection.h:19:34: error: template argument 1 is invalid
Connection(QPair<City*, City*> cities, int cost);
Map.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Map
TEMPLATE = app
SOURCES +=\
City.cpp \
Connection.cpp \
Map.cpp \
Driver.cpp
HEADERS += \
City.h \
Connection.h \
Map.h
FORMS += \
Map.ui
Map.h:
#ifndef MAP_H
#define MAP_H
#include <QApplication>
#include <QGraphicsItem>
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QDebug>
#include <QPainter>
#include <QString>
#include <QVector>
#include "ui_Map.h"
#include "Connection.h"
#include "City.h"
namespace Ui
{
class Map;
}
class Map : public QMainWindow
{
Q_OBJECT
public:
explicit Map(QWidget *parent = 0);
~Map();
private:
Ui::Map *ui;
QGraphicsScene *scene;
};
#endif // MAP_H
Map.cpp:
#include "Map.h"
#include "ui_Map.h"
Map::Map(QWidget *parent) : QMainWindow(parent), ui(new Ui::Map)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
City *c1 = new City(30, 30, 30, "K");
City *c2 = new City(30, 90, 30, "O");
Connection *conn1 = new Connection(45, 45, 45, 105, 1);
c1->setZValue(4);
c2->setZValue(4);
conn1->setZValue(2);
scene->addItem(c1);
scene->addItem(c2);
scene->addItem(conn1);
}
Map::~Map()
{
delete ui;
}
City.h:
#ifndef CITY_H
#define CITY_H
#include "Map.h"
class City : public QGraphicsItem
{
private:
int x;
int y;
int r;
QString name;
QRectF bounds;
QVector<QPair<City*, int> > neighbors;
public:
City();
City(int x, int y, int r, QString name);
int getX() { return this->x; }
int getY() { return this->y; }
int getR() { return this->r; }
QString getName() { return this->name; }
QVector<QPair<City*, int> > getNeighbors() { return this->neighbors; }
void setNeighbors(QVector<QPair<City*, int> >) { this->neighbors = neighbors; }
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // CITY_H
City.cpp:
#include "Map.h"
City::City()
{
this->bounds = QRectF(0, 0, 0, 0);
}
City::City(int x, int y, int r, QString name)
{
this->x = x;
this->y = y;
this->r = r;
this->name = name;
this->bounds = QRectF(x, y, r, r);
}
QRectF City::boundingRect() const
{
return QRectF(x, y, r, r);
}
void City::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec;
if (this->bounds == QRectF(0, 0, 0, 0))
rec = boundingRect();
else
rec = this->bounds;
QBrush brush(Qt::blue);
QPen pen(Qt::blue);
painter->setBrush(brush);
painter->setPen(pen);
painter->drawEllipse(rec);
}
Connection.h:
#ifndef CONNECTION_H
#define CONNECTION_H
#include "Map.h"
class Connection : public QGraphicsItem
{
private:
int x1, x2;
int y1, y2;
int cost;
QRectF bounds;
QPair<City*, City*> cities; // PROBLEMATIC LINE -- CLAIMS CITY WAS NOT DECLARED
public:
Connection();
Connection(int x1, int y1, int x2, int y2, int cost);
Connection(QPair<City*, City*> cities, int cost); // PROBLEMATIC LINE -- CLAIMS CITY WAS NOT DECLARED
int getCost() { return this->cost; }
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // CONNECTION_H
Connection.cpp:
#include "Map.h"
Connection::Connection()
{
this->bounds = QRectF(0, 0, 0, 0);
}
Connection::Connection(int x1, int y1, int x2, int y2, int cost)
{
this->x1 = x1;
this->x2 = x2;
this->y1 = y1;
this->y2 = y2;
this->bounds = QRectF(x1, x2, y1, y2);
}
Connection::Connection(QPair<City*, City*> cities, int cost) // PROBLEMATIC BLOCK
{
int r = cities.first->getR();
this->x1 = cities.first->getX() + r/2;
this->x2 = cities.second->getX() + r/2;
this->y1 = cities.first->getY() + r/2;
this->y2 = cities.second->getY() + r/2;
this->cost = cost;
this->bounds = QRectF(x1, x2, y1, y2);
}
QRectF Connection::boundingRect() const
{
return QRectF(0, 0, 0, 0);
}
void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec;
if (this->bounds == QRectF(0, 0, 0, 0))
rec = boundingRect();
else
rec = this->bounds;
QBrush brush(Qt::red);
QPen pen(Qt::red);
painter->setPen(pen);
painter->drawLine(QLine(this->x1, this->y1, this->x2, this->y2));
}
Driver.cpp:
#include "Map.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Map w;
w.show();
return a.exec();
}
Thank you for taking the time to look at this. I appreciate any feedback whatsoever. The problematic lines were compilable for a while but it seems that adding certain things "broke" it.
The answer to this thread got me thinking it might be something silly, but my attempts to change the order of things have not fixed it.
Just looking at the error message one can see that the City class is indeed not defined at Connection.
In file included from ../Map/Map.h:17:0,
from ../Map/City.cpp:1:
City.cpp starts out with a #include "Map.h" which is odd because there are no references to the Map type in the cpp file. City.cpp should start out with an #include "City.h".
City.h starts out with an #include "Map.h" which is even stranger because the City class make no mention of Map.
So at this point City.cpp:1 -> Map.h :17 you are including "Connection.h". At this point, there are no defined types. Not City, not Map. The compiler approaches the class Connection with no types defined.
Fixing it:
It is hard to guess how your dependencies actually look like but one solution for this particular include might be.
Start by including City.h first in your City.cpp. You should #include "X.h" for all X.cpp files.
Remove the #include "Map.h" from the City.h file.
If the Map type is required in City.cpp, include it.
The Connection.h obviously requires at least a forward declared class class City because of the pointers. So either do a forward declaration or an `#include "City.h"
In conclusion: Only include files in the header that your class actually requires. In the corresponding cpp, first include the header. Any type the header requires will also be required by the implementation. After that you include the additional types your implementation requires.

C++ Sfml text class Unhandled exception at 0x0F58155F (sfml-graphics-d-2.dll)

Hello I add Text Class to my project , where text is fallowing the moving ball
i build is successful, but when i trying debuging i receive
Unhandled exception at 0x0F58155F (sfml-graphics-d-2.dll) in Lotto.exe: 0xC0000005: Access violation reading location 0xCCCCCD24.
My main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Ball.h"
#include "Line.h"
#include "TextBall.h"
using namespace std;
using namespace sf;
int main()
{
RenderWindow win(VideoMode(800, 600), L"RozdziaƂ 1");
Clock stoper;
Font font;
font.loadFromFile("arial.ttf");
float ySpeed = -100;
float xSpeed = -100;
Ball CircleOne(win);
Line linia1(win);
linia1.sie(500, 500);
linia1.position(20, 20);
linia1.thiness(2);
TextBall text(win);
text.dFont(font);
text.dString("Hello");
text.dCharacterSize(40);
text.dColor(sf::Color::Black);
text.dPosition(CircleOne.GetPosition().x + 5, CircleOne.GetPosition().y);
CircleOne.SetPozition(100, 100);
// CircleOne.SetOutlineColor(Color::Red);
CircleOne.Fil(Color::Yellow);
CircleOne.Promien(15);
// CircleOne.thinesS();
while (win.isOpen())
{
win.clear(Color::White);
Event e;
while (win.pollEvent(e))
{
if (e.type == Event::Closed)
win.close();
}
auto dt = stoper.restart().asSeconds();
CircleOne.Move(xSpeed *dt , ySpeed * dt);
//text.Move(xSpeed *dt, ySpeed * dt);
linia1.draw();
CircleOne.Draw();
text.Draw();
int positionY = CircleOne.GetPosition().y;
if (positionY <= 0){ ySpeed = -ySpeed; }
if (positionY >= 600.0-2*CircleOne.radius()){ ySpeed = -ySpeed; }
int positionX = CircleOne.GetPosition().x;
if (positionX <= 0){ xSpeed = -xSpeed; }
if (positionX >= 800.0-2*CircleOne.radius()){xSpeed = -xSpeed; }
win.display();
}
}
and TextBall class
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
using namespace sf;
class TextBall{
public:
TextBall(RenderWindow&);
void dString(String);
void dCharacterSize(int);
void dColor(Color);
void dPosition(float, float);
void Move(float, float);
void Draw();
void dFont(Font);
private:
Text text;
RenderWindow& Render;
};
TextBall::TextBall(sf::RenderWindow& app) : Render(app)
{
}
void TextBall::dString(String liczba)
{
text.setString(liczba);
}
void TextBall::dCharacterSize(int size)
{
text.setCharacterSize(size);
}
void TextBall::dColor(Color kolor)
{
text.setColor(kolor);
}
void TextBall::dPosition(float x, float y)
{
text.setPosition(x, y);
}
void TextBall::Move(float a, float b)
{
text.move(a, b);
}
void TextBall::Draw()
{
Render.draw(text);
}
void TextBall::dFont(Font fon)
{
text.setFont(fon);
}
When i add text without a additional class its working fine, i afraid it object sending in TextBall is faulty. Plaese how can i solve this project

No matching constructor for initialization of "Type"

I am writing a board game using C++ (with SFML2.2 library, Xcode 6.1.1). I created two classes: block and board. The idea is to create a 4x4 vector of blocks inside board. The codes are below:
In block.h
#ifndef __Game1024__block__
#define __Game1024__block__
#include <stdio.h>
#include <iostream>
#endif /* defined(__Game1024__block__) */
using namespace std;
class block{
public:
sf::RectangleShape rect;
sf::Text text;
block();
block(const int X,const int Y,const double size, const double blockSize, const double charSize, const int value, const sf::Color fillColor, const sf::Color textColor);
};
In block.cpp
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include "block.h"
using namespace std;
block::block(){}
block::block(const int X,const int Y,const double size, const double blockSize, const double charSize, const int value, const sf::Color fillColor, const sf::Color textColor){
double centerX = (X+0.5)*size;
double centerY = (Y+0.5)*size;
stringstream ss;
ss << value;
string sval = ss.str();
rect.setSize(sf::Vector2f(blockSize,blockSize));
rect.setOrigin(blockSize/2.0, blockSize/2.0);
rect.setPosition(centerX, centerY);
rect.setFillColor(fillColor);
text.setStyle(sf::Text::Bold);
text.setCharacterSize(charSize);
text.setString(sval);
text.setColor(textColor);
text.setOrigin(text.getLocalBounds().width/2.0, text.getLocalBounds().height);
text.setPosition(centerX, centerY);
}
In board.h
#ifndef __Game1024__board__
#define __Game1024__board__
#include <stdio.h>
#endif /* defined(__Game1024__board__) */
#include <vector>
#include "block.h"
using namespace std;
class board{
private:
int winSizeX;
int winSizeY;
int margin;
double charSize;
public:
vector<vector<block>> matrix;
board();
board(int winSizeX_,int winSizeY_,int margin_, double charSize_);
void draw();
};
And in board.cpp
#include "board.h"
//#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "block.h"
using namespace std;
board::board(){}
board::board(int winSizeX_,int winSizeY_,int margin_, double charSize_){
winSizeX = winSizeX_;
winSizeY = winSizeY_;
margin = margin_;
charSize = charSize_;
matrix = vector<vector<block>>(4,vector<block>(4));
double size = winSizeX/4.0; // = 256
double blockSize = size - 2*margin;
const sf::Color fillColor = sf::Color::Red;
const sf::Color textColor = sf::Color::White;
sf::Font font;
if(!font.loadFromFile("./sansation.ttf")){
exit(EXIT_FAILURE);
};
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
int value = 4*j + i + 1;
matrix[i][j] = block(i,j,size,blockSize, charSize,value,fillColor,textColor);
matrix[i][j].text.setFont(font);
matrix[i][j].text.setOrigin(matrix[i][j].text.getLocalBounds().width/2.0,b[i][j].text.getLocalBounds().height);
}
}
}
The error comes from board.cpp
matrix[i][j] = block(i,j,size,blockSize, charSize,value,fillColor,textColor);
One weird thing is when I typed matrix[i][j] = block, the autofill of my IDE (Xcode) will generate
matrix[i][j] = block(<#const int X#>, <#const int Y#>, <#const double size#>, <#const double blockSize#>, <#const double charSize#>, <#const int value#>, <#const int fillColor#>, <#const int textColor#>)
The last two arguments of constructor block are changed from type sf::Color into int.
If I create a 4x4 vector of blocks in the main.cpp file and call the constructor of block, there is no error...
Any help?
Thanks in advance!
[Update] I attached the scrrenshot of the error message
Thanks #molbdnilo
I made a stupid mistake that I forgot to include the SFML header in "block.h"
#include <SFML/Graphics.hpp>
Now problem solved! Thanks for all advices. I am new to C++ projects, please feel free to speak out all my bad practices in the code. Very helpful!

Inititalze vector of custom class?

I have the following the 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();
}
And then I have the following in main.cpp. The problem is with the line vector<Rectangle> rectangles; I get the following error:
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;
I'm at a loss for what's up. I think I've done my class correctly. If anyone could tell me what's wrong, I'd appreciate it.
Here's Point.h and 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;
}
}
You're including <Windows.h>, which will pollute the global namespace with many, many declarations, including a function called Rectangle that clashes with your class.
If you actually need the Windows API, your best option is probably to put all your stuff inside a namespace.