This has been asked before by others....

But it seems I can't apply the solutions given to them for some odd reason.

My issue is the age old VS2015 Error C2512: "No appropriate default constructor available."

I am using supplied library files for FLTK etc. while going through Stroustrup's PPP book as many here before have done. Never gets old right?

Anyhow, I am attempting to define a Structure/Class for creating a portion of an ellipse as an arc and naturally I am following the structure Bjarne gives by deriving from what I THINK is a fully defined struct called Ellipse that he provides.

He provides a constructor that takes parameters and I THINK I am invoking this correctly - but perhaps I am not. (Afterall, I'm following the book right?) however the compiler tells me that no default constructor is found so I think I'm obviously filling the member variables incorrectly??? Not sure.

Anyhow, here is the DECLARATION of my struct Arc below and the constructor of THIS particular structure. It is the constructor for Arc that is vomiting on the error you see above==>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  struct Arc : Ellipse	//Arc inherits from ellipse since we're drawing elliptic arc sections
{
	using Ellipse::Ellipse;					//using Ellipse's constructors

	Arc(Point p, int ww, int hh, int Dgree);  //declaration of constructor for arc
	void draw_lines() const;  //Arc gets its own version of draw_lines()?? needed?
	int get_deg() const;	//function to return the degree measure of the arc object

private:

	int deg{ 0 };	//degree of the arc section to draw

};	//end of Arc declarations


Arc::Arc(Point p, int ww, int hh, int degree) : deg { degree }
{  //<== THIS IS WHERE THE ERROR FROM THE COMPILER STARTS

	add(Point{ p.x - ww,p.y - hh });

}// end of Arc constructor definition 



Am I not using an initialization list correctly to give the underlying Ellipse members their assigned values?

I attempted to make a simple default Ellipse constructor but the compiler was not having any of that either.

Ideas?

Xanadu
Last edited on
Idea 1:

Upgrade Visual Studio.

I posted your code into Visual Studio 2019 CE, and with a few obvious additions (I have not Point class here, add means nothing from the code you've posted)...it compiled.

That assumes the add function is something like

void add( const Point & p );

...and Point is something akin to

1
2
3
4
5
struct Point
{
 int x;
 int y;
};


This compiled in MSVC and Clang 8.0 (which Microsoft's installer set up with merely a click of the options, so I have two compilers connected to VS and they work interchangeably).

So, I must assume that VS2015's compliance with C++14 is limited.

...but I didn't ask, are you sure you have set the compiler's language compliance to C++14 or higher?


Also, I'm probably set for C++17 by default here.

Though I still have VS2017, I removed VS2015 a while back, so I've not tried it.
Last edited on
> He provides a constructor that takes parameters and I THINK I am invoking
> this correctly
nowhere in your code you invoke Ellipse's constructor

> no default constructor is found so I think I'm obviously filling the member
> variables incorrectly
you are trying to use the default constructor for ellipse
no such constructor exists

> Am I not using an initialization list correctly to give the underlying
> Ellipse members their assigned values?
: deg { degree }
that's your initialization list
there you initialize `deg'
`deg' is a member variable of `Arc'

¿where do you think you are doing anything to the members of ellipse?
you should have something like Ellipse(42, 3.14, "hello world")

> I attempted to make a simple default Ellipse constructor but the compiler was
> not having any of that either.
if you want to discuss that attempt, then show that attempt.


> This has been asked before by others....
chose a better title next time
for example: «No appropriate default constructor available.»
Last edited on
What version of FLTK are you using? Stroustrup uses 1.1.10. That version is years outdated.

The current stable version is 1.3.5. It does everything 1.1.10 does, with bug fixes and enhancements.

You can download the latest stable version here:

https://www.fltk.org/software.php
Niccolo,

You have good points about the compiler and to answer you outright: No, I don't know if I have the compiler operating from a C++14 point of view strictly and I can try to investigate this unless you know exactly where to go to make these changes? I'll have to take a look at it tonight of course. Also, I need to upgrade my compiler without a doubt! Maybe I should go back to CodeBlocks? LOL! I think I will stick with the VS for now - but I DO need to check on the "strictness" of the compiler's settings. Also, perhaps I should obtain the Clang 8.0 compiler for VS2015 (available? Compatible?) so I might have some questions about that for you in the near future perhaps.

You are correct, the "add()" function is in fact inherited from the "Shape" definition in the libraries you helped with before and I believe are invoked because I am including those headers and .cpp files that have the definitions of the various other base classes that Stroustrup defined for the book.

ne555....... are you implying that if I inherit from a base class that I need to explicitly invoke that base class's constructors? Can you give an example to detail your thinking on this?? WHERE and HOW do you accomplish what you are implying? The declaration of "Arc" the definition of "Arc"?? Inside the constructor for "Arc"?

The author does not appear to explicitly invoke these constructors, however I could be wrong and this is why I am asking.

No, I DON'T think I am doing anything to the members of Ellipse, hence my question==>
Am I not using an initialization list correctly to give the underlying Ellipse members their assigned values?


So you suggest this==>
you should have something like Ellipse(42, 3.14, "hello world")
Where does this go? In what context are you trying to say this works? Can you be a little more specific?

Xanadu

Last edited on
Am I not using an initialization list correctly to give the underlying Ellipse members their assigned values?

No. In fact, you are not using the initialisation list at all to give the underlying Ellipse members any values.

The only thing you are using the initialisation list for, is to set Arc's own members, not the members of Ellipse.

Ganado's link takes you to a SO answer that shows you exactly how to do this. Ignoring it was a poor decision.
Last edited on
Thank you all.

Mikeyboy and Ganado - thanks for the very pointed and direct info. I will start reading and understanding. I will go to the link and see how to implement a superclass's initialization of that class's members in a sub-class's constructor? Does that sound right?

Sadly the book does NOT show this explicitly for some reason or perhaps I missed it. Either way this might be by design from the author.....

Furry guy, I am using the book's version of FLTK (1.1.10) since I had never used a GUI library before. I figured the information in the book (regarding install, setup, what have you..) would be closer to being accurate if I stuck with the same version rather than upgrades that perhaps changed the "way" things were to be done significantly. From a noob perspective this seemed the safest and so far has proved a good choice. So far FLTK, in and of itself, has NOT been the source of any problems I've had. Most are from my own ignorance or from Strousstrup's provided libraries.

Xanadu

So everyone,

So my sub-class should look like the following IF I were writing this to invoke Ellipse's constructor to fill Ellipse's members from the subclass constructor of Arc ?? ==>

[Keep in mind Ellipse has members of width (w) and height(h) and inherits the Point p from Shape (Shape = Ellipse's base class)]

1
2
3
4
5
6
7
Arc::Arc(Point p, int ww, int hh, int degree) : Ellipse(p), Ellipse(ww), Ellipse(hh), 
deg ( degree )  //changed all the curly braces to regular parentheses??  OK?  Not necessary?
{  
	add(Point{ p.x - ww,p.y - hh });  //is this superfluous if Ellipse already inherits add() 
//from Shape or does Arc need its own version of this because it changes how add() works?  Why or why not?

}// end of Arc constructor definition  


How is this attempt? The odd thing from the book is I don't see Stroustrup do this except for the SUBCLASS'S member(s) - but he doesn't set the base class members explicitly and sort of leaves the student thinking: "How were those assigned? Did the compiler just auto-magically know they were members of the base class and handled it??" ...... very confusing from a noob perspective.

Xanadu
I'll pull my copy of his book out...where are you in that book?

I need to upgrade my compiler without a doubt! Maybe I should go back to CodeBlocks? LOL!


Oh, h**l no. Just grab the free VS2019.

As to where the C++ level is, look in the project settings, under C++, in language, look for the dropdown and choose at least C++14.
1
2
3
4
5
6
7
Arc::Arc(Point p, int ww, int hh, int degree) : Ellipse(p), Ellipse(ww), Ellipse(hh), 
deg ( degree )  //changed all the curly braces to regular parentheses??  OK?  Not necessary?
{  
	add(Point{ p.x - ww,p.y - hh });  //is this superfluous if Ellipse already inherits add() 
//from Shape or does Arc need its own version of this because it changes how add() works?  Why or why not?

}// end of Arc constructor definition  


1) No. If you want to invoke a constructor that takes multiple arguments, it's:

Arc::Arc(Point p, int ww, int hh, int degree) : Ellipse(p, ww, hh)

(EDIT: Fixed the bold tag)

Seriously, if your textbook doesn't tell you this, then you need to seriously consider getting a better textbook.

2) In this case, it doesn't matter. Using () is not always the same as {}. If you're interested, I'd advise reading up on the various forms of initialisation in modern C++, and how they differ. Warning: it can make your head spin a bit if it's new to you - new standards have added new ways of initialising things.

3) Regarding the call to add() in the constructor, you haven't shown us what the Ellipse constructor does, nor what the add function does, so it's hard to offer any meaningful advice. I will ask this, though: should Arc really inherit from Ellipse? Is an arc really a type of ellipse?
Last edited on
Niccolo,

I'm Chapter 13, Ex #1. And looking at how he (Stroustrup) constructed Circle and Ellipse structures to do what they do in order to use the FLTK library.

MikeyBoy........ I don't understand the
[b]
above.... what is the
[b]
doing in this context or what is its significance and is this C++14 that I should know? Can you please explain that small tidbit for me? Were you just trying to BOLD that section and forgot the end?

So initializing the members separately will cause the compiler to reject this because Ellipse's constructor takes all of these parameters together at once so that is how it must literally see the arguments presented??

Also, the book I'm using is from Stroustrup.... it is very good but he PURPOSELY introduces topics and ideas that he admittedly and openly hasn't explained yet or formally introduced the students to. I think he is obviously going to get to what you are saying but for some reason he thinks that introducing these topics a little out of sequence helps to have the student start thinking about "why" C++ needs to be programmed as it is?? This is at least my take on it.

It will definitely NOT be the last resource I start to use and read about regarding the language though!

OK. Thank you for the braces head's up - I have seen this topic in its mind numbing forms come up many times and that is why I decided to ask. I have seen the very large difference that the braces and parentheses make at times and wasn't sure if this was one of those.

To answer your final question..... the problem I am solving states that we want a new class/struct that draws arc sections of Ellipses ..... when I read this I naturally came to the conclusion that since I have an Ellipse class and I want to draw arc sections of Ellipses - that using Ellipse as a base class would be a smart move. But I am not a seasoned pro and you and many others could probably give many good reasons for making this its own class entirely or inherit from Shape directly rather than through Ellipse instead.

Xanadu
Were you just trying to BOLD that section and forgot the end?

You guessed it! My apologies for the mistake - I've fixed it now.

So initializing the members separately will cause the compiler to reject this because Ellipse's constructor takes all of these parameters together at once so that is how it must literally see the arguments presented??

What you should understand is that you are not "initializing members" - you are simply invoking the constructor of Ellipse. When you create an object of type Arc you also creating an object of type Ellipse, because an Arc is an Ellipse. So a constructor of Ellipse will execute, as part of the process of creating an Arc.

If you don't explicitly invoke a constructor of Ellipse, then the default constructor will be invoked. But in your code, Ellipse has no default constructor - hence the error you were originally getting.

When you wrote Ellipse(p), what that actually means is that you were attempting to invoke a constructor that took a single argument of type Point. Does such a constructor exist? I don't know - unless I've missed it, you haven't shown us that class. But it would be nonsense to try and invoke three different constructors for the same object.

From context, I'm assuming Ellipse does have a constructor that takes those three arguments, and so I wrote code that would invoke that constructor. The point is, you have to specify a constructor that actually exists!
Mikey,

Great! Since that was a BOLD attempt I am TOTALLY tracking with you now! Thank you. Just wanted to be sure I wasn't seeing some C++19 standard or something! LOL.

OK. That makes sense. No there is NO constructor in Ellipse that takes only one argument so I see why you are saying that it isn't going to work. Ellipse's constructor looks like this ==>

 
Ellipse::Ellipse (Point p, int width, int height);


So you were correct at inferring the proper constructor for Ellipse!!

So as you have suggested, the compiler would definitely NOT like me sending Ellipse(p), Ellipse(w) or Ellipse(h) [as I wrote above - this is a "duh" moment now] since none of those constructors even exist. (Nor does Ellipse() as you all were telling me.)

So thank you sir! That explains why the compiler was saying my code was just ridiculous!

Lesson learned and won't be making that mistake again. I must admit, sort of surprised Stroustrup would NOT emphasize this very important detail for us uneducated types so that we wouldn't naturally stumble into it.

Xanadu

@Niccolo,

With VS 2019 (and VS 2017) the C++ language standard defaults to C++14. Of course someone can deliberately set it to C++14 or later if they want.

https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019#remarks
I have those two, and it does.

The OP is using VS2015. I don't have that one installed, and don't recall its defaults.

Some earlier version of the OP's posted code, which generated errors on VS2015, compiled without issue for me on VS2019.
Last edited on
VS 2015 language standard defaults to /std::c++14 only with update 3.
https://social.msdn.microsoft.com/Forums/en-US/7d50300f-4b6c-4740-9735-2045ba5e22f8/what-is-the-default-c-standard-used-by-the-visual-studio-15-and-visual-studio-17

I remember C++14 support with VS 2015 was lacking in several areas even after the update. When VS 2017 was released I uninstalled 2015 for 2017.

Currently I keep 2017 and 2019 both installed. Both are 100% compliant with C++17.
@Xanadu4ever

Yeah, I'd be very surprised if he didn't cover that. Still, I'm glad it's worked out and you could get your code working.
Last edited on
Yes thank you all for the help! Problem Solved.

The solution was correct (as you all knew it would be) and everything is in order.
I would also be surprised if Stroustrup left a discussion about initialization lists out and referring back to a base class.

I looked at the book and I believe in a couple chapters he addresses this more as well as pointers, type inference (with "auto), templates and many other topics that he seems to be hinting at in these earlier chapters. I really don't appreciate that particular style of teaching but so far I am working with it and obviously I come here to ask you all to fill in the blanks that he conveniently leaves out at times.

Since, there are some subtle points that Dr. Bjarne hasn't covered just yet..... do you guys recommend another book(s) besides Principles and Practices to continue the further in-depth study of these types of problems and take my learning to the next level up from this one?

I have Meyer's "modern C++" and I have Josoutis's "STL reference" (these both still need read BTW!!) but obviously I need a book to supplement my learning once I'm done with these. I have also been using learncpp.com as well and that is a great resource too!

Just queuing up the next text I need to peruse to be able to fully come up to C++ speed.

Thanks again people.

Xanadu
"Problem Solved."

Then please tick the box so the thread is green check-marked. :)
Topic archived. No new replies allowed.