error: calling a private constructor of class

this is code from a youtube tutorial, i'v seen it compile properly in his video. i tried asking the author via comments but yet to get a reply. hence seeking advice here as well.

I'v tried making the constructors public by adding
Public:
but apparently the syntax is wrong.

Brushing up my fundamentals now, before trying leetcode challenges again...

will appreciate help figuring out how to improve


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

class Point
{
	double x;
	double y;
	
	// Public:
	
	Point() { x = 0 ; y = 0;  }			// Default Constructor
									
							
	
	Point(double X, double Y) { x = X; y = Y; }
										// Parameterized Constructor
	
	Point(const Point& rhs) { x = rhs.x; y = rhs.y; }
										// Copy Constructor
};				

int main()
{
	Point p1;
	
	Point p2(1.3, 3.4);
	
	Point p3 = p1;
	
	return 0;
}
Currently all methods and data members of your class are default declared private. Every one of your ctors are inaccessible outside the class itself. Lines 23, 25 & 27 will error out.

And it is public: (lower case), NOT Public:.

Mistyped case is a very core "gotcha" factor in C/C++.

Learn C++ might be more of a help to cram C++ into your cranium than youtube videos.

https://www.learncpp.com/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

class Point
{
	double x = 0.0 ; // default member initialiser
	double y = 0.0 ; // default member initialiser

	public: // note: case-sensitive!

        Point() noexcept = default ; // explicitly defaulted default constructor
                                     // default member initialisers are used to initialise members
        
        Point( double xx, double yy ) noexcept : x(xx), y(yy) {} // constructor
                                     // members are explicitly initialised by the constructor

        // copy constructor etc. are implicitly declared
        // the implementation would provide these

    // for testing
    friend std::ostream& operator<< ( std::ostream& stm, Point pt )
    { return stm << '{' << pt.x << ',' << pt.y << '}' ; }
};

int main()
{
	Point p1; // default initialised
	std::cout << p1 << '\n' ; // {0,0}

	Point p2( 1.3, 3.4 ); // {1.3,3.4}
	std::cout << p2 << '\n' ;

	Point p3 = p1; // copy initialise
	std::cout << p3 << '\n' ; // {0,0}
}

http://coliru.stacked-crooked.com/a/bfe722cd06d32d8a
thanks, something else I wanted to ask

how did the youtube author manage to compile his code without using the
public: keyword?

my guess is that he's using a older version of the compiler?
the tutorial was posted in August 2017
> how did the youtube author manage to compile his code

The author could not have managed to compile the code as in the original post without errors
(with a reasonably conforming C++ compiler).
Maybe the code that was actually compiled was different from the one that was posted.
my guess is that he's using a older version of the compiler?
These access specifier where part of the language since the beginning. So either he used a very bad non standard compiler or he compiled another code.
I suspect that class was originally struct and that at some point it got changed without other required changes. struct and class are very similar except that struct all are public by default with class all are private by default.
Registered users can post here. Sign in or register to post.