简体   繁体   中英

Erlang undef Error

I have the following Erlang code running on Eclipse Erlide:

%% @author gakuo
%% @doc @todo Add description to links.


-module(links).

%% ====================================================================
%% API functions
%% ====================================================================
-export([start/1, test/1,middle/2]).



%% ====================================================================
%% Internal functions
%% ====================================================================
start(N)->
    register(first,spawn_link(links,make,[N-1])).
make(0)->
    last();
make(N)->
    middle(spawn_link(links,make,[N-1]),N).
middle(Next,N)->
    receive
        Msg->
            Next ! Msg,
            io:format("Process ~w received ~w~n",[N,Msg]),
            middle(Next,N)

    end.
last()->
    receive
        stop->
            io:format("last process now exiting ~n", []),
            exit(finished);
        Msg->
            io:format("last process received ~w ~n", [Msg]),
            last()
    end.
test(Msg)->
           first ! Msg.

I run it as follows:

links:start(3).

At this point, I get the following error:

  Eshell V9.0.4
(processlinking@GAKUO)1> links:start(3).
** exception exit: undef
     in function  links:make/1
        called as links:make(2)

This happens before I run the next command which is:

links:test(stop).

After the two commands, I was expecting the following:

Process 2 received stop
Process 1 received stop
Last process now exiting
stop

After some research, I found out that Undef is raised because the function cannot be found when evaluating a function call. Further research on StackOverflow for similar errors did not help. The solutions involved upgrading the installed Erlang version but my problem seems to be a logical one. Being new to Erlang, I cannot point out what it is. As usual, your help will be highly appreciated.

spawn_link (and other functions that accept a module name + function name + arguments list) can only call functions that that module exports. Functions that are not exported are only callable from within the module and need to be called without the module name prefix. So, you need to export make/1 to make this code work:

-export([start/1, test/1, middle/2, make/1]).
                                    ^^^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM