something wrong when i compile my own struct

Discussion in 'C++' started by remlostime, Feb 13, 2008.

  1. remlostime

    remlostime Guest

    struct lineType
    {
    int x, y;
    lineType(int tx, int ty)
    {
    if (tx == 0)
    {
    ty = 1 << 20;
    tx = 1;
    }
    x = tx;
    y = ty;
    }
    };
    lineType line[30000];

    when i compile it
    error: no matching function for call to `lineType::lineType()
    why?
     
    remlostime, Feb 13, 2008
    #1
    1. Advertising

  2. remlostime

    Lars Uffmann Guest

    remlostime wrote:
    > lineType(int tx, int ty)
    >
    > when i compile it
    > error: no matching function for call to `lineType::lineType()
    > why?
    >


    You defined a "constructor" - your declaration would probably need to
    look like
    lineType line[30000](0, 0);

    But I have never defined an array of classes, or an array of structs
    that are defined like a class. So I wouldn't know for sure. Either way,
    a workaround would be if you rename your inline function to something
    other than the struct name.

    Best Regards,

    Lars
     
    Lars Uffmann, Feb 13, 2008
    #2
    1. Advertising

  3. Lars Uffmann wrote:
    > remlostime wrote:
    >> lineType(int tx, int ty)
    >>
    >> when i compile it
    >> error: no matching function for call to `lineType::lineType()
    >> why?
    >>

    >
    > You defined a "constructor" - your declaration would probably need to
    > look like
    > lineType line[30000](0, 0);


    That's a syntax error.

    > But I have never defined an array of classes, or an array of structs
    > that are defined like a class. So I wouldn't know for sure. Either
    > way, a workaround would be if you rename your inline function to
    > something other than the struct name.


    .... or give the constructor the default arguments (if 0,0 are what
    you want you could do

    lineType(int tx = 0, int ty = 0);

    ) which should make it the default constructor, acceptable to use
    with an array without specifying the initialisers for all elements.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 13, 2008
    #3
  4. remlostime

    Guest

    On Feb 13, 10:17 am, remlostime <> wrote:
    > struct lineType
    > {
    >   int x, y;
    >   lineType(int tx, int ty)
    >   {
    >     if (tx == 0)
    >     {
    >       ty = 1 << 20;
    >       tx = 1;
    >     }
    >     x = tx;
    >     y = ty;
    >   }};
    >
    > lineType line[30000];
    >
    > when i compile it
    > error: no matching function for call to `lineType::lineType()
    > why?


    In order to create an array, your struct needs a default
    constructor (i.e. one that takes no arguments).

    HTH
     
    , Feb 13, 2008
    #4
  5. remlostime

    manish sahu Guest

    On Feb 13, 8:17 pm, remlostime <> wrote:
    > struct lineType
    > {
    >   int x, y;
    >   lineType(int tx, int ty)
    >   {
    >     if (tx == 0)
    >     {
    >       ty = 1 << 20;
    >       tx = 1;
    >     }
    >     x = tx;
    >     y = ty;
    >   }};
    >
    > lineType line[30000];
    >
    > when i compile it
    > error: no matching function for call to `lineType::lineType()
    > why?


    hey its a wrong method because u give a same name to structure and
    compiler gets confused and we cannot have a constructor in c
     
    manish sahu, Feb 13, 2008
    #5
  6. remlostime

    red floyd Guest

    manish sahu wrote:
    > On Feb 13, 8:17 pm, remlostime <> wrote:
    >> struct lineType
    >> {
    >> int x, y;
    >> lineType(int tx, int ty)
    >> {
    >> if (tx == 0)
    >> {
    >> ty = 1 << 20;
    >> tx = 1;
    >> }
    >> x = tx;
    >> y = ty;
    >> }};
    >>
    >> lineType line[30000];
    >>
    >> when i compile it
    >> error: no matching function for call to `lineType::lineType()
    >> why?

    >
    > hey its a wrong method because u give a same name to structure and
    > compiler gets confused and we cannot have a constructor in c


    So? It's C++. As has already been pointed out, the issue is that there
    is no default constructor.
     
    red floyd, Feb 13, 2008
    #6
  7. remlostime

    Kira Yamato Guest

    On 2008-02-13 10:52:47 -0500, "Victor Bazarov" <> said:

    > Lars Uffmann wrote:
    >> remlostime wrote:
    >>> lineType(int tx, int ty)
    >>>
    >>> when i compile it
    >>> error: no matching function for call to `lineType::lineType()
    >>> why?
    >>>

    >>
    >> You defined a "constructor" - your declaration would probably need to
    >> look like
    >> lineType line[30000](0, 0);

    >
    > That's a syntax error.
    >
    >> But I have never defined an array of classes, or an array of structs
    >> that are defined like a class. So I wouldn't know for sure. Either
    >> way, a workaround would be if you rename your inline function to
    >> something other than the struct name.

    >
    > ... or give the constructor the default arguments (if 0,0 are what
    > you want you could do
    >
    > lineType(int tx = 0, int ty = 0);
    >
    > ) which should make it the default constructor, acceptable to use
    > with an array without specifying the initialisers for all elements.


    Neat. I thought you need an actual default constructor, i.e., one that
    takes no argument.

    --

    // kira
     
    Kira Yamato, Feb 14, 2008
    #7
  8. remlostime

    Ron Natalie Guest

    Kira Yamato wrote:

    > Neat. I thought you need an actual default constructor, i.e., one that
    > takes no argument.
    >

    Yep, the definition of a default constructor is not one with no args,
    but one that can be called with no args. Similarly a copy constructor
    is defined when you have the first arg of T& or const T& and any
    additional args are defaulted.
     
    Ron Natalie, Feb 14, 2008
    #8
  9. remlostime

    Kira Yamato Guest

    On 2008-02-14 08:49:01 -0500, Ron Natalie <> said:

    > Kira Yamato wrote:
    >
    >> Neat. I thought you need an actual default constructor, i.e., one that
    >> takes no argument.
    >>

    > Yep, the definition of a default constructor is not one with no args,
    > but one that can be called with no args. Similarly a copy constructor
    > is defined when you have the first arg of T& or const T& and any
    > additional args are defaulted.


    Ooo. Double neat. :)

    --

    // kira
     
    Kira Yamato, Feb 14, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    774
  2. Saverio M.
    Replies:
    0
    Views:
    518
    Saverio M.
    Jul 3, 2006
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. Nagaraj
    Replies:
    1
    Views:
    869
    Lionel B
    Mar 1, 2007
  5. David Filmer
    Replies:
    17
    Views:
    266
    J. Romano
    Aug 18, 2004
Loading...

Share This Page