Help? Hopefully not a silly question. Regarding global declarations

This is a discussion on Help? Hopefully not a silly question. Regarding global declarations within the C++ Programming forums, part of the General Programming Boards category; Perhaps this just isn't possible but I was making a program which needs a global access to a database resource. ...

  1. #1
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Help? Hopefully not a silly question. Regarding global declarations

    Perhaps this just isn't possible but I was making a program which needs a global access to a database resource. Every file includes my "global.h" file, however when I try to declare it as an extern it fails.

    Note: dbaccess is the name of the class as well.

    snippet of globals.h


    #include "dbaccess.h"
    ...

    extern dbaccess theDB;

    then it gives me 2 errors:

    error C2146: syntax error : missing ';' before identifier 'theDB'
    error C2501: 'theDB' : missing storage-class or type specifiers

    the second one should follow the first since it has no idea what theDB is apparently.

    globals.h is included by dbaccess.h, and I have guards against circular inclusion.

    I could do this in other ways I know, but it would be easier to have it global for my purposes, perhaps a bit faster as well. Any ideas / help?
    Last edited by dpro; 07-15-2005 at 05:29 PM. Reason: Changing title to be more helpful

  2. #2
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Is dbaccess theDB defined in dbaccess.h? Or in another header? maybe you just need extern theDB;
    But if you just declaring a dbaccess theDB in globals.h you shouldn't need extern. But if you're trying to have dbaccess theDB in dbaccess.h as it looks like you are trying to do it should be defined in dbaccess.h. If it's defined I'm out of ideas.
    Unless theDB is a member of dbaccess?
    Using Dev-C++ on Windows

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    You haven't really given enough information. A few possibilities I can think of to check, offhand are.

    1) dbaccess in dbaccess.h is a macro of some sort, not a type declation.

    2) dbaccess is not declared in dbaccess.h (eg is the name you want DbAccess ?)

    3) dbaccess is in dbaccess.h is only a forward declaration. This means you can only declare pointers to dbaccess, not actual objects of the type. If you want to declare an object, you need more than a forwarded declaration.

    Otherwise, I suggest trying to produce a small but complete example that illustrates your problem rather than paraphrasing your description. The process of producing such an example may help you find the problems, without help. If you still can't work it out, your question will be a bit clearer.....

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    dbaccess.h

    Code:
    #pragma once
    #include "globals.h"
    
    class dbaccess
    {
    public:
    	dbaccess();
    	~dbaccess();
    	QueryStruct * queryDB( string query, int * rowNums );
    	
    
    private:
    	MYSQL *conn;
    	MYSQL_ROW sqlRow;
    	QueryStruct queries;
    };
    Code:
    #include "dbaccess.h"
    
    dbaccess::dbaccess()
    {
    	conn = NULL;
    	MYSQL_RES *r = NULL;
    	
    	conn = mysql_init(NULL);
    	mysql_real_connect(conn, "localhost", "user", "pass", "mytest", 0, NULL, 0);
    }
    
    dbaccess::~dbaccess()
    {
    	mysql_close( conn );
    }
    
    QueryStruct * dbaccess::queryDB( string query, int * numRows )
    {
    	//connect to the db and return the query to the caller
    	int testval = 0, tempCounter = 0;
    	ULLONG  numResults = 0;
    	
    	QueryStruct * tempData = NULL;
    	MYSQL_RES * result = NULL;
    	
    	testval = mysql_select_db( conn, DBNAME );
    
    	do
    	{
    		testval = mysql_query( conn, query.c_str() );
    		result = mysql_store_result( conn );
    		++tempCounter;
    	}while( result == NULL && tempCounter < 5 );
    
    	if( result == NULL )
    	{
    		//tempData is null, return nothing
    		return tempData;
    	}
    	else
    	{
    		numResults = mysql_num_rows( result );
    		for( ULLONG i = numResults; i >= 0; i-- )
    		{
    			ULLONG numFields = mysql_num_fields( result );
    			tempData->rowsPerQuery.push_back( numFields );
    			MYSQL_ROW row = mysql_fetch_row( result );
    			tempData->queryRows.push_back( row );
    		}
    	}
    
    	return tempData;
    }
    globals.h

    Code:
    #include <mysql.h>
    #include "dbaccess.h"
    
    struct QueryStruct
    {
    std::vector<int> rowLen;
    std::vector<MYSQL_ROW> rowdata;
    };
    extern dbaccess theDB;

    main.cpp

    Code:
    #include "globals.h"
    #include "connection.h"
    
    int main(void)
    {
    int numRows = 0;
    connection * con = new connection();
    return 0;
    }
    connection.h

    Code:
    #pragma once
    #include "globals.h"
    
    class connection
    {
    public:
     connection();
     ~connection();
    };
    connection.cpp

    Code:
    connection::connection()
    {
       
         char query[] = "Select * from table";
         QueryStruct * x =  theDB.queryDB( query, &numRows );
    
    }
    
    etc....

    This is a rough example, if there are syntactic errors or slight semantic errors its because it is late. Thanks.
    Last edited by dpro; 07-16-2005 at 04:49 AM. Reason: Added dbaccess.cpp

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    You have dbaccess.h including globals.h which includes dbaccess.h .... Try removing that recursive relationship.

    Include guards will also be useful: they are both more portable between compilers and more likely to work than the #pragma once. i.e. in each header file have

    Code:
    #ifndef SOME_MACRO_PICKED_TO_BE_UNIQUE_TO_THIS_HEADER
    #define SOME_MACRO_PICKED_TO_BE_UNIQUE_TO_THIS_HEADER
    
    // contents of the header
    
    #endif

  6. #6
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    True enough, however I don't know if that really will help things. Would that truely affect the fact that it cannot find the definition?

    I was under the impression that "pragma once" although not perfect, works quite well for this, and I have used this many times to remove circular dependencies.

    I will try this, but seems a bit weird to have that be the problem...

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Circular references in header files can result in several symptoms. The symptom you describe is one of the possibilities.

    Pragma once is something I wouldn't go near. It's behaviour is highly compiler dependent; if you use an other compiler, the same results aren't guaranteed. Using include guards gives specific effects, portably.

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Hmmm interesting, I suppose it really does depend on how you feel about code.

    Sometimes portability is not being aimed for, in this case, its not completely needed.

    However I will give that a try, I think more likely is that I will have to move some structures to a different global file and reorganize a bit. This program is getting large from an organizational standpoint, so its probably wise for me to reword the structure and less of the code. Thanks for the replies, I am sure they will help me quite a bit, moreover they did help me realize that this particular path (the circular part) needs to be changed to effectively move on. Thanks!

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Quote Originally Posted by dpro
    Hmmm interesting, I suppose it really does depend on how you feel about code.
    In my case, I've seen too many instances where a customer sees a bit of software, says "I'd like that to run on XXX", and a management or marketing type gives a glib assurance that it is easily done.

    Quote Originally Posted by dpro
    Sometimes portability is not being aimed for, in this case, its not completely needed.
    Sure. But I'm not a fan of using a non-portable approach when there is a valid portable approach, unless there are significant advantages in the non-portable approach. I've yet to see cases where #pragma once gives significant advantages over using simple #include guards.

    Quote Originally Posted by dpro
    However I will give that a try, I think more likely is that I will have to move some structures to a different global file and reorganize a bit. This program is getting large from an organizational standpoint, so its probably wise for me to reword the structure and less of the code. Thanks for the replies, I am sure they will help me quite a bit, moreover they did help me realize that this particular path (the circular part) needs to be changed to effectively move on.
    Good. I would have said something similar in an earlier post, but I figured it's better to point out the concerns an allow you to reason out the punch line. Having worked it out for yourself, you're both less likely to be offended at the notion of needing to do it, and more likely to do a better job restructuring your code.

  10. #10
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Hehe never really offended at code restructuring just checking my alternatives. It can be annoying but if it helps its always a good thing

    True enough, I've never been a fan of include guards, but thats a personal preference. I've more been a fan of things like "require_once" from php. Hey I am a bit lazy

    Anyway, thanks for the help, I am certainly not new to these sorts of things, but I wanted to be stubborn for a moment to see if I could get my way with the code, but looks like reorganization will provide more than butting my head against the wall, thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob silly pointer? question
    By darin722 in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2009, 06:58 AM
  2. Global classes and a typedef question!
    By Akkernight in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2009, 01:57 PM
  3. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  5. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21