24小时热门版块排行榜    

查看: 479  |  回复: 2

lixy1217

木虫 (著名写手)

[求助] 关于C++析构函数出现的问题

这是代码,用于计算两个向量的加法。#include "iostream.h"

#include "iostream.h"

class vector
{
public:
        int n;
        double*v;
        vector(int a=0)
        {
          if(a>0)
          {  v=new double[a];  n=a;  }
          else{  n=0;  v=NULL;  }
        }
        friend vector operator+(vector&a,vector&b)
        {
                int i;
                vector c;
                if(a.n!=b.n)  {cerr<<"Error!!!"<                 c.n=a.n;
                c.v=new double [a.n];
        for(i=0;i                         c.v[ i ]=a.v[ i ]+b.v[ i ];
                return c;
        }
        void operator=(vector &c)
        {
          int i;
          cout<<"???"<           n=c.n;  v=new double[n];
          for(i=0;i           {  v[ i ]=c.v[ i ];  }
        }
        ~vector()
        {
          cout<<"n="<           if(n>0)  delete[]v;
          v=NULL;
          n=0;
        }
};

void main()
{
  vector f1(2),f2(2),f3;
  f1.v[0]=1.0;
  f1.v[1]=1.0;
  f2.v[0]=2.0;
  f2.v[1]=2.0;
  f3=f1+f2;
  cout<<"f3.n="<   cout< }
结果输出为
n=2
???
n=2
f3.n=2
-1.45682e+144
n=2
同时出现了指针错误。
与网上一些关于矩阵向量的程序包对比后,没有发现什么明显错误。很明显出自于对析构函数的应用,可是却不知问题具体在哪里,如何修改。

目前的办法只能在类中增加一个判定型变量,然后以此确定对象是否执行析构,但这样显然不符合C++的初衷。

[ 来自科研家族 皇家数理科学协会 ]

[ Last edited by lixy1217 on 2012-12-6 at 22:06 ]
回复此楼
偶尔敞开心扉,世界将不再孤独
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖

★ ★ ★ ★ ★
感谢参与,应助指数 +1
lixy1217: 金币+5, ★★★很有帮助, 我用的是VC++6.0,难道又是因为版本问题?而且在库函数\"iostream\"中不能重载输出流<< 2012-12-07 09:24:22
什么编译器,vs2008没问题
CODE:
#include
using namespace std;
class vector
{
public:
        int n;
        double*v;

        vector(int a=0)
        {
          if(a>0)
          {  v=new double[a];  n=a;  }
          else{  n=0;  v=NULL;  }
        }

        friend vector operator+(vector&a,vector&b)
        {
                int i;
                vector c;
                if(a.n!=b.n)  {cerr<<"Error!!!"<                 c.n = a.n;
                c.v = new double[c.n];
                for(i=0;i                         c.v[ i ]=a.v[ i ]+b.v[ i ];
                return c;
        }
        void operator=(vector &c)
        {
          int i;
          cout<<"="<           n=c.n;
          v = new double[c.n];
          for(i=0;i           {  v[ i ]=c.v[ i ];  }
        }
        ~vector()
        {
          cout<<"n="<           if(n>0)  delete[]v;
          v=NULL;
          n=0;
        }
};

void main()
{
  vector f1(2),f2(2),f3;
  f1.v[0]=1.0;
  f1.v[1]=1.0;
  f2.v[0]=2.0;
  f2.v[1]=2.0;
  f3=f1+f2;
  cout<<"f3.n="<   cout< }

结果
CODE:
n=2
???
n=2
f3.n=2
3
n=2
n=2
n=2

Process returned 0 (0x0)   execution time : 0.043 s
Press any key to continue.

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2012-12-06 22:46:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wangww2011

木虫 (著名写手)

引用回帖:
2楼: Originally posted by libralibra at 2012-12-06 22:46:18
什么编译器,vs2008没问题
#include <iostream>
using namespace std;
class vector
{
public:
        int n;
        double*v;

        vector(int a=0)
        {
          if(a>0)
     ...

没有问题?gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) 结果如下
CODE:
test.cpp:45:11: error: ‘::main’ must return ‘int’
test.cpp: In function ‘int main()’:
test.cpp:52:9: error: no match for ‘operator=’ in ‘f3 = operator+(vector&, vector&)((* & f2))’
test.cpp:52:9: note: candidate is:
test.cpp:27:14: note: void vector::operator=(vector&)
test.cpp:27:14: note:   no known conversion for argument 1 from ‘vector’ to ‘vector&’

To楼主,人生苦短,尽可能的用python吧
3楼2012-12-07 00:40:57
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 lixy1217 的主题更新
信息提示
请填处理意见