Sign in to follow this  
zappernapper

simple constructor

Recommended Posts

zappernapper    186
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?

Share this post


Link to post
Share on other sites
andrew_480    163
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).

Share this post


Link to post
Share on other sites
rip-off    10976
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.

Share this post


Link to post
Share on other sites
rip-off    10976
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++.

Share this post


Link to post
Share on other sites
SeeJay    122
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.

Share this post


Link to post
Share on other sites
Roboguy    794
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this