debug error : max must have union class struct types
this is m开发者_Go百科y code:
#include <iostream>
using namespace std;
class Sp
{
private :
int a;
int b;
public:
Sp(int x = 0,int y = 0) : a(x), b(y) { };
int max(int x,int y);
};
int Sp::max(int a,int b) { return (a > b ? a : b); };
int main()
{
int q,q1;
cin >> q >>q1;
Sp *mm = new Sp(q,q1);
cout << mm.max(q,q1);
return 0;
}
Instead of: mm.max(q,q1);
you need to use: mm->max(q,q1);
mm is a pointer and needs to be addressed as such.
Alternately, you could just say:
Sp mm(q,q1);
cout<< mm.max(q,q1);
and avoid pointers all together.
mm
is a pointer to an Sp
; you need to use ->
, not .
:
cout << mm->max(q,q1);
However, it makes no sense to create the Sp
object dynamically anyway; why not just:
Sp mm(q, q1);
cout << mm.max(q, q1);
As it is written now using dynamic allocation, you don't destroy the object that you created by using delete
, which is an all-to-common programming error.
精彩评论