By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

creating array with user defined datatype

P: 10
HI all,

let me explain the problem:
In my simulation I am using two class: particle and Container.

//////////// the outline of particle class/////////////////////
Sphere.h---->
Expand|Select|Wrap|Line Numbers
  1. #ifndef SPHERE_H_
  2. #define SPHERE_H_
  3.  
  4.     Class Particle
  5.    {   
  6.        private:
  7.  
  8.         double x;    // position of the particle
  9.         double Vx;  // velocity of the particle
  10.        double _R;   // radius of the particle
  11.  
  12.         public:
  13.        particle()x(0.0),Vx(0.0),_R(0.0){} //default initialisation
  14.        particle(double radius)x(0.0),Vx(0.0),_R(radius){} // user defined one
  15.  
  16.  //   and the rest of the code goes here.
  17.     };    
  18. #endif
  19.  
In another class named "Container" i need to create an array of particle.


Expand|Select|Wrap|Line Numbers
  1.  #ifndef CONTAINER_H_
  2. #define CONTAINER_H_
  3.  
  4. #include<vector>
  5. using namespace std;
  6.  #include"sphere.h"
  7.  
  8.    class Container
  9.     {
  10.         private:
  11.        vector<particle> HardSphere;
  12.  
  13.   // and the rest of the program part goes here.
  14.  
  15.    };
  16.  
  17. #endif
  18.  
The problem is that in the "container" i want to have the array of particles with desired radius say 1.0. But i always find it invokes the default constructor which set radius to zero. Of course latter on i can run a do loop to set the radius to a desired value. But i was wondering if there is any other way to invoke the different particle constructor from the default one as shown in the particle class. Or using the do loop is the only way.

thank you
anupam
Jun 13 '07 #1
Share this Question
Share on Google+
7 Replies


Savage
Expert 100+
P: 1,764
HI all,

let me explain the problem:
In my simulation I am using two class: particle and Container.

//////////// the outline of particle class/////////////////////
Sphere.h---->
#ifndef SPHERE_H_
#define SPHERE_H_

Class Particle
{
private:

double x; // position of the particle
double Vx; // velocity of the particle
double _R; // radius of the particle

public:
particle()x(0.0),Vx(0.0),_R(0.0){} //default initialisation
particle(double radius)x(0.0),Vx(0.0),_R(radius){} // user defined one

// and the rest of the code goes here.
};
#endif

In another class named "Container" i need to create an array of particle.



#ifndef CONTAINER_H_
#define CONTAINER_H_

#include<vector>
using namespace std;
#include"sphere.h"

class Container
{
private:
vector<particle> HardSphere;

// and the rest of the program part goes here.

};

#endif

The problem is that in the "container" i want to have the array of particles with desired radius say 1.0. But i always find it invokes the default constructor which set radius to zero. Of course latter on i can run a do loop to set the radius to a desired value. But i was wondering if there is any other way to invoke the different particle constructor from the default one as shown in the particle class. Or using the do loop is the only way.

thank you
anupam
How are you creating particles?

Savage
Jun 13 '07 #2

P: 10
How are you creating particles?

Savage
Savage, i could not follow you. Actually it is a part of a very long program. so i only showed the relevant part. Could you please explain me your question in details.

just to inform that i access a particle in array as:
for 2nd particle:
HardSphere[1].Pos() // for position
HardSphere[1].Vel() // for velocity
HardSphere[1].Radius() // for radius.
etc.

the function Pos() and Vel() are defined in the particle class:
which return the position and the velocity respectively.
Jun 13 '07 #3

Savage
Expert 100+
P: 1,764
Savage, i could not follow you. Actually it is a part of a very long program. so i only showed the relevant part. Could you please explain me your question in details.

just to inform that i access a particle in array as:
for 2nd particle:
HardSphere[1].Pos() // for position
HardSphere[1].Vel() // for velocity
HardSphere[1].Radius() // for radius.
etc.

the function Pos() and Vel() are defined in the particle class:
which return the position and the velocity respectively.
Sorry,this is how u create array:

vector<particle> HardSphere;

particle will envoke defalut constructor unless you call overrided constructor(one that you have created).Your own constructor will be only called if u specify his argument,in this case that's double radius,so try this:

vector<particle(1.0)> HardSphere;

Savage
Jun 13 '07 #4

P: 10
Sorry,this is how u create array:

vector<particle> HardSphere;

particle will envoke defalut constructor unless you call overrided constructor(one that you have created).Your own constructor will be only called if u specify his argument,in this case that's double radius,so try this:

vector<particle(1.0)> HardSphere;

Savage
Hi Savage,

thank you for your suggestion. But Still i am getting compilation error. So let me write the program in full form. That might help to point out the error message.

/////////////// Sphere.h///////////////////////////////////////////
Expand|Select|Wrap|Line Numbers
  1. //sphere.h sphere definition
  2. #ifndef SPHERE_H_
  3. #define SPHERE_H_
  4.  
  5. #include<vector>
  6. using namespace std;
  7.  
  8. class particle
  9. {
  10. private:
  11.  
  12.       double r;  // position 
  13.       double v;  // velocity 
  14.       double _R ;  // radius of a particle
  15.  
  16.  
  17. public:
  18.  
  19.       particle():r(0.0),v(0.0),_T(0.0),_R(0.5){}  //default constructor.
  20.  
  21.       particle(const double x,  const double vx, const double Radius):r(x), v(vx)  {_R=Radius;} 
  22.  
  23.       particle(const double Radius):_R(Radius)
  24.                {r=0.0, v=0.0;}
  25.  
  26.       ~particle(){};
  27.  
  28. //talking of postion and velocity  form     
  29.      Vector & pos(){return r;}
  30.      Vector  pos()const{return r;}
  31.      Vector & velocity(){return v;}
  32.      Vector  velocity()const{return v;}
  33.  
  34.      double Radius()const{return _R;}
  35.      friend ostream & operator<<(ostream & os, const particle & v);
  36. };
  37. typedef particle Particle;
  38. #endif
  39.  
/////////////////////////Sphere.cpp: contains the description of functions///////////
// definition sphere .h member functions
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. #include"sphere.h"
  4.  
  5. // Now we write the particle class function
  6.  
  7.     ostream &operator<<(ostream &os, const particle & v)
  8.      {
  9.            cout.setf(ios_base::fixed);
  10.            cout.precision(14);
  11.            cout.setf(ios_base::showpoint);
  12.  
  13.            cout<<"Wellcome to particle state:\n";
  14.            cout<<"Radius of the particle: "<<v.Radius()<<"\n";
  15.            cout<<"The Position : "<<v.r<<"\n";
  16.            cout<<"The Velocity : "<<v.v<<"\n";
  17.  
  18.             return os;
  19.     }
  20.  
/////////////////////////Here The test program//////////////////////////////////
// use simple sphere class
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. #include"sphere.h"
  5. int main()
  6. {
  7.     cout<<"Wellcome to stl particle state\n";
  8.     Particle x;
  9.     Particle y(4.5);
  10.  
  11.  
  12.     vector<Particle(4.5)> z;      ///LINE NO: 13 the compilation error 
  13.     z.resize(5);
  14.     cout<<x<<endl;
  15.  
  16.     cout<<z[1]<<endl;
  17.  
  18.     return 0;
  19. }
/////////////////////////////Here is the make file/////////////////////////
CC = c++
CFLAGS =
LDFLAGS =

Run: sphere.o
$(CC) $(CFLAGS) testsphere.cpp sphere.o $(LDFLAGS) -o Run

sphere.o: sphere.cpp
$(CC) $(CFLAGS) -c sphere.cpp

//////////////////////////Here is the compilation message////////////////////
[anupam@localhost GranAdvSTL2]$ make

c++ -c sphere.cpp
c++ testsphere.cpp Myvector.o Varr.o sphere.o -o Run
testsphere.cpp: In function `int main()':
testsphere.cpp:13: error: a call to a constructor cannot appear in a constant-expression
testsphere.cpp:13: error: template argument 1 is invalid
testsphere.cpp:13: error: template argument 2 is invalid
testsphere.cpp:13: error: invalid type in declaration before ';' token
testsphere.cpp:14: error: request for member `resize' in `z', which is of non-class type`int'
testsphere.cpp:16: error: invalid types `int[int]' for array subscript
make: *** [Run] Error 1
////////////////////////////////////////////////////////////////////////////////////////////////////////

I really appreciate your help regarding my problem.
thank you.
Jun 13 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
vector<Particle(4.5)> z; ///LINE NO: 13 the compilation error
You add Particle obects to the vector this way:
Expand|Select|Wrap|Line Numbers
  1. vector<Particle>  z;
  2. Particle  obj(4.5);
  3. z.push_back(obj);
  4.  
Jun 13 '07 #6

P: 10
You add Particle obects to the vector this way:
Expand|Select|Wrap|Line Numbers
  1. vector<Particle>  z;
  2. Particle  obj(4.5);
  3. z.push_back(obj);
  4.  
Thanks for your response. It works perfectly. But i am curious to know whether you can call a user defined constructor in the creation of array like in the above program. But it appears it always call the default constructor.

Do you think someway we can overload " new " operator to invoke user defined constructor in this case. I tried, but it didn't work.

anyway, thanks for showing me the simple way out.
Jun 14 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
You need to specify the initial contents when the array is created:

For an array of int you can:
Expand|Select|Wrap|Line Numbers
  1. int arr[2] = { 1,2};
  2.  
Here you specify the intial contents of the array elements between the braces. The compiler does not care if the int value is hard-coded or the result of a function call:
Expand|Select|Wrap|Line Numbers
  1. int arr1[2] = { fx(2), fx(3)};
  2.  
where:
Expand|Select|Wrap|Line Numbers
  1. int fx(int in)
  2. {
  3.     return in;
  4. }
  5.  
So do the same thing with an array of class obects:

Expand|Select|Wrap|Line Numbers
  1. class Test
  2. {
  3.      private:
  4.         int data;
  5.      public:
  6.         Test(int);
  7. };
  8. Test::Test(int in) : data(in)
  9. {
  10. }
  11.  
Now the trick is to call the constructor as the array elements are created.
Following the array of int example, you could:
Expand|Select|Wrap|Line Numbers
  1. Test array[2] = {Test(5), Test(6)};
  2.  
What this really does is call the class copy constructor. Test(5) creates a Test object. That object is copied to the array element and then it dies.

Be sure you have the necessary copy constructors if you try this.
Jun 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.