dbTalk Databases Forums  

Compile Error/Code Issue

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss Compile Error/Code Issue in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Freyja
 
Posts: n/a

Default Compile Error/Code Issue - 11-16-2006 , 01:51 AM






After about 2 years of sitting around dreading the idea of doing this on my
own, I finally decided to try to get my application functional again by
updating to the new code (with lack of C++ experience/much formal training
this is a daunting task). After 6.5 hours of trial and error, research
galore and pure luck, I've managed to get a 'nearly' functional compile
going. I seem to be stuck on a couple of errors which I'm sure are simple
and I'm just overlooking something. If someone could lend me a hand and
point me in the right direction, or even tell me what I'm doing wrong, I
would greatly appreciate it. I've included the errors and coresponding code
below. Thank you!


######## Error 1 ########

DatabaseControl.cpp: In constructor 'DatabaseControl:atabaseControl()':
DatabaseControl.cpp:45: error: returning a value from a constructor

####### Code 1 #########

DatabaseControl:atabaseControl()
{

// Connect to database server
mysqlpp::Connection con(mysqlpp::use_exceptions);
try {
cout << "Connecting to database server..." << endl;
con.connect(DBNAME, DBHOST, DBUSER, DBPASS);
}
catch (exception& er) {
cerr << "Connection failed: " << er.what() << endl;
return -1;
}

######### Error 2 ########

DatabaseControl.cpp: In member function 'std::map<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, Command*,
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std:air<const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
Command*> > > DatabaseControl::getCommands()':
DatabaseControl.cpp:69: error: request for member 'query' in
'((DatabaseControl*)this)->DatabaseControl::con', which is of non-class type
'Connection*'

######## Code 2 ########

map<string, Command*> DatabaseControl::getCommands()
{
map<string, Command*> theMap;

try
{
mysqlpp::Query query = con.query();
//Query query = con.query();
//Query query = m_conDB->query();
query << "SELECT * FROM tbl_commands";

mysqlpp::Result res = query.store();
//Result res = query.store();
mysqlpp::Row row;
//Row row;

mysqlpp::Result::iterator it;
//Result::iterator it;
for(it = res.begin(); it != res.end(); ++it)
{
row = *it;
Command *newCmd = new Command(
row["Name"].get_string(),
row["Code"].get_string(),
(int)row["MinPosition"],
(int)row["Level"],
(int)row["Logged"],
(int)row["Enabled"] );
theMap[newCmd->getName()] = newCmd;
}
}
catch(const mysqlpp::BadQuery& er)
{
cerr << "Error querying for Command:" << er.what() << endl;
}

return theMap;
}

########## Error 3 #########

DatabaseControl.cpp: In destructor 'virtual
DatabaseControl::~DatabaseControl()':
DatabaseControl.cpp:1270: warning: possible problem detected in invocation
of delete operator:
DatabaseControl.cpp:1270: warning: invalid use of undefined type 'struct
Connection'
DatabaseControl.h:18: warning: forward declaration of 'struct Connection'
DatabaseControl.cpp:1270: note: neither the destructor nor the
class-specific operator delete will be called, even if they are declared
when the class is defined.

######### Code 3 ##########

/**
* Destructor for the class. Disconnects from the database and
* deletes the pointer to the database connection.
*/
DatabaseControl::~DatabaseControl()
{
delete con;
//delete m_conDB;
con = NULL;
//m_conDB = NULL;
}


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
  #2  
Old   
Joel Fielder
 
Posts: n/a

Default RE: Compile Error/Code Issue - 11-16-2006 , 03:06 AM






I'm sure these are just c++ errors as opposed to anything mysql++
related - if so you should follow this up on a c++ list somewhere.

Error 1, exactly what it says on the tin, constructors can't return a
value of any type so just remove the "return -1;". Personally, I'd let
whatever instanciates DatabaseControl handle the exception (or at least
re-throw it), but otherwise making that change will correct the error.

I think error 2 is because "con" is a pointer, probably you need to use
con->query(). I think error 3 you're missing the header file for
mysqlpp::Connection.

Also, because you're shadowing the "con" member variable in the
constructor it is probably not allocated anywhere. Result: app will
probably crash when you call getCommands or DatabaseControl goes out of
scope Enjoy more, and turn on -Wshadow to have the compiler let you
know about it. Turn on -Wall for that matter

Joel


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
  #3  
Old   
Freyja
 
Posts: n/a

Default Re: Compile Error/Code Issue - 11-16-2006 , 07:36 PM



Thanks a million for the help, I was able to work through most of the issues
but I think my fundamental modifications of the code that I did last night
were what was giving me the most problems. So, in short, I backed up the
version that I finally got to compile (which still doesn't compile into the
program itself which is why I think it might be a bigger set of what I did)
and then pulled a fresh copy of the file and did some very basic
modifications and I think I'm quite a bit further along but I appear to be
running into a big of an issue with it at this point and was wondering if I
could get any pointers as to if I have something with mysqlpp set up wrong?
I think I'm really really close, but a touch stuck =\.

### The Errors ###

lyra:src/SQL# make
g++ -Wall -Wno-deprecated -O -g -I/usr/include/mysql++ -I/usr/include/my
sql -c DatabaseControl.cpp
DatabaseControl.h:29: error: ISO C++ forbids declaration of 'Connection'
with no type
DatabaseControl.h:29: error: expected ';' before '*' token
DatabaseControl.cpp: In constructor 'DatabaseControl:atabaseControl()':
DatabaseControl.cpp:39: error: 'm_conDB' was not declared in this scope
DatabaseControl.cpp:39: error: expected type-specifier before 'Connection'
DatabaseControl.cpp:39: error: expected `;' before 'Connection'
DatabaseControl.cpp: In member function 'std::map<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, Command*,
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std:air<const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
Command*> > > DatabaseControl::getCommands()':
DatabaseControl.cpp:54: error: 'm_conDB' was not declared in this scope

### The Snippets of Code ###

I have included mysql++.h and using namespace mysqlpp


DatabaseControl *DatabaseControl::s_DBCinstance((DatabaseControl*) 0);

/**
* Private contructor for the class. Creates a new Connection object
* and connects to the database.
*/
DatabaseControl:atabaseControl()
{
try
{
m_conDB = new Connection();
m_conDB->connect(DBNAME, DBHOST, DBUSER, DBPASS);
}
catch(const BadQuery& er)
{
cerr << "Error occured: " << er.what() << endl;
}
}

###### 2nd error related to below ######

map<string, Command*> DatabaseControl::getCommands()
{
map<string, Command*> theMap;

try
{
Query query = m_conDB->query();
query << "SELECT * FROM tbl_commands";

Result res = query.store();
Row row;

Result::iterator it;
for(it = res.begin(); it != res.end(); ++it)
{
row = *it;
Command *newCmd = new Command(
row["Name"].get_string(),
row["Code"].get_string(),
(int)row["MinPosition"],
(int)row["Level"],
(int)row["Logged"],
(int)row["Enabled"] );
theMap[newCmd->getName()] = newCmd;
}
}
catch(const BadQuery& er)
{
cerr << "Error querying for Command:" << er.what() << endl;
}

return theMap;
}


### The Header File Snippets ###

I have included mysql++.h and using namespace mysqlpp here as well.

class Connection;

class DatabaseControl : public IDataPersist
{
private:
Connection *m_conDB;
static DatabaseControl *s_DBCinstance;
DatabaseControl();

public:
virtual ~DatabaseControl();
static DatabaseControl* Instance();
map<string, Command* > getCommands();
**SNIPPED**

----- Original Message -----
From: "Joel Fielder" <joel.fielder (AT) switchplane (DOT) com>
To: <plusplus (AT) lists (DOT) mysql.com>
Sent: Thursday, November 16, 2006 2:46 AM
Subject: RE: Compile Error/Code Issue


Quote:
I'm sure these are just c++ errors as opposed to anything mysql++
related - if so you should follow this up on a c++ list somewhere.

Error 1, exactly what it says on the tin, constructors can't return a
value of any type so just remove the "return -1;". Personally, I'd let
whatever instanciates DatabaseControl handle the exception (or at least
re-throw it), but otherwise making that change will correct the error.

I think error 2 is because "con" is a pointer, probably you need to use
con->query(). I think error 3 you're missing the header file for
mysqlpp::Connection.

Also, because you're shadowing the "con" member variable in the
constructor it is probably not allocated anywhere. Result: app will
probably crash when you call getCommands or DatabaseControl goes out of
scope Enjoy more, and turn on -Wshadow to have the compiler let you
know about it. Turn on -Wall for that matter

Joel


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe:
http://lists.mysql.com/plusplus?unsu...adow (DOT) net



--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #4  
Old   
Joel Fielder
 
Posts: n/a

Default Re: Compile Error/Code Issue - 11-17-2006 , 02:40 AM



I'm 100% sure that mysql++ is fine here. Have you compiled and run the
example programs? If so then it's safe to say that mysql++ is all setup
fine and that the problem is with your code. I strongly suggest a c++
mailing list because you will get more people willing to help out and
this is becoming somewhat off-topic

More than likely this is caused by the fact that the compiler can't work
out which version of Connection you want.

By including mysql++.h you are getting mysqlpp::Connection, but you
bring it into global namespace with the using declaration, allowing you
to declare instances of mysqlpp::Connection without writing "mysqlpp".

Then, you forward declare another class called Connection.

As far as the compiler is concerned, you now have two classes, both
named Connection. It has no way of working out which version of
Connection you actually want to use, so it works on the principle of
"find the nearest thing called Connection", which is your forward
declared class. This has no definition so you can't instantiate or use
the object. And so it fails.

May I refer you to this document:
http://www.possibility.com/Cpp/CppCodingStandard.html - this is a pretty
comprehensive document on c++ coding style but also has a lot of best
practice tips, including, "Don't place using namespace directive at
global scope in a header file. This can cause lots of magic invisible
conflicts that are hard to track."

Personally, I never use "using" because I like to be able to look at the
code and know exactly what's going on without having to guess, and
importantly I am guaranteeing never to run into these problems. Look at
it from this point of view: have you spent less time working out what
the problem is than you saved by not having to type "mysqlpp::"?

Joel.


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.