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///////////////////////////////////////////
-
//sphere.h sphere definition
-
#ifndef SPHERE_H_
-
#define SPHERE_H_
-
-
#include<vector>
-
using namespace std;
-
-
class particle
-
{
-
private:
-
-
double r; // position
-
double v; // velocity
-
double _R ; // radius of a particle
-
-
-
public:
-
-
particle():r(0.0),v(0.0),_T(0.0),_R(0.5){} //default constructor.
-
-
particle(const double x, const double vx, const double Radius):r(x), v(vx) {_R=Radius;}
-
-
particle(const double Radius):_R(Radius)
-
{r=0.0, v=0.0;}
-
-
~particle(){};
-
-
//talking of postion and velocity form
-
Vector & pos(){return r;}
-
Vector pos()const{return r;}
-
Vector & velocity(){return v;}
-
Vector velocity()const{return v;}
-
-
double Radius()const{return _R;}
-
friend ostream & operator<<(ostream & os, const particle & v);
-
};
-
typedef particle Particle;
-
#endif
-
/////////////////////////Sphere.cpp: contains the description of functions///////////
// definition sphere .h member functions
-
#include<iostream>
-
using namespace std;
-
#include"sphere.h"
-
-
// Now we write the particle class function
-
-
ostream &operator<<(ostream &os, const particle & v)
-
{
-
cout.setf(ios_base::fixed);
-
cout.precision(14);
-
cout.setf(ios_base::showpoint);
-
-
cout<<"Wellcome to particle state:\n";
-
cout<<"Radius of the particle: "<<v.Radius()<<"\n";
-
cout<<"The Position : "<<v.r<<"\n";
-
cout<<"The Velocity : "<<v.v<<"\n";
-
-
return os;
-
}
-
/////////////////////////Here The test program//////////////////////////////////
// use simple sphere class
-
#include<iostream>
-
#include<vector>
-
using namespace std;
-
#include"sphere.h"
-
int main()
-
{
-
cout<<"Wellcome to stl particle state\n";
-
Particle x;
-
Particle y(4.5);
-
-
-
vector<Particle(4.5)> z; ///LINE NO: 13 the compilation error
-
z.resize(5);
-
cout<<x<<endl;
-
-
cout<<z[1]<<endl;
-
-
return 0;
-
}
/////////////////////////////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.