Variable scope in C++

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Variable scope in C++

Post by nime »

Hello,
Not strictly wxwidgets questions, but I need help if allowed...
I made my first simple class in C++ for "reusable" handling of common situations with sqlite.

Code: Select all

#ifndef SQLITEPP_H
#define SQLITEPP_H

#include <stdio.h>
#include "sqlite3.h"

class clssqlite
{
public:
    int set_collation(sqlite3 *handle, char* setcollation);
    int table_present(sqlite3 *handle, char* tablename);
    int table_drop(sqlite3 *handle, char* tablename);

    struct mystruct
    {
        char one[10];
        int two;
        float three;
    } __attribute__((__packed__));
};
#endif // SQLITEPP_H
This works OK (I think) because is possible to use class functions from different files just with including header:

Code: Select all

        clssqlite c_sql;
        char* database = (char*)"m_test.db";
        char* src_table = (char*)"mytable";

        if (dropdb) c_sql.table_drop(db, src_table);
        if (!c_sql.table_present(db, src_table))
        {
            // make table...
        ... etc...
But I cannot use mystruct as public struct:

Code: Select all

        c_sql.mystruct ms;
        ms.three = 3.14;
        strcpy(ms.one, "my try");
So I need help on this one.

And this one:

Code: Select all

#if defined(linux)
    wxCSConv cconv852(wxT("ibm852"));
#else
    wxCSConv cconv852(wxFONTENCODING_CP852);
#endif
Where to put this code so cconv852 could be reachable through whole multifile app.
I try to put it in above header but I get message "cconv852 declared more than once".
I also didn't forget to include <wx/strconv.h> for that.
amk_tt
Earned a small fee
Earned a small fee
Posts: 19
Joined: Sat Nov 28, 2009 4:45 pm
Location: Russia

Re: Variable scope in C++

Post by amk_tt »

mystruct declared in scope of c_sql. Not member. You should write
c_sql::mystruct ms;
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Re: Variable scope in C++

Post by nime »

Dont want to work again! :(

Code: Select all

c_sql::mystruct ms;
ms.three = 3.14;
ms.one = "my try";
error: ‘c_sql’ is not a class or namespace
error: expected ‘;’ before ‘ms’
error: ‘ms’ was not declared in this scope
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Variable scope in C++

Post by PB »

I know you do not want to hear it but I will give that invaluable (and free) advice again: Go read a C++ primer! ;) As I wrote before, it WILL save you tremendous amount of time, effort and frustration. No offense, but if you say "I don't have a nerve to sit through a book", maybe you should find a different hobby than C++ programming?
nime wrote:Dont want to work again! :(

Code: Select all

c_sql::mystruct ms;
ms.three = 3.14;
ms.one = "my try";
Obviously, you have to use class name (clssqlite) here, not the name of a variable.
Also, this is C++, get rid of character arrays (we use std:: string or wxString). When we are at it, the usage of raw pointers is also generally frown upon and for good reasons - use smart pointers instead, you did hear about RAII, right?
And this one:

Code: Select all

    #if defined(linux)
        wxCSConv cconv852(wxT("ibm852"));
    #else
        wxCSConv cconv852(wxFONTENCODING_CP852);
    #endif
Using global variables of classes is generally advised against, because you have no control regarding their creation, which can lead to hard to find bugs. That's why you should use functions returning const reference to the local static instance of the class (the instance is created upon the first call of the function), exactly the way I showed you before when I introduced that piece of code.

</preaching>
Post Reply