在erlang中'function not exported'

why*_*why 4 erlang

这是我的代码,当我运行:gen_server_test:alloc,它给了我错误,请帮帮我

-module(gen_server_test).
-behaviour(gen_server).
-export([start_link/0]).
-export([alloc/0, free/1]).
-export([init/1, handle_call/3, handle_cast/2]).
start_link() ->
    gen_server:start_link({local, gen_server_test}, gen_server_test, [], []).

alloc() ->
    io:format("allocing"),
        gen_server:call(?MODULE, {alloc}).

free(Ch) ->
    gen_server:cast(gen_server_test, {free, Ch}).

init(_Args) ->
    {ok, channels()}.

handle_call({alloc}, _From, LoopData) ->
    io:format("handle alloc").

handle_cast({free, Ch}, LoopData) ->
    io:format(Ch).

free() -> 
        io:format("free").

channels() ->
    io:format("channels").
Run Code Online (Sandbox Code Playgroud)

错误:

23> gen_server_test:alloc().     
allocinghandle alloc
=ERROR REPORT==== 6-May-2011::18:05:48 ===
** Generic server gen_server_test terminating 
** Last message in was {alloc}
** When Server state == ok
** Reason for termination == 
** {'function not exported',
       [{gen_server_test,terminate,[{bad_return_value,ok},ok]},
        {gen_server,terminate,6},
        {proc_lib,init_p_do_apply,3}]}
** exception exit: undef
     in function  gen_server_test:terminate/2
        called as gen_server_test:terminate({bad_return_value,ok},ok)
     in call from gen_server:terminate/6
     in call from proc_lib:init_p_do_apply/3
Run Code Online (Sandbox Code Playgroud)

D.N*_*bon 10

以下是错误报告的重要部分:handle_call/3的{bad_return_value,ok} 有效返回值为:

  {reply,Reply,NewState} 
| {reply,Reply,NewState,Timeout}
| {reply,Reply,NewState,hibernate}
| {noreply,NewState} 
| {noreply,NewState,Timeout}
| {noreply,NewState,hibernate}
| {stop,Reason,Reply,NewState} 
| {stop,Reason,NewState}
Run Code Online (Sandbox Code Playgroud)

将handle_call重写为:

handle_call({alloc}, _From, LoopData) ->
  Reply = io:format("handle alloc"),
  {reply, Reply, LoopData}
Run Code Online (Sandbox Code Playgroud)

现在会"解决"这个问题.但是我建议你查看gen_server的文档,因为你还有其他一些奇怪的东西.例如,您的州是结果ìo:format("channels").这根本没有任何意义.

我想你正在学习gen_servers,这很好,我保证文档将帮助你解决你的知识之路上的许多小问题.

  • 此外,未定义或导出所需的回调函数gen_server_test:terminate/2 - 修复这一点非常重要,因此一旦修复此错误,您就不会在干净关闭时遇到另一个错误. (2认同)