0

I'm trying to provide custom exception handling mechanism to a class inherited from mysqlpp::StoreQueryResult in mysql++ library. But what i'm finding really hard to do is to find a way to refer to the actual object within the class, so i'm unable to use the vector operation at() properly in order to retrieve a value at an index.

Here is the header

/* MySQLQueryResult.h */


#ifndef MYSQLQUERYRESULT_H
#define MYSQLQUERYRESULT_H

#include <mysql++.h>
#include <result.h>

namespace  MinesScanner {

    namespace MoonStone {

        class MySQLQueryResult : public mysqlpp::StoreQueryResult  {
        public:

            MySQLQueryResult();


            MySQLQueryResult(const MySQLQueryResult &other);

            MySQLQueryResult& operator=(const MySQLQueryResult &other);

            mysqlpp::Row& operator[](int index);

        private:

            int _dat;



        };

    }

}

#endif  /* MYSQLQUERYRESULT_H */

Here is the source file

/* MySQLQueryResult.cpp */

#include "MySQLQueryResult.h"

namespace MinesScanner {

    namespace MoonStone {

        MySQLQueryResult::MySQLQueryResult( )
    : StoreQueryResult( )
    {
    }

    MySQLQueryResult::MySQLQueryResult( const StoreQueryResult &ob )
    : StoreQueryResult( ob )
    {
    }

    MySQLQueryResult& MySQLQueryResult::operator=( const StoreQueryResult &ob )
    {

        StoreQueryResult::operator=( ob ) ;
        return *this ;

    }

    mysqlpp::Row& MySQLQueryResult::operator[]( int index )
    {


        try {

            std::cout << " Called " << this->at( index ) << std::endl ;
            return this->at( index ) ;
        } catch ( std::exception& excpn_ob ) {
            std::cerr << " Exception caught : " << excpn_ob.what( ) << std::endl ;
        }


    }


    }

}

A simple usage example will show more clearly what i want to achieve.

#include "MySQLQueryResult.h"

int main() {
StoreQueryResult lisres = getMinesData( ( string ) row.at( 0 ) ) ; // returns StoreQueryResult object storing the result

cout << lisres[0][7] << endl; // outputs "Falkreath Stormcloak Mines"

MySQLQueryResult my_lisres = getMinesData( ( string ) row.at( 0 ) ) ; // returns StoreQueryResult object storing the result

cout << my_lisres[0][7] << endl; // ERROR!

}

So i basically want to add more boundary checking, check for null values , and handle out_of_range exception using the operator[] in class MySQLQueryResult but its not working.

I want to be able to access MySQLQueryResult object using array subscripts . Either i'm getting a garbage value or a Seg fault. Please let me know how to get this right

2
  • Has your program written output message which you define inside subscript operator? Commented Jun 28, 2014 at 20:18
  • Yes its printing "Called" for every usage of the array subscript operator Commented Jun 29, 2014 at 6:24

1 Answer 1

3

but its not working

It looks like you also need to overload const version of subscript operator:

const mysqlpp::Row& operator[](int index) const;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.