Thread: Cxx Access issues

  1. #1
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88

    Cxx Access issues

    I can't seem to get access to private variables I declared in my class. i get the following errors
    Code:
    sqlModel.cxx: In function ‘bool createDummyDatabase()’:
    sqlModel.cxx:12:4: error: ‘db’ was not declared in this scope
    sqlModel.cxx:13:4: error: ‘dbFilename’ was not declared in this scope
    sqlModel.cxx: In function ‘bool createDummyTables()’:
    sqlModel.cxx:22:10: error: ‘query’ was not declared in this scope
    sqlModel.cxx: In function ‘void populateDummyTables()’:
    sqlModel.cxx:33:7: error: ‘query’ was not declared in this scope
    I'm pretty sure it's not Qt specific but my forwary into objects. Any help would be appreciated.
    Code:
    //sqlModel.h
    #ifndef sqlmodel_h
    #define sqlmodel_h
    
    #include <QObject>
    #include <QtSql>
    #include <QList>
    #include <QMap>
    #include <QDebug>
    #include <QDir>
    
    class sqlModel : public QObject {
       
       Q_OBJECT
    
       public:
       sqlModel(QObject* = 0);
       virtual ~sqlModel();
       QList<QMap<QString, QString> > getDummyData();
      
       private:
       QSqlDatabase db;
       QString dbFilename;
       QSqlQuery query;
       QSqlRecord record;
    
       //our methods
       bool createDummyDatabase();
       bool createDummyTables();
       void populateDummyTables();
    };
    
    #endif
    and the definition file
    Code:
    //sqlmodel.cxx
    #include "sqlModel.h"
    
    sqlModel::sqlModel(QObject* parent) : QObject(parent){
       createDummyDatabase();
       createDummyTables();
       populateDummyTables();
    }
    
    bool createDummyDatabase(){
       bool rtn = false;
       db = QSqlDatabase::addDatabase("QSQLITE");
       dbFilename = QDir::homePath() + "./dummy.music.db";
       db.setDatabaseName(dbFilename);
       if(db.open()) return true;
       //else 
       return rtn;
    }
       
    bool createDummyTables(){
       bool rtn = false;
       rtn = query.exec("CREATE TABLE IF NOT EXISTS dummy_music(id INTEGER PRIMARY KEY, year INTEGER, artist TEXT, albulm text, title TEXT");
       if(!rtn){
          qDebug() << "Error creating dummy table...";
          return false;
       }
    
       return true;
    }
    
    void populateDummyTables(){
       for(int x = 0; x < 20; x++){
          query.exec("INSERT OR IGNORE INTO dummy_music(id, year, artist, albulm, title) VALUES(1001, 1999, 'Matthew', 'apparations', 'his album')");
          query.exec("INSERT OR IGNORE INTO dummy_music(id, year, artist, albulm, title) VALUES(1001, 1999, 'Matthew', 'apparations', 'his album')");
          query.exec("INSERT OR IGNORE INTO dummy_music(id, year, artist, albulm, title) VALUES(1001, 1999, 'Matthew', 'apparations', 'his album')");
          query.exec("INSERT OR IGNORE INTO dummy_music(id, year, artist, albulm, title) VALUES(1001, 1999, 'Matthew', 'apparations', 'his album')");
          query.exec("INSERT OR IGNORE INTO dummy_music(id, year, artist, albulm, title) VALUES(1001, 1999, 'Matthew', 'apparations', 'his album')");
       }
    }
    
    /*  not finished yet
    QList<QMap<QString, QString> > getDummyData(){
       //temp containers for our artist info
       QList<QMap<QString, QString> > tableData;
       QMap<QString, QString> artist;
       //make our query and package it
       query.exec("SELECT * FROM dummy_music");
       record = query.record(); //get the query fields
       //itereate over each record
       while(query.next()){
          QString id = query.value(record.indexOf("id").toString;
          QString year = query.value(record.indexOf("year").toString;
          QString artist = query.value(record.indexOf("artist").toString;
          QString albulm = query.value(record.indexOf("albulm").toString;
          QString title = query.value(record.indexOf("title").toString;
          //insert each field into a map
          artist["id"] = id; artist["year"] = year; artist["artistName"] = artistName; artist["albulm"] = albulm; artist["title"] = title;
          tableData.append(artist);
       }
       //return the list
       return tableData;
    }
    */
    sqlModel::~sqlModel(){}

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You must prefix the function with "sqlModel::" to make them able to see the private vars.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Code:
    bool createDummyDatabase()
    ...should be:

    Code:
    bool sqlModel::createDummyDatabase()
    As they are now, you are declaring them as normal non-member functions, so they cannot access the member variables in the class.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    I feel like a moron thatz what happens when ur coding 12 hours straight. Fml. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation (segmentation fault) issues with an array ?
    By Leavenotrace in forum C Programming
    Replies: 1
    Last Post: 12-21-2011, 07:29 AM
  2. Replies: 15
    Last Post: 04-19-2011, 07:29 AM
  3. Replies: 3
    Last Post: 09-30-2008, 12:10 AM
  4. .EXE Issues
    By SpankyTheWalrus in forum Windows Programming
    Replies: 4
    Last Post: 03-07-2004, 12:48 PM
  5. IP access and DNS access to Cprog.com
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-31-2002, 07:22 PM