Trouble passing integers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chad.denyar@gmail.com

    Trouble passing integers

    Hi, I have a constructor and a function called setValues. They're
    both the same in how they act, but the function errors during
    compiling. Could anyone give me any advice as to why this is
    occurring?

    Here is the code:

    //beginning
    #include <iostream>
    #include <cstdlib>
    using namespace std;

    class area {
    int *height, *width;

    public:
    area(int, int);
    area();
    ~area();
    void setValues(int, int);
    int getArea() {
    return (*height * *width);
    }
    };

    area::area() {
    width = new int;
    height = new int;
    }

    area::area(int a, int b) {
    width = new int;
    height = new int;
    *width = a;
    *height = b;
    }

    area::~area () {
    delete width;
    delete height;
    }

    void area::setValues (int a, int b) {
    *width = a;
    *height = b;
    }

    int main(int argc, char *argv[]) {
    int t1 = atoi(argv[1]);
    int t2 = atoi(argv[2]);
    area square(t1, t2);
    area rect();
    int t3 = atoi(argv[3]);
    int t4 = atoi(argv[4]);
    rect.setValues( t3,t4);
    cout << "The area is: " << square.getArea( ) << endl;
    return 0;
    }
    //end

    Thanks,
    Chad

  • Rolf Magnus

    #2
    Re: Trouble passing integers

    chad.denyar@gma il.com wrote:
    Hi, I have a constructor and a function called setValues. They're
    both the same in how they act, but the function errors during
    compiling.
    In which line and what's the error you get?
    Could anyone give me any advice as to why this is occurring?
    Yes, the compiler. It usually gives you an error message that tells you the
    reason. That message might not be meaningful to you, but to others here, it
    probably is.
    Here is the code:
    >
    //beginning
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    >
    class area {
    int *height, *width;
    Why not simply int? Using pointers and dynamic memory just makes it more
    complicated and slower without giving you any benefit.
    public:
    area(int, int);
    area();
    ~area();
    void setValues(int, int);
    int getArea() {
    return (*height * *width);
    }
    };
    Your class fails to obey the Rule of Three. As soon as you try to copy an
    object of type area, you get undefined behavior.
    area::area() {
    width = new int;
    height = new int;
    }
    >
    area::area(int a, int b) {
    width = new int;
    height = new int;
    *width = a;
    *height = b;
    }
    >
    area::~area () {
    delete width;
    delete height;
    }
    >
    void area::setValues (int a, int b) {
    *width = a;
    *height = b;
    }
    >
    int main(int argc, char *argv[]) {
    int t1 = atoi(argv[1]);
    int t2 = atoi(argv[2]);
    area square(t1, t2);
    area rect();
    Note that the above line declares a function called rect that returns an
    area object.
    int t3 = atoi(argv[3]);
    int t4 = atoi(argv[4]);
    rect.setValues( t3,t4);
    Since rect is a function, this won't work.
    cout << "The area is: " << square.getArea( ) << endl;
    return 0;
    }
    //end
    >
    Thanks,
    Chad

    Comment

    Working...