Hey there,
I'm working through Stroustrup's book on learning programming, and one of his exercises is to build a class that will output the arc shape. My problem is that the compiler says that my constructor is an unresolved external. I once got this problem because I hadn't included the proper libraries, but I know that isn't the case here. Here's my code:
void set_width(int ww){w=ww;}
int width() const {return w;}
void set_height(int hh) {h=hh;}
int height() const {return h;}
private :
int w;
int h;
int a1;
int a2;
Point p;
};
int main(){
Simple_window win(Point(100,100),600,400,"Canvas");
Marc A(Point(150,150), 200, 200, 100, 110);
win.attach(A);
}
No, where do you define the constructor? The definition is the code between curly braces {}, what you have there is only a declaration - that is, you are declaring to the compiler that it exists, but will be defined elsewhere in the code.
I was under the impression that defining it was optional. Does it just need brackets for the sake of format or is there something I should include in it?
Edit: I'm guessing there's something I should add, since I added the empty brackets and now it starts, but it immediately closes.
Hmm, I get what you're saying in theory, but I seem to be having trouble putting it into practice. I just put in "
struct Marc:Shape{
Marc()
: p(20,20), w(45), h(45), a1(30), a2(70){};"
And it told me that no overloaded function takes five arguments. I'll try it with the other format you suggested and get back to you.
Edit: Yep, same error for Marc(){p=(Point(20,20)), w=45, h=45, a1=30, a2=70;};
Edit, the deux: Okay, so I combined the original and new constructor, and now I have: "Marc(Point p, int w, int h, double a1, double a2){p=(Point(20,20)), w=45, h=45, a1=30, a2=70;};"
It's not giving me an error, but I'm back to having the program run for a moment and then completing.
For the sake of not editing my post a third time, I just have to say, I've tried quite a few different formats for my constructor, and I either get the same result or "no default constructor exists for class." I feel like Stroustrup skipped over something important with constructors, because I'm not getting this.
Use your original constructor and combine it with what you have now: Marc(Point p, int w, int h, double a1, double a2) : p(p), w(w), h(h), a1(a1), a2(a2) {}