Results 1 to 3 of 3

Thread: Custom class not declared in scope

  1. #1
    Join Date
    Mar 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question 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:
    Qt Code:
    1. In file included from ../Map/Map.h:17:0,
    2. from ../Map/City.cpp:1:
    3. ../Map/Connection.h:14:11: error: 'City' was not declared in this scope
    4. QPair<City*, City*> cities;
    5. ^
    6. ../Map/Connection.h:14:18: error: 'City' was not declared in this scope
    7. QPair<City*, City*> cities;
    8. ^
    9. ../Map/Connection.h:14:23: error: template argument 1 is invalid
    10. QPair<City*, City*> cities;
    11. ^
    12. ../Map/Connection.h:14:23: error: template argument 2 is invalid
    13. ../Map/Connection.h:19:22: error: 'City' was not declared in this scope
    14. Connection(QPair<City*, City*> cities, int cost);
    15. ^
    16. ../Map/Connection.h:19:29: error: 'City' was not declared in this scope
    17. Connection(QPair<City*, City*> cities, int cost);
    18. ^
    19. ../Map/Connection.h:19:34: error: template argument 1 is invalid
    20. Connection(QPair<City*, City*> cities, int cost);
    To copy to clipboard, switch view to plain text mode 

    Map.pro:
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = Map
    6. TEMPLATE = app
    7.  
    8. SOURCES +=\
    9. City.cpp \
    10. Connection.cpp \
    11. Map.cpp \
    12. Driver.cpp
    13.  
    14. HEADERS += \
    15. City.h \
    16. Connection.h \
    17. Map.h
    18.  
    19. FORMS += \
    20. Map.ui
    To copy to clipboard, switch view to plain text mode 

    Map.h:
    Qt Code:
    1. #ifndef MAP_H
    2. #define MAP_H
    3.  
    4. #include <QApplication>
    5. #include <QGraphicsItem>
    6. #include <QMainWindow>
    7. #include <QtCore>
    8. #include <QtGui>
    9. #include <QGraphicsScene>
    10. #include <QGraphicsEllipseItem>
    11. #include <QDebug>
    12. #include <QPainter>
    13. #include <QString>
    14. #include <QVector>
    15.  
    16. #include "ui_Map.h"
    17. #include "Connection.h"
    18. #include "City.h"
    19.  
    20. namespace Ui
    21. {
    22. class Map;
    23. }
    24.  
    25. class Map : public QMainWindow
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. explicit Map(QWidget *parent = 0);
    31. ~Map();
    32.  
    33. private:
    34. Ui::Map *ui;
    35. };
    36.  
    37. #endif // MAP_H
    To copy to clipboard, switch view to plain text mode 


    Map.cpp:
    Qt Code:
    1. #include "Map.h"
    2. #include "ui_Map.h"
    3.  
    4. Map::Map(QWidget *parent) : QMainWindow(parent), ui(new Ui::Map)
    5. {
    6. ui->setupUi(this);
    7.  
    8. scene = new QGraphicsScene(this);
    9. ui->graphicsView->setScene(scene);
    10.  
    11. City *c1 = new City(30, 30, 30, "K");
    12. City *c2 = new City(30, 90, 30, "O");
    13.  
    14. Connection *conn1 = new Connection(45, 45, 45, 105, 1);
    15.  
    16. c1->setZValue(4);
    17. c2->setZValue(4);
    18. conn1->setZValue(2);
    19.  
    20. scene->addItem(c1);
    21. scene->addItem(c2);
    22. scene->addItem(conn1);
    23. }
    24.  
    25. Map::~Map()
    26. {
    27. delete ui;
    28. }
    To copy to clipboard, switch view to plain text mode 


    City.h:
    Qt Code:
    1. #ifndef CITY_H
    2. #define CITY_H
    3.  
    4. #include "Map.h"
    5.  
    6. class City : public QGraphicsItem
    7. {
    8. private:
    9. int x;
    10. int y;
    11. int r;
    12. QString name;
    13. QRectF bounds;
    14. QVector<QPair<City*, int> > neighbors;
    15.  
    16. public:
    17. City();
    18. City(int x, int y, int r, QString name);
    19. int getX() { return this->x; }
    20. int getY() { return this->y; }
    21. int getR() { return this->r; }
    22. QString getName() { return this->name; }
    23. QVector<QPair<City*, int> > getNeighbors() { return this->neighbors; }
    24. void setNeighbors(QVector<QPair<City*, int> >) { this->neighbors = neighbors; }
    25. QRectF boundingRect() const;
    26. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    27.  
    28. };
    29.  
    30. #endif // CITY_H
    To copy to clipboard, switch view to plain text mode 

    City.cpp:
    Qt Code:
    1. #include "Map.h"
    2.  
    3. City::City()
    4. {
    5. this->bounds = QRectF(0, 0, 0, 0);
    6. }
    7.  
    8. City::City(int x, int y, int r, QString name)
    9. {
    10. this->x = x;
    11. this->y = y;
    12. this->r = r;
    13. this->name = name;
    14. this->bounds = QRectF(x, y, r, r);
    15. }
    16.  
    17. QRectF City::boundingRect() const
    18. {
    19. return QRectF(x, y, r, r);
    20. }
    21.  
    22. void City::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    23. {
    24. QRectF rec;
    25.  
    26. if (this->bounds == QRectF(0, 0, 0, 0))
    27. rec = boundingRect();
    28. else
    29. rec = this->bounds;
    30.  
    31. QBrush brush(Qt::blue);
    32. QPen pen(Qt::blue);
    33.  
    34. painter->setBrush(brush);
    35. painter->setPen(pen);
    36. painter->drawEllipse(rec);
    37. }
    To copy to clipboard, switch view to plain text mode 


    Connection.h:
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include "Map.h"
    5.  
    6. class Connection : public QGraphicsItem
    7. {
    8.  
    9. private:
    10. int x1, x2;
    11. int y1, y2;
    12. int cost;
    13. QRectF bounds;
    14. QPair<City*, City*> cities; // PROBLEMATIC LINE -- CLAIMS CITY WAS NOT DECLARED
    15.  
    16. public:
    17. Connection();
    18. Connection(int x1, int y1, int x2, int y2, int cost);
    19. Connection(QPair<City*, City*> cities, int cost); // PROBLEMATIC LINE -- CLAIMS CITY WAS NOT DECLARED
    20. int getCost() { return this->cost; }
    21. QRectF boundingRect() const;
    22. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    23. };
    24.  
    25.  
    26. #endif // CONNECTION_H
    To copy to clipboard, switch view to plain text mode 


    Connection.cpp:
    Qt Code:
    1. #include "Map.h"
    2.  
    3. Connection::Connection()
    4. {
    5. this->bounds = QRectF(0, 0, 0, 0);
    6. }
    7.  
    8. Connection::Connection(int x1, int y1, int x2, int y2, int cost)
    9. {
    10. this->x1 = x1;
    11. this->x2 = x2;
    12. this->y1 = y1;
    13. this->y2 = y2;
    14.  
    15. this->bounds = QRectF(x1, x2, y1, y2);
    16.  
    17. }
    18.  
    19. Connection::Connection(QPair<City*, City*> cities, int cost) // PROBLEMATIC BLOCK
    20. {
    21. int r = cities.first->getR();
    22. this->x1 = cities.first->getX() + r/2;
    23. this->x2 = cities.second->getX() + r/2;
    24. this->y1 = cities.first->getY() + r/2;
    25. this->y2 = cities.second->getY() + r/2;
    26.  
    27. this->cost = cost;
    28.  
    29. this->bounds = QRectF(x1, x2, y1, y2);
    30. }
    31.  
    32. QRectF Connection::boundingRect() const
    33. {
    34. return QRectF(0, 0, 0, 0);
    35. }
    36.  
    37. void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    38. {
    39. QRectF rec;
    40.  
    41. if (this->bounds == QRectF(0, 0, 0, 0))
    42. rec = boundingRect();
    43. else
    44. rec = this->bounds;
    45.  
    46. QBrush brush(Qt::red);
    47. QPen pen(Qt::red);
    48.  
    49. painter->setPen(pen);
    50. painter->drawLine(QLine(this->x1, this->y1, this->x2, this->y2));
    51. }
    To copy to clipboard, switch view to plain text mode 


    Driver.cpp:
    Qt Code:
    1. #include "Map.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Map w;
    7. w.show();
    8.  
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    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.
    Last edited by Eul; 8th March 2016 at 01:02.

  2. #2
    Join Date
    Mar 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Custom class not declared in scope

    Problem solved!

    Solution from another help site.

    Hopefully this is appropriate. If not, please delete.

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Custom class not declared in scope

    It is customary to post the solution so others may benefit should they have a similar problem.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Not declared in scope
    By mikea in forum Newbie
    Replies: 3
    Last Post: 4th October 2014, 08:06
  2. endl' was not declared in this scope
    By k.qasempour in forum Newbie
    Replies: 8
    Last Post: 21st June 2012, 09:36
  3. ui not declared in scope
    By steve.bush in forum Newbie
    Replies: 5
    Last Post: 19th March 2011, 08:58
  4. GetFileSizeEx was not declared in this scope
    By Threepwood in forum Qt Programming
    Replies: 2
    Last Post: 8th February 2011, 12:03
  5. Replies: 12
    Last Post: 28th May 2010, 00:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.