开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜