Hello,
I have a class Pdisk

class Pdisk
{
public :
	Pdisk(string diskname, int numberofblocks, int blocksize);
private :
	string diskname;
	int numberofblocks; 
	int blocksize;  
};

Now I want to make a class filesys which can access an object of class Pdisk. I am not sure how to pass this object.

filesys.h:

class fileSys{

public:
         Filesys(Pdisk& disk);
private:
	Pdisk disk;
};

filesys.cpp file:

Filesys::Filesys(Pdisk& disk){//...}

In my driver program I want to call

Pdisk disk("disk",64,128);
  fileSys fsys(disk);

I am using Visual C++ and get the error that there is no default constructor. The only constructor for Pdisk I have is the one I pasted here. Can someone explain to me how this works.
What I understand is that I have an existing object of class pdisk that I pass to the constructor of the class filesys. Since the Pdisk disk already exists I do not want to call the Pdisk constructor anymore. So I only pass the object by reference.
I do not understand the error message or how the call would be correct.

Thanks for any help!

class Pdisk
{
public :
	Pdisk(string diskname, int numberofblocks, int blocksize);
        Pdisk(): numberofblocks(0), blocksize(0){} //default ctor
private :
	string diskname;
	int numberofblocks; 
	int blocksize;  
};

A word of caution because from the question, I think that you don't fully grasp what is going to happen [if I am wrong then apologies].

Your code says this:

class fileSys{
public:
  Filesys(Pdisk& disk);
private:
   Pdisk disk;
};

And that implies that you are going to write the constructor something like this:

Filesys::Filesys(Pdisk& P) : 
  disk(P) 
{}

OR

Filesys::Filesys(Pdisk& P) 
{
   disk=P;
}

I suspect from the error that you have, you wrote the second. That way TWO thinks are going to happen that you don't want.

First, you are using the compiler generated copy constructor and compiler generated assignment operator, HOWEVER you are using a class Pdisk that is not a POD [Plain old data] class (because it contains std::string which has memory management within it). Thus you are going to get strange, difficult to repeat runtime errors.

Second, you are constructing Pdisk by using a default constructor (which you have now to give) and then coping the data in. Very inefficient.

The rule of thumb is: unless you are certain that the compilers default constructor, assigment operator, copy constructor and destructor are exactly what you want write them explicitly. i.e. your class declaration should look like this:

class Pdisk
{
  // stuff...
  public:
    Pdisk(const std::string&,int,int);
    Pdisk(const Pdisk&);
    Pdisk& operator=(const Pdisk&);
    ~Pdisk();
  // more stuff....
};

If you have 1% doubt, write them. If something strange happens when using the class and you haven't written them, write them and then debug.

I also don't think that you want a default constructor for this class, based on what you have written. At least make very sure you know were in the code it is being called from.

Edited 5 Years Ago by StuXYZ: n/a

This article has been dead for over six months. Start a new discussion instead.