Results 1 to 11 of 11

Thread: Trying to learn. Not declared in this scope error

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Angry Trying to learn. Not declared in this scope error

    I am trying to create a main window and if a SQLite file doesn't exist, then create one. Then populate a combobox with data from a query. I am using QT Creator. I am learning and would like to know why I get the error and not just the correct code if that isn't too much to ask.
    Qt Code:
    1. //database.h
    2. //Database functions will go here
    3. #ifndef DATABASE_H
    4. #define DATABASE_H
    5.  
    6. class database
    7. {
    8. public:
    9. database();
    10. };
    11.  
    12. #endif // DATABASE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //MainWindow.h
    2. #ifndef MXMAINWINDOW_H
    3. #define MXMAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6.  
    7. namespace Ui {
    8. class MXMainWindow;
    9. }
    10.  
    11. class MXMainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MXMainWindow(QWidget *parent = 0);
    17. ~MXMainWindow();
    18.  
    19. private:
    20. Ui::MXMainWindow *ui;
    21. };
    22.  
    23. #endif // MXMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //database.cpp
    2. #include "database.h"
    3. #include <QtSql>
    4. #include <QApplication>
    5. #include <QtGui>
    6.  
    7. bool createConnection()
    8. {
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName("rider.mt");
    11. if (!db.open()) {
    12. QMessageBox::warning(0, QObject::tr("Database Error"),
    13. db.lastError().text());
    14. return false;
    15. }
    16.  
    17.  
    18. QSqlQuery dbquery;
    19. dbquery.prepare ("CREATE TABLE Rider (firstname varchar(20))");
    20. dbquery.exec();
    21. dbquery.prepare("insert into Rider (firstname)Jeremy')");
    22. dbquery.exec("insert into Rider values('Ricky', 'Charmichael')");
    23. dbquery.exec("insert into Rider values('Chad', 'Reed')");
    24. dbquery.next();
    25. QString strTest=dbquery.value(0).toString();
    26. QString strTest2= dbquery.value(1).toString();
    27. return true;
    28. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //mainwindow.cpp
    2. #include "mxmainwindow.h"
    3. #include "ui_mxmainwindow.h"
    4. #include <QtSql>
    5. #include "database.h"
    6.  
    7. MXMainWindow::MXMainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MXMainWindow)
    10. {
    11. ui->setupUi(this);
    12. while (dbquery.next()) //Error here.. dbquery not declared in this scope
    13. {
    14. ui->cmbName->addItem(dbquery.value(0).toString());
    15. }
    16. }
    17.  
    18. MXMainWindow::~MXMainWindow()
    19. {
    20. delete ui;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //main.cpp
    2. #include <QtGui/QApplication>
    3. #include "mxmainwindow.h"
    4. #include <QtSql>
    5. #include <QtGui>
    6. bool createConnection()
    7. {
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName("mxtrainer.dat");
    10. if (!db.open()) {
    11. QMessageBox::warning(0, QObject::tr("Database Error"),
    12. db.lastError().text());
    13. return false;
    14. }
    15. return true;
    16. }
    17.  
    18. void createDb()
    19. {
    20. QSqlQuery query;
    21. //query.exec("DROP TABLE scooter");
    22.  
    23. query.exec("CREATE TABLE rider ("
    24. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    25. "name VARCHAR(20) NOT NULL, "
    26. "weight INTEGER NOT NULL, ");
    27. query.exec("INSERT INTO rider (name, weight) "
    28. "VALUES ('Villapoto', 155");
    29. query.exec("INSERT INTO rider (name, weight) "
    30. "VALUES ('Carmichael', 165");
    31. query.exec("INSERT INTO rider (name, weight) "
    32. "VALUES ('McGrath', 175");
    33.  
    34. }
    35. int main(int argc, char *argv[])
    36. {
    37. QApplication a(argc, argv);
    38. bool create = !QFile::exists("mxtrainer.dat");
    39. if (!createConnection())
    40. return 1;
    41. if (create)
    42. createDb();
    43. MXMainWindow w;
    44. w.show();
    45.  
    46. return a.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 
    I thought by including the header file of where the declaration is made it would provide the scope? What is the problem with this?
    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to learn. Not declared in this scope error

    Variables local to a function or method are not visible outside that function. dbQuery is a local variable created on the stack inside your createConnection() function and cannot be seen inside the MXMainWindow constructor where you try to use it. Apart from not being visible outside createConnection(), dbQuery ceases to exist at the end of the function.

    In any case, I cannot see a query that would return the rows you are expecting to populate the combo box with e.g. "SELECT name FROM riders".
    Last edited by ChrisW67; 21st September 2010 at 03:41. Reason: updated contents

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to learn. Not declared in this scope error

    I was basing my code on http://doc.qt.nokia.com/4.6/sql-cachedtable.html and they use the connection and query in the connection.h file and it seems to work. What am I doing wrong? I did forget the query to populate the rows. In trying to make things work I duplicated making the table. I would like to have the connection and queries in a header file but I don't know why it doesn't work like the example. I moved the query to the mxmainwindow the program compiles but there is no data in the combobox.
    I see that the mxtrainer.dat file is created but there is no data in it.
    So my questions are:
    1. Why didn't my program work like the example where the connection and query were in a seperate header file
    2. Why isn't the mxtrainer.dat file being populated?
    3. Why isn't the combobox getting populated? (Maybe because of #2!!)
    Suggested code corrections?

    Here is the new code:
    Qt Code:
    1. //mxmainwindow.h
    2. #ifndef MXMAINWINDOW_H
    3. #define MXMAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6.  
    7. namespace Ui {
    8. class MXMainWindow;
    9. }
    10.  
    11. class MXMainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MXMainWindow(QWidget *parent = 0);
    17. ~MXMainWindow();
    18.  
    19. private:
    20. Ui::MXMainWindow *ui;
    21. };
    22.  
    23. #endif // MXMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //mxmainwindow.cpp
    2. #include "mxmainwindow.h"
    3. #include "ui_mxmainwindow.h"
    4. #include <QtSql>
    5. //#include "database.h"
    6.  
    7. MXMainWindow::MXMainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MXMainWindow)
    10. {
    11. ui->setupUi(this);
    12.  
    13. QSqlQuery dbquery;
    14. dbquery.prepare ("SELECT name from rider");
    15. dbquery.exec();
    16.  
    17. while (dbquery.next())
    18. {
    19. ui->cmbName->addItem(dbquery.value(0).toString());
    20. }
    21. }
    22.  
    23. MXMainWindow::~MXMainWindow()
    24. {
    25. delete ui;
    26. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //main.cpp
    2. #include <QtSql>
    3. #include <QtGui>
    4. bool createConnection()
    5. {
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    7. db.setDatabaseName("mxtrainer.dat");
    8. if (!db.open()) {
    9. QMessageBox::warning(0, QObject::tr("Database Error"),
    10. db.lastError().text());
    11. return false;
    12. }
    13. return true;
    14. }
    15.  
    16. void createDb()
    17. {
    18. QSqlQuery query;
    19. //query.exec("DROP TABLE scooter");
    20.  
    21. query.exec("CREATE TABLE rider ("
    22. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    23. "name VARCHAR(20) NOT NULL, "
    24. "weight INTEGER NOT NULL, ");
    25. query.exec("INSERT INTO rider (name, weight) "
    26. "VALUES ('Villapoto', 155");
    27. query.exec("INSERT INTO rider (name, weight) "
    28. "VALUES ('Carmichael', 165");
    29. query.exec("INSERT INTO rider (name, weight) "
    30. "VALUES ('McGrath', 175");
    31.  
    32. }
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication a(argc, argv);
    36. bool create = !QFile::exists("mxtrainer.dat");
    37. if (!createConnection())
    38. return 1;
    39. if (create)
    40. createDb();
    41. MXMainWindow w;
    42. w.show();
    43.  
    44. return a.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by poporacer; 21st September 2010 at 04:37. Reason: more info

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to learn. Not declared in this scope error

    1. Your original program didn't compile because it was not valid C++. This is not about what is in which file: it would have failed even if you put all the code in the same file.
    2. You only call createDb() if the "mxtrainer.dat" file does not exist already. What happens if it exists but is empty?
    3. Quite possibly because of 2. If you look at QSqlQuery::lastError() after your call to exec() you might see a clue.

    You have a tool for single stepping through your program can be very useful here.

  5. #5
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to learn. Not declared in this scope error

    What do I need to do to have the connection and queries in the header file? If put the function in to test if the mxtrainer is there to see if it is the first time the program has run. If it is the first time, create the table. If not use the existing data. Where is the tool for single stepping through the code...I have been using breakpoints to try and find the problems. I got rid of the test and put in QSqlQuery::Last error like this:
    Qt Code:
    1. void createDb()
    2. {
    3. QSqlQuery query;
    4. //query.exec("DROP TABLE scooter");
    5.  
    6. query.exec("CREATE TABLE rider ("
    7. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    8. "name VARCHAR(20) NOT NULL, "
    9. "weight INTEGER NOT NULL, ");
    10. query.exec("INSERT INTO rider (name, weight) "
    11. "VALUES ('Villapoto', 155");
    12. query.exec("INSERT INTO rider (name, weight) "
    13. "VALUES ('Carmichael', 165");
    14. query.exec("INSERT INTO rider (name, weight) "
    15. "VALUES ('McGrath', 175");
    16. qDebug() << query.lastError();
    17. }
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication a(argc, argv);
    21. //bool create = !QFile::exists("mxtrainer.dat");
    22. //if (!createConnection())
    23. //return 1;
    24. //if (create)
    25. createDb();
    26. MXMainWindow w;
    27. w.show();
    28.  
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 
    I got an error database not open.
    Where do I go from here?
    PS. I really appreciate all your help!!

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to learn. Not declared in this scope error

    The "Database not open error" is the result of commenting out the createConnection() call, which is opening the database! In your previous attempt the error you would be really interested in was the exec() call after trying to select the rider's names.

    The debugging tool you are setting breakpoints in can single-step your program. Look on the Debug menu in Qt Creator when you are stopped at a break point.

    I don't know exactly what you mean by "What do I need to do to have the connection and queries in the header file?". You just put the code there and make sure it is included where required. However, it is usually not a good idea to put real implementation code into header files (with the exception of trivial inline methods). The Qt examples do this to simplify the construction of many independent programs that use that example database. Headers are typically expected to contain declarations only, with the implementation (if any) in a matching cpp file.

  7. #7
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to learn. Not declared in this scope error

    I uncommented out that section and got the error QSqlError(1, "Unable to execute statement", "near "175": syntax error") And stepping in goes through the QT code. Am I missing something?
    And when I put in after the Select name query I got: QSqlError(-1, "Unable to fetch row", "No query")
    Last edited by poporacer; 21st September 2010 at 06:05. Reason: more info

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to learn. Not declared in this scope error

    Your SQL INSERT statements are malformed: missing their closing )

    Edit: So is your create table.

  9. #9
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to learn. Not declared in this scope error

    Thank you so much...I am starting to figure this out a little bit at a time. I really appreciate your help!
    The program works and the combo box gets populated, but in the QSqlError message I get
    QSqlError(-1, "", "")
    QSqlError(-1, "", "")
    I couldn't find any reference to what the -1 or the "","" errors are. Any Ideas?
    Last edited by poporacer; 22nd September 2010 at 01:15. Reason: updated contents

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to learn. Not declared in this scope error

    I think (not near a machine to test) that QSqlError::isValid() would return false... no error.

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Trying to learn. Not declared in this scope error

    Quote Originally Posted by ChrisW67 View Post
    I think (not near a machine to test) that QSqlError::isValid() would return false... no error.
    You are right, -1 indicates that there is no error. To see if a statement was successful fetch the result of QSqlQuery::exec()

Similar Threads

  1. Replies: 2
    Last Post: 16th July 2010, 07:17
  2. QDomDocument was not declared in this scope
    By grantbj74 in forum Newbie
    Replies: 5
    Last Post: 25th August 2009, 09:43
  3. glutSolidSphere was not declared in this scope error
    By nuts_fever_007 in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 04:56
  4. Replies: 2
    Last Post: 28th December 2007, 18:30
  5. error: 'connect' was not declared in this scope ??
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2007, 15:27

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.