void myfun(CPoint *pts)
{
  pts = new CPoint[2];
  pts[0].x = 1;
  pts[0].y = 1;
  pts[1].x = 2;
  pts[1].y = 2;
}CPoint *A = NULL;
myfun(A);
怎么A 没有值?急,请知道的说一下,谢谢

解决方案 »

  1.   

    CPoint *A = myfun();
    void myfun()
    {
    CPoint *pts = new CPoint[2];
    pts[0].x = 1;
    pts[0].y = 1;
    pts[1].x = 2;
    pts[1].y = 2;
    return pts;
    }
      

  2.   

    1、你在什么地方看的?
    2、看看A[0],A[1]的值;
    3、对结构和类对象尽量用引用(&)来返回。
      

  3.   

    兄弟,代码写错了。
    void myfun(CPoint *pts)
    这里是把指针所指的内容传进去了。
    但指针本身没有传进去啊。
      

  4.   

    void myfun(CPoint **pts)
    {
    *pts = new CPoint[2];
      CPoint *p = pts;
      p[0].x = 1;
      p[0].y = 1;
      p[1].x = 2;
      p[1].y = 2;
    }这才是正确的。
      

  5.   

    to rommi() 
    我想返回一个int型,表示数组的个数?抄到这上面失误。
    int myfun(CPoint *pts)
    {
      pts = new CPoint[2];
      pts[0].x = 1;
      pts[0].y = 1;
      pts[1].x = 2;
      pts[1].y = 2;
      return 2;
    }CPoint *A = NULL;
    int icnt = myfun(A);//这里举例是写了两个点,实际中是很多的
    CRgn rgn;
    rgn.CreatePolygonRgn(A,icnt,ALTERNATE);这时看A中是空的。to: syy64(太平洋)
    我是跟踪,执行完方法到
    rgn.CreatePolygonRgn(A,icnt,ALTERNATE);这时看A中是空的。
      

  6.   

    to: fancyxing(凡鱼)
    我试试
      

  7.   

    int myfun(CPoint& *pts)
    加个引用
      

  8.   

    to: fancyxing(凡鱼)
    我应该怎么调用 呢
      

  9.   

    int myfun(CPoint **pts)
    {
    *pts = new CPoint[2];
    CPoint *p = pts;
    p[0].x = 1;
    p[0].y = 1;
    p[1].x = 2;
    p[1].y = 2;
    return 2;
    }CPoint *A = NULL;
    int i = myfun(A);
      

  10.   

    直接调用呀,就如myfun(&A);
      

  11.   

    void myfun(CPoint **pts)
    {
    *pts = new CPoint[2];
      CPoint *p = *pts;
      p[0].x = 1;
      p[0].y = 1;
      p[1].x = 2;
      p[1].y = 2;
    }CPoint *A = NULL;
    myfun(&A);
      

  12.   

    to gofqjyie(誓将天下了然于胸)
    可以了.谢谢
      

  13.   

    估计问题出在这里*pts = new CPoint[2];
    这个语句是不是应该在函数的外面,由调用这个函数的函数来执行呢
      

  14.   

    void  myfun(CPoint  *&pts)  //多个引用
    {  
       pts  =  new  CPoint[2];  
       pts[0].x  =  1;  
       pts[0].y  =  1;  
       pts[1].x  =  2;  
       pts[1].y  =  2;  
    }  
     
    CPoint  *A  =  NULL;  
    myfun(A);
      

  15.   

    typedef CArray <CPoint, CPoint> CPointArray;int myfun(CPointArray& pts)
    {
      pts.SetSize(2);
      pts[0].x = 1;
      pts[0].y = 1;
      pts[1].x = 2;
      pts[1].y = 2;
      return 2;
    }CPointArray A;
    int icnt = myfun(A);//这里举例是写了两个点,实际中是很多的
    CRgn rgn;
    rgn.CreatePolygonRgn(A[0],icnt,ALTERNATE);这时看A中是空的。
      

  16.   

    就像fancyxing(凡鱼)所说的,
    任何的参数的这种传递都是传一个副本进去。所以我们要改变什么值就要把它的地址传进去。
      

  17.   

    方法1:参数传递改为引用传参(&)
    方法2:参数传递改为多加一个地址传参(**)
      

  18.   

    这是C++常见问题之一,因为CPoint *pts = new CPoint[2]属于函数局部的对象,在函数执行完毕之后会自动释放,应该先new一个对象,然后再把指针传进去,而不是在函数内部new这个对象