something wrong when i compile my own struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • remlostime

    something wrong when i compile my own struct

    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::line Type()
    why?

  • manish sahu

    #2
    Re: something wrong when i compile my own struct

    On Feb 13, 8:17 pm, remlostime <remlost...@gma il.comwrote:
    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::line Type()
    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

    Comment

    • red floyd

      #3
      Re: something wrong when i compile my own struct

      manish sahu wrote:
      On Feb 13, 8:17 pm, remlostime <remlost...@gma il.comwrote:
      >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::line Type()
      >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.

      Comment

      • Ron Natalie

        #4
        Re: something wrong when i compile my own struct

        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.

        Comment

        • Kira Yamato

          #5
          Re: something wrong when i compile my own struct

          On 2008-02-14 08:49:01 -0500, Ron Natalie <ron@spamcop.ne tsaid:
          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

          Comment

          Working...