Erlang ';函数未导出';在二郎
这是我的代码,当我运行:gen_server_test:alloc时,它给出了错误,请帮我解决Erlang ';函数未导出';在二郎,erlang,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({loca
-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").
错误:
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
您的
handle\u调用
和handle\u cast
方法需要返回类型为{reply,Response,NewState}
或{noreply,NewState}
的元组。有关允许返回值的更多信息,请参阅文档
io:format(..)
函数仅返回ok
。。这就是您看到此错误的原因。以下是错误报告的重要部分:{bad\u return\u value,ok}
的有效返回值为:
将句柄\调用重写为:
handle_call({alloc}, _From, LoopData) ->
Reply = io:format("handle alloc"),
{reply, Reply, LoopData}
现在就能“解决”问题了。但我建议大家浏览一下gen_server的文档,因为通常会有一些奇怪的事情发生。例如,您的状态是ìo:format(“channels”)
的结果。这毫无意义
我猜您正在学习gen_服务器,这很好,我保证它将帮助您解决知识道路上的许多小问题。此外,所需的回调函数gen_server_test:terminate/2未定义或导出-修复这一点也很重要,因此一旦修复此错误,您就不会在完全关闭时遇到另一个错误。
handle_call({alloc}, _From, LoopData) ->
Reply = io:format("handle alloc"),
{reply, Reply, LoopData}