Encountering a linker error with my constructor.

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:

#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include<FL/Fl_Window.H>
#include "../GUI.h"
#include "../Simple_window.h"
#include "../Point.h"
#include "../Graph.h"

struct Marc:Shape{
Marc(Point p, int w, int h, double a1, double a2);

void draw_lines() const
{fl_arc(p.x, p.y, w, h, a1, a2);}

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);
}

Any ideas?
Marc(Point p, int w, int h, double a1, double a2);
Where do you give a definition to it?
Assuming you mean giving values to the variables, I do that in main. Is there something wrong with that?
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.
Last edited on
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.
Last edited on
Well, you actually have to assign to the members in your class. There are two ways to do this - I'll give a small example:
1
2
3
4
5
6
7
class MyClass
{
    int x;
    int y, z;
public:
    MyClass() : x(0), y(2) { z = 7; }
};

MyClass mc;
This constructs x from 0, constructs y from 2, and constructs z with the default constructor for int, then after that it assigns 7 to z.

1
2
3
4
5
6
class Another
{
    std::string s;
public:
    Another(const std::string &str) : s(str) {}
};

Another a ("hello");
Guess what this does ;)
Last edited on
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.
Last edited on
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) {}
Thanks, LB, you've been most helpful.
Topic archived. No new replies allowed.