about static vector (of pointers) member function

Discussion in 'C++' started by Josefo, May 19, 2007.

  1. Josefo

    Josefo Guest

    Hello, is someone so kind to tell me why I am getting the following
    errors ?


    vector_static_function.c:20: error: expected constructor, destructor,
    or type conversion before '.' token
    vector_static_function.c:21: error: expected constructor, destructor,
    or type conversion before '.' token


    when compiling this:


    // about static vector (of pointers) member function

    #include <iostream>
    #include <vector>
    using namespace std;

    class G4PreCompoundModel
    {
    public:
    static vector<int*> * GetCreatorModels()
    { return &G4PreCompoundModel::theCreatorModels; }

    // private:

    static vector<int*> theCreatorModels;

    };

    int uno=1, dos=2;
    G4PreCompoundModel::theCreatorModels.push_back(&uno);
    G4PreCompoundModel::theCreatorModels.push_back(&dos);

    int main ()
    {
    G4PreCompoundModel::GetCreatorModels();
    return 0;
    }

    Thanks in advance amd best regards

    Jose Manuel
    Josefo, May 19, 2007
    #1
    1. Advertising

  2. Josefo wrote:
    > Hello, is someone so kind to tell me why I am getting the following
    > errors ?
    >
    >
    > vector_static_function.c:20: error: expected constructor, destructor,
    > or type conversion before '.' token
    > vector_static_function.c:21: error: expected constructor, destructor,
    > or type conversion before '.' token
    >
    >
    > when compiling this:
    >
    >
    > // about static vector (of pointers) member function
    >
    > #include <iostream>
    > #include <vector>
    > using namespace std;
    >
    > class G4PreCompoundModel
    > {
    > public:
    > static vector<int*> * GetCreatorModels()
    > { return &G4PreCompoundModel::theCreatorModels; }
    >
    > // private:
    >
    > static vector<int*> theCreatorModels;
    >
    > };
    >
    > int uno=1, dos=2;
    > G4PreCompoundModel::theCreatorModels.push_back(&uno);

    ^^^^^^^ what is this supposed to be ? A statement is not allowed here,
    it must be in a function block.

    Look up your C++ book on how to define static data members and how to
    initialize them.

    > G4PreCompoundModel::theCreatorModels.push_back(&dos);
    >
    > int main ()
    > {
    > G4PreCompoundModel::GetCreatorModels();
    > return 0;
    > }
    >
    > Thanks in advance amd best regards
    >
    > Jose Manuel
    >
    Gianni Mariani, May 19, 2007
    #2
    1. Advertising

  3. Josefo

    Salt_Peter Guest

    On May 19, 1:46 pm, Josefo <> wrote:
    > Hello, is someone so kind to tell me why I am getting the following
    > errors ?
    >
    > vector_static_function.c:20: error: expected constructor, destructor,
    > or type conversion before '.' token
    > vector_static_function.c:21: error: expected constructor, destructor,
    > or type conversion before '.' token
    >
    > when compiling this:
    >
    > // about static vector (of pointers) member function
    >
    > #include <iostream>
    > #include <vector>
    > using namespace std;
    >
    > class G4PreCompoundModel
    > {
    > public:
    > static vector<int*> * GetCreatorModels()
    > { return &G4PreCompoundModel::theCreatorModels; }
    >
    > // private:
    >
    > static vector<int*> theCreatorModels;


    Where is the definition for this static member?

    >
    > };
    >
    > int uno=1, dos=2;
    > G4PreCompoundModel::theCreatorModels.push_back(&uno);
    > G4PreCompoundModel::theCreatorModels.push_back(&dos);


    These push_back() calls are not allowed outside main() or another
    function. You can't code anything else but a declaration or a
    definition in a namespace (in this case: the global namespace).

    >
    > int main ()
    > {
    > G4PreCompoundModel::GetCreatorModels();
    > return 0;
    >
    > }
    >
    > Thanks in advance amd best regards
    >
    > Jose Manuel
    Salt_Peter, May 19, 2007
    #3
  4. Josefo wrote:
    > Hello, is someone so kind to tell me why I am getting the following
    > errors ?
    >
    >
    > vector_static_function.c:20: error: expected constructor, destructor,
    > or type conversion before '.' token
    > vector_static_function.c:21: error: expected constructor, destructor,
    > or type conversion before '.' token
    >
    >
    > when compiling this:
    >
    >
    > // about static vector (of pointers) member function
    >
    > #include <iostream>
    > #include <vector>
    > using namespace std;
    >
    > class G4PreCompoundModel
    > {
    > public:
    > static vector<int*> * GetCreatorModels()
    > { return &G4PreCompoundModel::theCreatorModels; }
    >
    > // private:
    >
    > static vector<int*> theCreatorModels;
    >
    > };
    >
    > int uno=1, dos=2;
    > G4PreCompoundModel::theCreatorModels.push_back(&uno);
    > G4PreCompoundModel::theCreatorModels.push_back(&dos);
    >
    > int main ()
    > {
    > G4PreCompoundModel::GetCreatorModels();
    > return 0;
    > }
    >
    > Thanks in advance amd best regards
    >
    > Jose Manuel
    >


    Here's how to write your code so that it works, (basically your design
    has static in the wrong place)

    class G4PreCompoundModel
    {
    public:
    static G4PreCompoundModel& GetCreatorModels()
    {
    static G4PreCompoundModel theModel;
    return theModel;
    }

    private:
    G4PreCompoundModel()
    {
    uno = 1;
    dos = 2;
    theCreatorModels.push_back(&uno);
    theCreatorModels.push_back(&dos);
    }

    int uno;
    int dos;
    vector<int*> theCreatorModels;

    };

    Now all you have to do is add whatever methods you require to
    G4PreCompoundModel.

    john
    John Harrison, May 20, 2007
    #4
  5. Josefo

    Josefo Guest

    On May 20, 8:45 am, John Harrison <> wrote:
    > Josefo wrote:
    > > Hello, is someone so kind to tell me why I am getting the following
    > > errors ?

    >
    > > vector_static_function.c:20: error: expected constructor, destructor,
    > > or type conversion before '.' token
    > > vector_static_function.c:21: error: expected constructor, destructor,
    > > or type conversion before '.' token

    >
    > > when compiling this:

    >
    > > // about static vector (of pointers) member function

    >
    > > #include <iostream>
    > > #include <vector>
    > > using namespace std;

    >
    > > class G4PreCompoundModel
    > > {
    > > public:
    > > static vector<int*> * GetCreatorModels()
    > > { return &G4PreCompoundModel::theCreatorModels; }

    >
    > > // private:

    >
    > > static vector<int*> theCreatorModels;

    >
    > > };

    >
    > > int uno=1, dos=2;
    > > G4PreCompoundModel::theCreatorModels.push_back(&uno);
    > > G4PreCompoundModel::theCreatorModels.push_back(&dos);

    >
    > > int main ()
    > > {
    > > G4PreCompoundModel::GetCreatorModels();
    > > return 0;
    > > }

    >
    > > Thanks in advance amd best regards

    >
    > > Jose Manuel

    >
    > Here's how to write your code so that it works, (basically your design
    > has static in the wrong place)
    >
    > class G4PreCompoundModel
    > {
    > public:
    > static G4PreCompoundModel& GetCreatorModels()
    > {
    > static G4PreCompoundModel theModel;
    > return theModel;
    > }
    >
    > private:
    > G4PreCompoundModel()
    > {
    > uno = 1;
    > dos = 2;
    > theCreatorModels.push_back(&uno);
    > theCreatorModels.push_back(&dos);
    > }
    >
    > int uno;
    > int dos;
    > vector<int*> theCreatorModels;
    >
    > };
    >
    > Now all you have to do is add whatever methods you require to
    > G4PreCompoundModel.
    >
    > john


    Thanks, John for your assistance. A sent a reply yesterday, but it
    does not appear published. Therefore, I write it again (sorry in
    advance if it shows up duplicated).Well, when I received your
    suggestion I had already more or less "elegantly" (or efficiently)
    solved the problem. Here you have the code:


    ///begin of the code
    // about static vector (of pointers) member function
    //19/05/07...my solution

    #include <iostream>
    #include <vector>
    using namespace std;

    class G4PreCompoundModel
    {
    public:
    static int uno, dos;

    static vector<int*> * GetCreatorModels()
    { return &
    G4PreCompoundModel::theCreatorModels; }

    // private:

    static vector<int*> theCreatorModels;

    private:
    static bool __init;
    static bool init() {
    theCreatorModels.push_back(&uno);
    theCreatorModels.push_back(&dos);
    return true;
    }
    };
    int G4PreCompoundModel::uno=1;
    int G4PreCompoundModel::dos=2;
    vector<int*> G4PreCompoundModel::theCreatorModels;
    bool G4PreCompoundModel::__init = G4PreCompoundModel::init();

    int main ()
    {
    int uno=1,dos=2;
    vector<int*> * p=G4PreCompoundModel::GetCreatorModels();
    cout<<"The returned pointer 'p' (to vector of pointers to integers)=
    "<<p<<endl;

    int ** p1;
    int ** p2;
    p1=&((*p).at(0))+1;
    p2=&((*p).at(1));

    if(uno==*((*p).at(0)) && dos==*((*p).at(1)))

    cout<<"Ok...uno =*((*p).at(0)) = "<<*((*p).at(0))<<" and dos =
    *((*p).at(1)) ="<<*((*p).at(1))<<endl;
    if (p1==p2)
    {cout<<"Right!..the pointers (to vector member pointers to integers)
    are sequential!!"<<endl;
    cout<<"&((*p).at(0))+1="<<&((*p).at(0))+1<<"
    &((*p).at(1))="<<p2<<endl;}
    cout<<"The vector member pointers (to integers..) are not sequential
    (why shoud they?):"<<endl;
    cout<<"(*p).at(0) = "<<(*p).at(0)<<" (*p).at(1) =
    "<<(*p).at(1)<<endl;
    return 0;
    }

    ////// end of the code



    When implementing your suggestion I had to comment the /private
    declaration in G4PreCompoundModel class in order to have acces to its
    constructor and hence to ints vector members. Here you hacve it:


    ///begin of the code
    // about static vector (of pointers) member function
    //20/05/07...John Allison's solution

    #include <iostream>
    #include <vector>
    using namespace std;
    class G4PreCompoundModel
    {
    public:
    static G4PreCompoundModel& GetCreatorModels()
    {
    static G4PreCompoundModel theModel;
    return theModel;
    }

    // private:
    G4PreCompoundModel()
    {
    uno = 1;
    dos = 2;
    theCreatorModels.push_back(&uno);
    theCreatorModels.push_back(&dos);
    }

    int uno;
    int dos;
    vector<int*> theCreatorModels;

    };


    int main ()
    {
    int uno=1,dos=2;
    int * puno;
    int * pdos;
    puno =G4PreCompoundModel::GetCreatorModels().theCreatorModels.at(0);
    pdos =G4PreCompoundModel::GetCreatorModels().theCreatorModels.at(1);



    if(uno==*puno && dos==*pdos)
    cout<<"Ok...uno =*puno = "<<*puno<<" and dos = *pdos
    ="<<*pdos<<endl;
    if (&puno+1==&pdos) {
    cout<<"The pointers (to vector member pointers to integers) are
    sequential:"<<endl;
    cout<<"&puno+1="<<&puno+1<<" &pdos="<<&pdos<<endl;
    cout<< "why they are not????"<<endl;}
    else
    { cout<<" The pointers (to vector member pointers to integers) are
    NOT sequential:"<<endl;
    cout<<"&puno+1="<<&puno+1<<" &pdos="<<&pdos<<endl;
    cout<< "why ????"<<endl;}
    cout<<"The vector member pointers (to integers..) are not sequential
    (why shoud they?):"<<endl;
    cout<<"puno = "<<puno<<" pdos = "<<pdos<<endl;

    return 0;
    }

    ///end of the code

    But which puzzles me is the fact that the pointers to the vector
    members (also pointers to integers, by he way) are not
    sequential...and they are sequential when constructucted in "my
    way" (my code). Can you tell me why?? Cheers and many thanks in
    advance

    Jose Manuel
    Jose Manuel
    Josefo, May 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    8
    Views:
    851
    Dhruv
    Jul 2, 2003
  2. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  3. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
  4. paul
    Replies:
    8
    Views:
    707
    Alf P. Steinbach
    Apr 30, 2009
  5. A L
    Replies:
    12
    Views:
    2,048
Loading...

Share This Page