something wrong when i compile my own struct

R

remlostime

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?
 
L

Lars Uffmann

remlostime said:
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
 
V

Victor Bazarov

Lars said:
remlostime said:
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
 
A

AnonMail2005

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
 
M

manish sahu

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
 
R

red floyd

manish said:
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.
 
K

Kira Yamato

Lars said:
remlostime said:
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.
 
R

Ron Natalie

Kira said:
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.
 
K

Kira Yamato

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. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top