无法通过包“Heap”找到对象方法“add”

时间:2010-08-29 20:35:40

标签: perl package

我不确定为什么perl没有认识到Heap的方法添加。获得问题标题中的消息。这是最相关的文件。

#!/usr/bin/perl -w

use strict;
use Util;
use Heap;
use HuffTree;

my $heap = Heap->new;
my $test = 3;
$heap->add($test); # <--------ERROR HERE-----------

package Heap;

use strict;
use warnings;

use POSIX ();

sub new {
  my $class = shift;
  my $self = { "aref" => [""],
           "next" => 1,
           @_};
  bless $self, $class;
}


sub print {
  my $self = shift;
  my $next = $self->{"next"};
  my $aref = $self->{"aref"};
  print "array => @$aref\n";
  print "next => $next\n";
}


sub compare {
  my ($self, $i, $j) = @_;
  my $x = $self->{"aref"}[$i];
  my $y = $self->{"aref"}[$j];

  if (!defined $x) {
    if (!defined $y) {
      return 0;
    } else {
      return -1;
    }
  }
  return 1 if !defined $y;
  return $x->priority <=> $y->priority;
}


sub swap {
  my ($self, $i, $j) = @_;
  my $aref = $self->{"aref"};
  ($aref->[$i], $aref->[$j]) = ($aref->[$j], $aref->[$i]);
}


sub add {
  my ($self, $value) = @_;
  my $i = $self->{"next"};
  $self->{"aref"}[$i] = $value;

  while ($i > 1) {
    my $parent = POSIX::floor($i/2);
    last if $self->compare($i, $parent) <= 0;
    $self->swap($i, $parent);
    $i = $parent;
  }
  $self->{"next"}++;
}


sub reheapify {
  my ($self, $i) = @_;
  my $left = 2 * $i;
  my $right = 2 * $i + 1;

  my $winleft = $self->compare($i, $left) >= 0;
  my $winright = $self->compare($i, $right) >= 0;
  return if $winleft and $winright;

  if ($self->compare ($left, $right) > 0) {
    $self->swap($i, $left);
    $self->reheapify($left);
  } else {
    $self->swap($i, $right);
    $self->reheapify($right);
  }
}


sub remove {
  my $self = shift;
  my $aref = $self->{"aref"};

  my $result = $aref->[1];
  $aref->[1] = pop @$aref;
  $self->{"next"}--;

  $self->reheapify(1);
  return $result;
}


sub empty {
  my $self = shift;
  return $self->{"next"} == 1;
}

1;

package HuffTree;

    use warnings;
    use strict;

use Pair;
our @ISA = "Pair";


sub priority {
  my $self = shift;

  # lowest count highest priority
  return -$self->{frequency}; 
}


sub left {
  my $self = shift;
  return $self->{left};
}


sub right {
  my $self = shift;
  return $self->{right};
}

1;

package Pair;

use warnings;
use strict;

sub new {
  my $class = shift;
  my $self = { @_ };
  bless $self, $class;
}


sub letter {
  my $self = shift;
  return $self->{letter};
}


sub frequency {
  my $self = shift;
  return $self->{frequency};
}


sub priority {
  my $self = shift;
  return $self->{frequency};
}

1;

package Util;

use strict;
use warnings;

sub croak { die "$0: @_: $!\n"; }


sub load_arg_file {
  my $path_name = shift @ARGV;
  my $fh;
  open($fh, $path_name) || croak "File not found.\n";
  return $fh;
}

1;

1 个答案:

答案 0 :(得分:5)

您已从CPAN安装Heap.pm。这是加载的,而不是你自己的Heap.pm。来自CPAN的new中的Heap.pm子看起来像这样:

sub new {
    use Heap::Fibonacci;

    return &Heap::Fibonacci::new;
}

这实际上是所述模块中的一个错误,因为Heap::Fibonacci使用了 bless \$h, $class;new中的标准Heap内容 所以引用被祝福到add包中 确实没有名为Heap::Fibonacci的子{(Heap确实如此)。

要解决您的直接问题,您可以:

  • 确保您的模块在“其他”@INC之前被选中(例如,通过use lib修改Heap::Fibonacci;
  • 或不重新发明轮子并实际使用Heap)。

无论如何,报告此问题可能是个好主意 到Heap.pm模块作者 - 因为即使你没有 您自己的{{1}},您的代码仍然会失败并显示相同的消息。