Struct Question

This is a discussion on Struct Question within the C++ Programming forums, part of the General Programming Boards category; My compiler begged to differ saying no constructor in Vector3 matches a constructor with 0 arguments Not about the code ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    My compiler begged to differ saying no constructor in Vector3 matches a constructor with 0 arguments
    Not about the code you posted. The code you posted was perfectly fine. The problem is in the way you implement constructors:
    Code:
    Camera::Camera(float angle, Vector3 center, Vector3 Los){
      Angle = angle;
      Center = center; 
      VectorLOS = Los;
    }
    This is wrong. You should always use constructor initializers when you can. The problem here is that it first tries to default-initialize the vectors (but can't, because there is no default constructor) and then reassigns them. This constructor should look like this:
    Code:
    Camera::Camera(float angle, Vector3 center, Vector3 Los)
      : Angle(angle), Center(center), VectorLOS(Los)
    {}
    This will work without giving Vector3 a default constructor. It can also save typing by removing the component-wise assignment you sometimes do.
    Code:
    Triangle::Triangle(Vector3 center, float width, float height)
      Center(center), Width(width), Height(height)
    {}
    For that matter, why do you ever do a component-wise assignment?
    Code:
    void Triangle::MoveTo(Vector3 newCenter){
      //Center.X = newCenter.X;
      //Center.Y = newCenter.Y;
      //Center.Z = newCenter.Z;
      Center = newCenter;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    The last triangle was from the first tutorial, and before I added the Vector3 constructor, I will change it.

    Thanks for the information about the constructors though I don't understand exactly what you are saying if I just did it without writing a 3 argument constructor I would not be able to do this...
    Code:
    Triangle ThisTriangle(Vector3(0.0f, 0.0f, 10.0f), 1.0f, 1.0f);
    Which was the whole reason I added the constructor.

    I will definitely adjust the class initializations to the way you suggested. Mind if I ask what scenario would break my implementation?

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Lesshardtofind View Post
    The last triangle was from the first tutorial, and before I added the Vector3 constructor, I will change it.

    Thanks for the information about the constructors though I don't understand exactly what you are saying if I just did it without writing a 3 argument constructor I would not be able to do this...
    Code:
    Triangle ThisTriangle(Vector3(0.0f, 0.0f, 10.0f), 1.0f, 1.0f);
    Which was the whole reason I added the constructor.
    The 3-arg constructor is fine. I'm saying that you don't really need the 0-arg constructor.

    I will definitely adjust the class initializations to the way you suggested. Mind if I ask what scenario would break my implementation?
    Well, as you found out, with the implementation you have, you need the 0-arg constructor. There's also the chance that it's a bit slower. (Depends on the inlining and dead store elimination behavior of your compiler, really.) Also, if you ever have a const member, there is no other way to initialize it.

    In the end it's simply good, idiomatic C++ to use the constructor initializers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You need to expicitly write the default constructor if you:
    A. Use it (intentionally or not), and
    B. The class has other constructors which suppresses creation of the default contructor.

    Definitely use constructor initialisation lists where you can.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by iMalc View Post
    You need to expicitly write the default constructor if you:
    A. Use it (intentionally or not), and
    B. The class has other constructors which suppresses creation of the default contructor.
    B2. or the compiler-generated default constructor doesn't do what you want (e.g. value-initialize built-in types - the compiler-generated default constructor leaves them uninitialized).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,677
    You either need to specify a default location in the declaration of the constructor, or to declare (and define) a constructor with no arguments.

    There are some exceptions, generation of the no-argument constructor by the compiler is suppressed if you supply another constructor.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 10:00 AM
  2. Struct delcaring a struct within itself question
    By illidari in forum C Programming
    Replies: 3
    Last Post: 12-03-2010, 03:01 PM
  3. Struct question
    By Death_Wraith in forum C++ Programming
    Replies: 21
    Last Post: 06-03-2004, 06:22 PM
  4. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 02:34 AM
  5. struct question...
    By Cheeze-It in forum C Programming
    Replies: 3
    Last Post: 11-07-2001, 06:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21