无法将 eosio::name 转换为 uint64_t

Posted

技术标签:

【中文标题】无法将 eosio::name 转换为 uint64_t【英文标题】:not able to convert eosio::name to uint64_t 【发布时间】:2019-05-17 01:05:48 【问题描述】:

我正在使用 eos.cdt v1.3.2 处理智能合约。我已经尝试尽可能地重构以跟上 eos.cdt 中所做的更改。这是合同:

#include <eosiolib/eosio.hpp>
#include <eosiolib/name.hpp>

using namespace eosio;
using namespace std;
class addressbook: contract 
  struct address 
     uint64_t account_name;
     string first_name;
     string last_name;
     string street;
     string city;
     string state;
     auto primary_key() const  return account_name; 
     EOSLIB_SERIALIZE( address, (account_name)(first_name)(last_name)(street)(city)(state) )
  ;
  public:
    using contract::contract;

    addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) 

    typedef eosio::multi_index< "address"_n, address > address_index;
    void myaction() 
      address_index addresses(_code, _code.value); // code, scope
      // add to table, first argument is account to bill for storage
      addresses.emplace(_self, [&](auto& address) 
        address.account_name = name("foo");
        address.first_name = "Daniel";
        address.last_name = "Larimer";
        address.street = "1 EOS Way";
        address.city = "Blacksburg";
        address.state = "VA";
      );
      auto user = addresses.get(name("foo"));
      eosio_assert(user.first_name == "Daniel", "Couldn't get him.");
    

EOSIO_ABI( addressbook, (myaction) )

当我尝试从命令行编译时,我收到以下错误消息:

get.cpp:34:33: error: no viable conversion from 'eosio::name' to 'uint64_t' (aka
      'unsigned long long')
      auto user = addresses.get(name("foo"));
                                ^~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/name.hpp:131:17: note: candidate
      function
      constexpr operator raw()const  return raw(value); 
                ^
/usr/local/eosio.cdt/bin/../include/eosiolib/multi_index.hpp:1963:30: note: 
      passing argument to parameter 'primary' here
      const T& get( uint64_t primary, const char* error_msg = "unable t...
                             ^
get.cpp:38:26: error: C++ requires a type specifier for all declarations
EOSIO_ABI( addressbook, (myaction) )
                         ^
get.cpp:38:37: error: expected function body after function declarator
EOSIO_ABI( addressbook, (myaction) )
                                    ^
get.cpp:27:32: error: assigning to 'uint64_t' (aka 'unsigned long long') from
      incompatible type 'eosio::name'
        address.account_name = name("foo");
                               ^~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/multi_index.hpp:1691:13: note: in
      instantiation of function template specialization
      'addressbook::myaction()::(anonymous
      class)::operator()<addressbook::address>' requested here
            constructor( obj );
            ^
get.cpp:26:17: note: in instantiation of function template specialization
      'eosio::multi_index<3626371193025593344,
      addressbook::address>::emplace<(lambda at get.cpp:26:32)>' requested here
      addresses.emplace(_self, [&](auto& address) 
                ^
4 errors generated.

我最关心的是读取 get.cpp:34:33: 错误:没有可行的从 'eosio::name' 到 'uint64_t' 的转换(又名 'unsigned long long') 因为此消息是由于 eos.cbt 中的更改而出现的。但是,似乎没有关于如何纠正此问题的解决方案。有人能解决这个问题吗?

【问题讨论】:

【参考方案1】:

看到这个答案:https://eosio.stackexchange.com/questions/3524/using-name-as-secondary-index

简短的版本是您需要在名称后添加.value

    auto user = addresses.get(name("foo").value);

【讨论】:

【参考方案2】:

使用

addresses.find(name("foo").value)

供参考- https://developers.eos.io/eosio-cpp/docs/using-multi-index-tables#section-1-create-a-struct

【讨论】:

以上是关于无法将 eosio::name 转换为 uint64_t的主要内容,如果未能解决你的问题,请参考以下文章

JavaCPP 错误:无法将 `char*` 转换为 `Abc*` 以将参数 '1' 转换为 `void testMyObject(Abc*)`

无法将 HttpContext 强制转换为用户

“无法将 'float' 转换为 float**” 错误

无法将 shapefile 转换为 geojson

无法将char转换为int?

无法将列表转换为数组:ValueError:只有一个元素张量可以转换为 Python 标量