【问题标题】:'Stack' Undeclared Error . Java to C“堆栈”未声明的错误。 Java 到 C
【发布时间】:2015-01-27 03:10:59
【问题描述】:

对于我的班级,我应该翻译提供给我们的代码并将其从 Java 翻译成 C。这是我第一次接触 C。所以我完成了。编译后我收到大约 6 个错误,说堆栈未声明。堆栈在代码中使用。谁能帮忙指出我哪里出错了。

// $Id: crpn.c,v 1.28 2014-04-08 15:23:19-07 - - $
// NAME
#include <assert.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>

int exit_status = EXIT_SUCCESS;
#define EMPTY (-1)
#define SIZE 16

struct stack{
   int top;
   double numbers[SIZE];
};

void bad_operator (const char *oper) {
   fflush (NULL);
   fprintf (stderr, "oper=\"%s\"\n", oper);
   fflush (NULL);
   exit_status = EXIT_FAILURE;
}

void push (struct stack *the_stack, double number) {
   if(stack->top >= SIZE - 1){
      printf ("%s: stack overflown%n", number);
   }else{
       stack->numbers[++stack->top] = number;
   }
}

void do_binop (struct stack *the_stack, char oper) {
   if (stack->top < 1) {
      printf ("'%s': stack undeflow", oper);
   }else{
      double right = stack->numbers[stack->top--];
        double left = stack->numbers[stack->top--];
      switch (oper) {
         case '+': push (stack, left + right); break;
         case '-': push (stack, left - right); break;
         case '*': push (stack, left * right); break;
         case '/': push (stack, left / right); break;
      }
   }
}

void do_print (struct stack *the_stack) {
   if (stack->top == EMPTY) {
      printf ("stack is empty%n");
   }else {
      for (int pos = 0; pos <= stack->top; ++pos) {
          printf ("%s%n", stack->numbers[pos]);
      }
   }
}

void do_clear (struct stack *the_stack) {
   stack->top = EMPTY;
}

void do_operator (struct stack *the_stack, const char *oper) {
   switch (oper->charAt(0)) {
      case '+': do_binop (stack, '+'); break;
      case '-': do_binop (stack, '-'); break;
      case '*': do_binop (stack, '*'); break;
      case '/': do_binop (stack, '/'); break;
      case ';': do_binop (stack);      break;
      case '@': do_clear (stack);      break;
      default : bad_operator (oper);   break;
   }
}
int main (int argc, char **argv) {
   if (argc != 1) {
      fprintf (stderr, "Usage: %s\n", basename (argv[0]));
      fflush (NULL);
      exit (EXIT_FAILURE);
   }
   struct stack the_stack;
   the_stack.top = EMPTY;
   char buffer[1024];
   for (;;) {
      int scanrc = scanf ("%1023s", buffer);
      if (scanrc == EOF) break;
      assert (scanrc == 1);
      if (buffer[0] == '#') {
         scanrc = scanf ("%1023[^\n]", buffer);
         continue;
      }
      char *endptr;
      double number = strtod (buffer, &endptr);
      if (*endptr == '\0') {
         push (&the_stack, number);
      }else if (buffer[1] != '\0') {
         bad_operator (buffer);
      }else {
         do_operator (&the_stack, buffer);
      }
   }
   return exit_status;
}

错误信息:

crpn.c: In function ‘push’:
crpn.c:25:7: error: ‘stack’ undeclared (first use in this function)
    if(stack->top >= SIZE - 1){
       ^
crpn.c:25:7: note: each undeclared identifier is reported only once for each function it appears in
crpn.c:26:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘double’ [-Wformat=]
       printf ("%s: stack overflown%n", number);
       ^
crpn.c:26:7: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=]
crpn.c:24:26: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void push (struct stack *the_stack, double number) {
                          ^
crpn.c: In function ‘do_binop’:
crpn.c:33:8: error: ‘stack’ undeclared (first use in this function)
    if (stack->top < 1) {
        ^
crpn.c:34:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
       printf ("'%s': stack undeflow", oper);
       ^
crpn.c:32:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_binop (struct stack *the_stack, char oper) {
                              ^
crpn.c: In function ‘do_print’:
crpn.c:48:8: error: ‘stack’ undeclared (first use in this function)
    if (stack->top == EMPTY) {
        ^
crpn.c:49:7: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=]
       printf ("stack is empty%n");
       ^
crpn.c:47:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_print (struct stack *the_stack) {
                              ^
crpn.c: In function ‘do_clear’:
crpn.c:58:4: error: ‘stack’ undeclared (first use in this function)
    stack->top = EMPTY;
    ^
crpn.c:57:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_clear (struct stack *the_stack) {
                              ^
crpn.c: In function ‘do_operator’:
crpn.c:62:16: error: request for member ‘charAt’ in something not a structure or union
    switch (oper->charAt(0)) {
                ^
crpn.c:63:27: error: ‘stack’ undeclared (first use in this function)
       case '+': do_binop (stack, '+'); break;
                           ^
crpn.c:67:7: error: too few arguments to function ‘do_binop’
       case ';': do_binop (stack);      break;
       ^
crpn.c:32:6: note: declared here
 void do_binop (struct stack *the_stack, char oper) {
      ^
crpn.c:61:33: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_operator (struct stack *the_stack, const char *oper) {

【问题讨论】:

    标签: java c undeclared-identifier


    【解决方案1】:

    'stack'未声明的错误

    stack 是类型,但您的指针是 the_stack

    void push (struct stack *the_stack, double number) 
    

    因此您需要在此类语句中使用the_stack 而不是stack

       if(stack->top >= SIZE - 1){
    

    【讨论】:

      【解决方案2】:

      变量声明为the_stack:

      void push (struct stack *the_stack, double number) {
      

      但你称它为stack

       if(stack->top >= SIZE - 1){
      

      您的代码中没有变量stack,因此编译器会抱怨未知标识符。 struct stack 是变量the_stack 的基本类型(完整类型是struct stack *)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-09
        • 2011-03-27
        • 2017-06-01
        • 2011-05-23
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多