Accessing a static Db object

Dear All,

I've created a database class in my project and I'm opening a db connection with that class whenever I need to query Db. However, it's very inefficient creata new database object in every function call and open a connection to database. For that reason I'm trying to change my implementation and create a static protected db object and open a connection once and close it when I finish all my queries.

So far, it sounds reasonable and easy :)

However, when I call "open" method of my database class from another class, I'm getting this error:
error C2662: 'Database::Open' : cannot convert 'this' pointer from 'const Database' to 'Database &'


How can I solve this problem?

My db header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#define CATCHERROR(ptr,a)	catch(_com_error &e)\
							{\
								ErrorHandler(e,m_ErrStr);\
								ptr=NULL;\
								return a;\
							}

#define CATCHERRGET			catch(_com_error &e)\
							{\
								ErrorHandler(e,m_ErrStr);\
								sprintf(m_ErrStr,"%s\n**For Field Name:%s",m_ErrStr,FieldName);\
								return 0;\
							}

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
              rename("EOF", "EndOfFile")

typedef ADODB::_RecordsetPtr	RecPtr;
typedef ADODB::_ConnectionPtr	CnnPtr; 

class Database;
class Table;


class Database
{
public:
	CnnPtr m_Cnn;
	char m_ErrStr[500];
	Database();
	bool Open(char* UserName, char* Pwd,char* CnnStr);
	bool Close();
	bool OpenTbl(int Mode, char* CmdStr, Table& Tbl);
	bool Execute(const char* CmdStr);
	bool Execute(const char* CmdStr, Table& Tbl);
	void GetErrorErrStr(char* ErrStr);
};

class Table{
public:
	RecPtr m_Rec;
	char m_ErrStr[500];
	Table();
	void GetErrorErrStr(char* ErrStr);
	int ISEOF();
	HRESULT MoveNext();
	HRESULT MovePrevious();
	HRESULT MoveFirst();
	HRESULT MoveLast();
	int AddNew();
	int Update();
	int Add(char* FieldName, char* FieldValue);
	int Add(char* FieldName,int FieldValue);
	int Add(char* FieldName,float FieldValue);
	int Add(char* FieldName,double FieldValue);
	int Add(char* FieldName,long FieldValue);
	bool Get(char* FieldName, char* FieldValue);
	bool Get(char* FieldName, std::string& FieldValue);	
	int GetInt(char* FieldName);
	bool Get(char* FieldName,float& FieldValue);
	bool Get(char* FieldName,double& FieldValue);
	bool Get(char* FieldName,double& FieldValue,int Scale);
	bool Get(char* FieldName,long& FieldValue);
};


Trying to call it from this class:
1
2
3
4
5
6
7
8
9
#include "Database.h"
namespace Myclass
{
    class Myclass
    {	
	protected:
		static const Database* dbObj;
}
}


And implementation:
 
if(!dbObj->Open(userIdValue, passwordValue, CnnStr)){}


Thanks in advance
Anyone has an idea?
The 'Database' is const but the 'Open()' function requires a non const object.

Remove that const on line 7 of 'Myclass'. Likely that you don't even need static
Thanks.

I tried that before.

When I make it like:
1
2
3
..
	protected: Database* dbObj;
..


I'm getting this error:
left of '->Open' must point to class/struct/union/generic type
By the way: You know that on line 8 of 'Myclass' a semicolon is missing?

That error means probably that you're trying to use 'dbObj' outside the scope of 'Myclass'.

If you want to use it outside you need to ask 'Myclass' for the pointer.
I have that semicolon, I missed that while writing simplified version.

It is inside the scope btw, let me write it more detailly:

1
2
3
4
5
6
7
8
9
10
11
#include "Database.h"
namespace MyClass
{
    class MyClass
    {
    public:        
...	
    protected:
	Database* dbObj;
    };
}


1
2
3
4
5
6
7
8
9
10
11
12

using namespace std;

namespace MyClass
{
int MyClass::RunAQuery(...){
     if(!dbObj->Open(userIdValue, passwordValue, CnnStr))
     {...}
     
     return 0;
}
}
Topic archived. No new replies allowed.