【问题标题】:pre-definition of struct, error结构的预定义,错误
【发布时间】:2014-05-05 22:16:27
【问题描述】:

我有三个来源: codeproc.h

typedef enum {typeBool, typeVarDeclaration, typeFuncDeclaration } nodeEnum;
typedef struct varDeclarationNodeType{
    char *varName;                  /* Var Name */
    int defaultType;                /* default value type*/
    int defaultValue;               /* default value*/
    int ndim;                       /* number of dimensions */
    int *dim;                       /* dimensions (expandable) */
} varDeclarationNodeType;

typedef struct {
    char *funcName;
    std::vector<char *> *args;       /* arguments (expandable) */
} funcDeclarationNodeType;

typedef struct {
    bool value;
} boolNodeType;

typedef struct nodeTypeTag {
    nodeEnum type;              /* type of node */

    union {
        boolNodeType boolVal;   /* bools */
        varDeclarationNodeType varDeclaration;      /*var declarations*/
        funcDeclarationNodeType funcDeclaration;
    };
} nodeType;

codeproc.cpp

#include "codeproc.h"
#include "codecontext.h"
...

codecontext.h

#include "codeproc.h"
class Function{
public:
    Function();
    ~Function();
    map<string, Value*> locals;     //local variables
    map<string, Value*> args;       //arguments
    int numOfArgs;                  //number of arguments
    nodeType *entryPoint;           //first statement in function
    Value *lastCallResult;          //variable that contain result from last call
};

错误:

codeproc.h 错误:“varDeclarationNodeType”之前的声明为“typedef struct varDeclarationNodeType varDeclarationNodeType”

就像那样。 这种情况下如何预定义struct?

【问题讨论】:

  • codecontext 包含codeproc,可以避免直接包含在cpp文件中
  • 您需要包含警卫(或确保 100% 不会多次包含同一个文件)
  • 谢谢,我在一个标题中错过了一个包含保护。

标签: c++ struct


【解决方案1】:

这部分

typedef struct varDeclarationNodeType{
    ...
} varDeclarationNodeType;

应该是这样的

typedef struct{
    ...
} varDeclarationNodeType;

你应该在你的头文件中使用头文件保护或#pragma once

【讨论】:

    【解决方案2】:

    我没有看到预处理器 #pragma once 指令或其他东西来确保您没有多次重新定义类型或包含头文件。

    include guards 放入您的文件中以防止重复包含。

    在您的特定情况下,头文件顶部的简单#pragma once 应该可以防止多重包含(确认所有必要的类型都已声明,上面的代码只是一个sn-p)。

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2010-12-17
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多