【问题标题】:What does this error in calling a method mean?调用方法的这个错误是什么意思?
【发布时间】:2011-03-25 15:15:16
【问题描述】:

我刚从 Vala 开始,我尝试制作一个简单的程序来询问两个输入:

  1. 一个指定循环度的int;和
  2. 一个包含 I/R 的字符,用于迭代或递归过程。

就在编译之前,我收到了这个错误:

test0.vala:8.5-8.16: error: Access to instance member `test0.test_exec' denied
        test_exec(q);
        ^^^^^^^^^^^ //the entire statement
Compilation failed: 1 error(s), 0 warning(s)

这个非常简单的程序的pastebin位于here

这是一个sn-p:

public static void main(string[] args)
{
    stdout.printf("Greetings! How many cycles would you like? INPUT: ");
    int q=0;
    stdin.scanf("%d", out q);
    test_exec(q);
}

public void test_exec(int q)
{
    //method code here
}

您能告诉我该怎么做,以及一些提示吗?谢谢。

【问题讨论】:

  • @Rek:欢迎来到代码审查。该站点用于查看工作代码,而不是用于修复损坏的代码。请参考常见问题解答:codereview.stackexchange.com/faq

标签: vala


【解决方案1】:

您将test_exec 定义为实例(非静态)方法。与静态方法不同,需要在给定类的实例上调用实例方法。但是,您尝试在没有此类实例的情况下调用它,因此会出现错误。

因此,您要么需要创建 test0 类的实例并在其上调用 test_exec(尽管这没有什么意义,因为 test_exec 不依赖或更改对象的任何状态 - 作为一个问题事实上test0 类没有任何状态)或将test_exec 以及test_exec 调用的其他方法设为静态。

【讨论】:

  • 感谢您的启发!它有效,但现在我在 test_exec 方法中捕获错误时出现逻辑错误:P
  • @Lyon:您可能应该为此创建一个新问题,包括所有代码和您希望它做什么以及它与实际做什么有何不同的描述。
猜你喜欢
  • 1970-01-01
  • 2010-09-07
  • 2018-11-29
  • 2011-03-01
  • 2012-11-06
相关资源
最近更新 更多