Custom 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:
Code:
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:
Code:
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:
Code:
#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;
}
{
Q_OBJECT
public:
~Map();
private:
Ui::Map *ui;
};
#endif // MAP_H
Map.cpp:
Code:
#include "Map.h"
#include "ui_Map.h"
{
ui->setupUi(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:
Code:
#ifndef CITY_H
#define CITY_H
#include "Map.h"
{
private:
int x;
int y;
int r;
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; }
};
#endif // CITY_H
City.cpp:
Code:
#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 {
}
{
if (this
->bounds
== QRectF(0,
0,
0,
0)) rec = boundingRect();
else
rec = this->bounds;
painter->setBrush(brush);
painter->setPen(pen);
painter->drawEllipse(rec);
}
Connection.h:
Code:
#ifndef CONNECTION_H
#define CONNECTION_H
#include "Map.h"
{
private:
int x1, x2;
int y1, y2;
int cost;
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; }
};
#endif // CONNECTION_H
Connection.cpp:
Code:
#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 {
}
{
if (this
->bounds
== QRectF(0,
0,
0,
0)) rec = boundingRect();
else
rec = this->bounds;
painter->setPen(pen);
painter
->drawLine
(QLine(this
->x1, this
->y1, this
->x2, this
->y2
));
}
Driver.cpp:
Code:
#include "Map.h"
int main(int argc, char *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.
Re: Custom class not declared in scope
Problem solved!
Solution from another help site.
Hopefully this is appropriate. If not, please delete.
Re: Custom class not declared in scope
It is customary to post the solution so others may benefit should they have a similar problem.