473,396 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

about static vector (of pointers) member function


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(&un o);
G4PreCompoundModel::theCreatorModels.push_back(&do s);

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

Thanks in advance amd best regards

Jose Manuel

May 19 '07 #1
4 3479
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(&un o);
^^^^^^^ 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(&do s);

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

Thanks in advance amd best regards

Jose Manuel
May 19 '07 #2
On May 19, 1:46 pm, Josefo <josemanue...@gmail.comwrote:
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(&un o);
G4PreCompoundModel::theCreatorModels.push_back(&do s);
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

May 19 '07 #3
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(&un o);
G4PreCompoundModel::theCreatorModels.push_back(&do s);

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
May 20 '07 #4
On May 20, 8:45 am, John Harrison <john_androni...@hotmail.comwrote:
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(&un o);
G4PreCompoundModel::theCreatorModels.push_back(&do s);
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().theCreator Models.at(0);
pdos =G4PreCompoundModel::GetCreatorModels().theCreator Models.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

May 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cppaddict | last post by:
Hi, I have the following line inside one of my class member functions: static bool tested = false; if (!tested) { MessageBox(NULL, "Hi", "Testing", MB_OK | MB_ICONEXCLAMATION); tested =...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
3
by: Sharpdew | last post by:
class Test { double i; public: void fun(){} void foo(void) { cout << "as"; }
1
by: ndbecker2 | last post by:
In the following: class X { void F() { static int g; } }; In a non-member function, static data is has static lifetime. In a member function, is a single static value shared amongst all...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.