no appropriate constructor available

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <math.h>

using namespace std;


enum tipTrokut {JK,JS,RS}; 

class Tocka
{
public:
float x;
float y;

	Tocka(float a, float b);
	Tocka(Tocka &A);
};

Tocka::Tocka (float a, float b){ 
	x=a; 
	y=b; 
}

Tocka::Tocka (Tocka &A){ 
	x= A.x; 
	y= A.y; 
}



class Trokut
{
private:
Tocka A;
Tocka B;
Tocka C;
public:
Trokut (const Tocka &E,const Tocka &D,const Tocka &F); //here compiler shows error
Trokut(const Trokut &T);
float opseg();
float povrsina();
};

Trokut::Trokut(const Tocka &E,const Tocka &D,const Tocka &F){ //here compiler shows error
	A.x= E.x;  
	A.y= E.y; 
	B.x= D.x;
	B.y= D.y;
	C.x= F.x;
	C.y= F.y;
}


Trokut::Trokut (const Trokut &T){ 
	A.x=T.A.x; 
	A.y=T.A.y; 
	B.x=T.B.x;
	B.y=T.B.y;
	C.x=T.C.y;
	C.y=T.C.y;
}

float Trokut::povrsina (){
	return (float)0.5*(A.x *(B.y-C.y)+B.x*(C.y-A.y)+C.x*(A.y-B.y)); 
}

float Trokut::opseg (){
	float kv=2, kor=0.5;
	return  pow(pow(A.x-B.x,kv)+pow(A.y-B.y,kv),kor) + pow(pow(C.x-B.x,kv)+pow(C.y-B.y,kv),kor) + pow(pow(A.x-C.x,kv)+pow(A.y-C.y,kv),kor);

}


int main (){
	int N;
	Tocka A(5,6);
	Tocka B(A);
	Tocka C(7,8), E(10,9);
	Trokut mojtrokut(A,C,E);
	Trokut mojtrokut2 (mojtrokut);
	cout<<"tocka B je "<<B.x<<" "<<B.y<<endl; 
	cout<<"povrisina trokuta je "<<mojtrokut.povrsina()<<", a opseg je "<<mojtrokut.opseg();
	cin>>N; 
}

my compiler shows me an error on places where i have put the comment
please help
did i do declaration wrong?
(btw tocka=point on english and trokut = triangle)
Tocka has no default constructor. Every constructor calls the default constructors of its members, unless told otherwise. To explicitly state what constructor to call, see http://www.cprogramming.com/tutorial/initialization-lists-c++.html
It is not clear why your compiler generates an error for the statement

Trokut (const Tocka &E,const Tocka &D,const Tocka &F); //here compiler shows error

It is not important for this statement that your class Tocka has no default constructor.

So to understand what is the reason of the error message in this statement, please show the full error message.
Last edited on
------ Build started: Project: class practice, Configuration: Debug Win32 ------
Compiling...
main file.cpp
c:\users\silvio badak\desktop\my project\class practice\class practice\main file.cpp(38) : error C2512: 'Tocka' : no appropriate default constructor available
c:\users\silvio badak\desktop\my project\class practice\class practice\main file.cpp(38) : error C2512: 'Tocka' : no appropriate default constructor available
c:\users\silvio badak\desktop\my project\class practice\class practice\main file.cpp(38) : error C2512: 'Tocka' : no appropriate default constructor available
c:\users\silvio badak\desktop\my project\class practice\class practice\main file.cpp(44) : error C2512: 'Tocka' : no appropriate default constructor available
c:\users\silvio badak\desktop\my project\class practice\class practice\main file.cpp(44) : error C2512: 'Tocka' : no appropriate default constructor available
c:\users\silvio badak\desktop\my project\class practice\class practice\main file.cpp(44) : error C2512: 'Tocka' : no appropriate default constructor available
E zemljače, trebas defaultni konstruktor:D
ubaci ga u klasu Tocka:

Tocka() {};

Ovako:

1
2
3
4
5
6
7
8
9
class Tocka
{
public:
float x;
float y;
        Tocka() {};
	Tocka(float a, float b);
	Tocka(Tocka &A);
};


I trebalo bi radit hehe :)
I consider this as a bug of the compiler. The declaration

Trokut (const Tocka &E,const Tocka &D,const Tocka &F);

does not require any default constructor, because parameters of the constructor are declared as references

I think you should change the compiler.


codekiddy (422) Mar 11, 2012 at 9:52pm
E zemljače, trebas defaultni konstruktor:D
ubaci ga u klasu Tocka:



You are wrong! The declaration

Trokut (const Tocka &E,const Tocka &D,const Tocka &F);

which the compiler marks as invalid does not require any default constructor. It is a bug of the compiler.
GCC gives the same error:

 In constructor 'Trokut::Trokut(const Tocka&, const Tocka&, const Tocka&)':
44:60: error: no matching function for call to 'Tocka::Tocka()'
44:60: note: candidates are:
24:1: note: Tocka::Tocka(Tocka&)
24:1: note:   candidate expects 1 argument, 0 provided
19:1: note: Tocka::Tocka(float, float)
19:1: note:   candidate expects 2 arguments, 0 provided
44:60: error: no matching function for call to 'Tocka::Tocka()'

44:60: note: candidates are:
24:1: note: Tocka::Tocka(Tocka&)
24:1: note:   candidate expects 1 argument, 0 provided
19:1: note: Tocka::Tocka(float, float)
19:1: note:   candidate expects 2 arguments, 0 provided
44:60: error: no matching function for call to 'Tocka::Tocka()'
44:60: note: candidates are:
24:1: note: Tocka::Tocka(Tocka&)
24:1: note:   candidate expects 1 argument, 0 provided
19:1: note: Tocka::Tocka(float, float)
19:1: note:   candidate expects 2 arguments, 0 provided
 In copy constructor 'Trokut::Trokut(const Trokut&)':
54:32: error: no matching function for call to 'Tocka::Tocka()'
54:32: note: candidates are:
24:1: note: Tocka::Tocka(Tocka&)
24:1: note:   candidate expects 1 argument, 0 provided
19:1: note: Tocka::Tocka(float, float)

19:1: note:   candidate expects 2 arguments, 0 provided
54:32: error: no matching function for call to 'Tocka::Tocka()'
54:32: note: candidates are:
24:1: note: Tocka::Tocka(Tocka&)
24:1: note:   candidate expects 1 argument, 0 provided
19:1: note: Tocka::Tocka(float, float)
19:1: note:   candidate expects 2 arguments, 0 provided
54:32: error: no matching function for call to 'Tocka::Tocka()'
54:32: note: candidates are:
24:1: note: Tocka::Tocka(Tocka&)
24:1: note:   candidate expects 1 argument, 0 provided
19:1: note: Tocka::Tocka(float, float)

19:1: note:   candidate expects 2 arguments, 0 provided
Althought it may not matter for a 'point', providing a default constructor is not always desirable.
And still, you're better using the initialization list in this case Trokut (const Tocka &E,const Tocka &D,const Tocka &F);

Your "copy constructor" is incorrect. It should be Tocka(const Tocka &A); But the one provided by the compiler already does what you want.
vlad from moscow wrote:
I consider this as a bug of the compiler. The declaration
Trokut (const Tocka &E,const Tocka &D,const Tocka &F);
does not require any default constructor, because parameters of the constructor are declared as references
It's not about the parameters, but about the members.
1
2
3
4
class Trokut
{
private:
Tocka A,B,C; //¿how are these constructed? 
If you omit the initialization list, the best guess of the compiler is to use the default constructors for them. But there is no default constructor (and again, maybe it shouldn't be one)
ne555 (2596) Mar 11, 2012 at 10:10pm
Your "copy constructor" is incorrect. It should be Tocka(const Tocka &A) ; But the one provided by the compiler already does what you want.


You are wrong! His copy constructor Tocka(Tocka &A); is correct!. Please read the C++ Standard. Copy constructor can have any of the four declarations

Tocka(Tocka &A);
Tocka(const Tocka &A);
Tocka( volatileTocka &A);
Tocka(const volatile Tocka &A);


And even all four declarations may be present in class definition simultaneosly.


ne555 (2596) Mar 11, 2012 at 10:10pm
It's not about the parameters, but about the members.

class Trokut
{
private:
Tocka A,B,C; //¿how are these constructed?


If you omit the initialization list, the best guess of the compiler is to use the default constructors for them. But there is no default constructor (and again, maybe it shouldn't be one)


You are again wrong! I am repeating the second time that the declaration Trokut (const Tocka &E,const Tocka &D,const Tocka &F);
is valid and does not contain any compile error. It does not require any default constructor.

To understand this try this simple example

1
2
3
4
5
6
7
8
9
10
11
12
struct C1
{
	C1( int x ) {}
	C1( C1 & ) {}
};

struct C2
{
	C1 a;
	C1 b;
	C2( const C1 &, const C1 & );
};


It should be compiled without any error.

It is the definition of the consttructor that is invalid.

Trokut::Trokut(const Tocka &E,const Tocka &D,const Tocka &F){ //here compiler shows error
A.x= E.x;
A.y= E.y;
B.x= D.x;
B.y= D.y;
C.x= F.x;
C.y= F.y;
}


and the compiler only here in definition shall show errors.

Do you see the difference between the correct declaration and the incorrect definition shown above?

Moreover with some changes the definition also can be made correct without any default constructor. For example

1
2
3
Trokut::Trokut(const Tocka &E,const Tocka &D,const Tocka &F) 
   : A( E.x, E.y ), B( D.x, D.y ), C( F.x, F.y )
{}


Take into account that the declaration was not changed. If it contains error then the compiler shall not compile the code because we did not change the declaration.

So it is a severe bug of the compiler. It misleads users and shows incorrect point in the code there are no errors instead of to show the point there indeed an error exists.
Last edited on
E zemljače, trebas defaultni konstruktor:D
ubaci ga u klasu Tocka:

Tocka() {};

Ovako:

1
2
3
4
5
6
7
8
9
class Tocka
{
public:
float x;
float y;
Tocka() {};
Tocka(float a, float b);
Tocka(Tocka &A);
};


I trebalo bi radit hehe :)

bas sam to uradio nakon sto sam postao
:D

i inserted Tocka(){}; constructor and now is fine...
anyway i think too that it is bug on compiler :|
I think that you understand nothing what I am writing. Look at the constructor I have showed above

1
2
3
Trokut::Trokut(const Tocka &E,const Tocka &D,const Tocka &F) 
   : A( E.x, E.y ), B( D.x, D.y ), C( F.x, F.y )
{}


Does it require any default constructor?! And the second question to you Have I changed the original declaration?!

No, I have not changed the original declaration. So the declaration of the constructor is correct! (How many times I should repeat this to you?!) It is the definition of the constructor that is incorrect. So the compiler shall show errors for that code that is indeed incorrect. But it points to fully correct code and says that it contains errors. It iis a severe bug of the compiler.
Last edited on
lol thanks vlad
i haven't read urs since it works with Tocka(){};
thanks again for ur explanation, i have read it all now and I got it
It is the definition of the constructor that is incorrect. So the compiler shall show errors for that code that is indeed incorrect. But it points to fully correct code and says that it contains errors. It iis a severe bug of the compiler.


It isn't a bug. The 'code' that is 'incorrect' is code that is not there. There is nothing wrong with the code in the constructor's body. If you supply the intializer list as required in this case, then use the same code in the body of the constructor, there is no error.


What you arre saying?!!!
There is nothing wrong with the code in the constructor's body.
What you are saying is a foolishness. You only may do such statements only "if you supply the initializer" But you did not supply the initializer. So the body of the constructor is incorrect.
If anything, the body of the constructor was irrelevant.
Topic archived. No new replies allowed.