Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C++ Constructor help. PLEASE HELP!!!

rjdfhorn_06rjdfhorn_06 Posts: 8Member
Can someone please show me how I would create these constructors in the source file...my compilers keeps telling me I have errors. I know I set up my main, header, and source file correctly because I am getting no errors until I start implementing the constructors.

In the HEADER file:(xArray.h)
class xArray{
public:
//constructors
xArray();
xArray(const xArray& a);
xArray(const int x);
private:
size_t len;
size_t size;
//data of int array
int *data;
};

In the source file: (xArray.cpp)
These are my implementations by they are giving me errors. I'm still a beginner at C++, please help.

xArray::xArray():len(0),size(32),data(new int[size]);
xArray::xArray(const xArray& a){
len = a.length;
size = len*2;
}
xArray::xArray(const int x){
//unsure about this one
}

PLEASE HELP!!!All help is appreciated.

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Can someone please show me how I would create these constructors in
    : the source file...my compilers keeps telling me I have errors. I
    : know I set up my main, header, and source file correctly because I
    : am getting no errors until I start implementing the constructors.
    :

    I'll write a few comments in your code. Also, next time it might be handy if you posted the actual errors along with the lines they point to, so we can help you better.

    : In the HEADER file:(xArray.h)
    [code]
    : class xArray{
    : public:
    : //constructors
    : xArray();
    : xArray(const xArray& a);
    [color=Green] //You don't need constant here, because it's a
    // non-reference datatype[/color]
    : xArray([color=Red]const [/color]int x);
    : private:
    : size_t len;
    : size_t size;
    : //data of int array
    : int *data;
    : };
    : [/code]
    : In the source file: (xArray.cpp)
    : These are my implementations by they are giving me errors. I'm still
    : a beginner at C++, please help.
    :
    [code]
    [color=Green]// You need to provide a function body, even if it's left empty[/color]
    : xArray::xArray():len(0),size(32),data(new int[size])
    [color=Blue]{ }[/color]
    : xArray::xArray(const xArray& a){
    [color=Green] //You are using a 'length' member of xArray, but you haven't
    // defined any such a variable (note that even if you had
    // declared it as a function, you'd still be missing the
    // function call parenthesis () behind length)
    //The solution is to define length as a function and
    // call it here by adding ()[/color]
    : [color=Red]len = a.length;[/color]
    : size = len*2;
    : }
    : xArray::xArray(const int x){
    : //unsure about this one
    : }
    : [/code]
    : PLEASE HELP!!!All help is appreciated.
    :

    For the last part about which you are unsure, you probably want to do something similar to what you did with the default constructor, only then setting size to x instead of 32.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.