gpt4 book ai didi

erlang - Erlang 中的“函数未导出”

转载 作者:行者123 更新时间:2023-12-01 17:32:52 25 4
gpt4 key购买 nike

这是我的代码,当我运行: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").

错误:

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

最佳答案

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

  {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}

将你的handle_call重写为:

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

现在就可以“解决”问题了。但我建议一般情况下阅读 gen_server 的文档,因为您还会遇到其他一些奇怪的事情。例如,您的状态是 ìo:format("channels") 的结果。这根本没有任何意义。

我猜你正在学习 gen_servers,这很棒,我保证 documentation将帮助你解决求知路上的很多小问题。

关于erlang - Erlang 中的“函数未导出”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5909865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com