simple constructor

Started by
8 comments, last by Roboguy 18 years ago
i'm sure it's something really stupid, but i need a fresh pair of eyes

struct sRect
{
    long left;
    long top;
    long right;
    long bottom;
    
    sRect(const long& l, const long& t, const long& r, const long& b)
    {
        left = l;
        top = t;
        right = r;
        bottom = b;
    };
    
    inline long GetVAxis()
    {return (left + ((right - left) >> 1));};
    inline long GetHAxis()
    {return (top + ((bottom - top) >> 1));};
    
    sPoint GetLT()
    {return sPoint(left, top);};
};

...
const sRect ZERO_RECT(0,0,0,0);
...

class cObject
{
    public:
        cObject(const sRect& position = ZERO_RECT)
        {
           m_rcPos = position; // error is here
        };
...

the compiler says that it can't find a match for sRect::sRect() what am i not seeing?
Advertisement
There is no default constructor, though I'm not sure why that's a problem. Just adding one will probably fix it.
____________________________________________________________Programmers Resource Central
i gahtered it wanted a default construcotr, but it shouldn't be asking for one... i'm defining values for every call to the sRect constructor, aren't i?
instead of defining a default i jst supplied default values, but it really shouldn't have been necessary... or can someone point out why?
Prototypes for functions have semi colons after them. Your constructor is implemented, and is not a prototype. Try removing the semi colon (same goes for your other functions).
I didn't even know structs had default constructors , where are the tutorials on that?
Hope I was helpful. And thank you if you were!
The reason is that your class has to call the default constuctor unless you tell it to do otherwise.

In your case, a default constructor won't hurt, but in general:

class cObject{    public:        cObject(const sRect& position = ZERO_RECT) : m_rcPos( position )         {        };...


Quote:
Prototypes for functions have semi colons after them. Your constructor is implemented, and is not a prototype. Try removing the semi colon (same goes for your other functions).


I've found compilers are very forgiving about stray semicolons, inside code or out in "space". Maybe its standard, I don't know, but that doest affect it. I have his code (modified) compiling here on my machine.
Quote:Original post by Samsonite
I didn't even know structs had default constructors , where are the tutorials on that?


A struct is a class that has default public access( and default public inheritance I think ).

Or likewise a class is a struct with default private ditto...

There are no other differences in c++.
Quote:Original post by rip-off
Quote:Original post by Samsonite
I didn't even know structs had default constructors , where are the tutorials on that?


A struct is a class that has default public access( and default public inheritance I think ).

Or likewise a class is a struct with default private ditto...

There are no other differences in c++.


Really!? No differences in how they are passed (by val/ref) or in memory management as in C# either? I'm relatively new to c++ so I'm just curious.
Quote:Original post by SeeJay
Quote:Original post by rip-off
Quote:Original post by Samsonite
I didn't even know structs had default constructors , where are the tutorials on that?


A struct is a class that has default public access( and default public inheritance I think ).

Or likewise a class is a struct with default private ditto...

There are no other differences in c++.


Really!? No differences in how they are passed (by val/ref) or in memory management as in C# either? I'm relatively new to c++ so I'm just curious.


Nope. That is the only difference.

This topic is closed to new replies.

Advertisement