【问题标题】:Cannot compile (Not declared & Expected Primary Expression)无法编译(未声明和预期的主表达式)
【发布时间】:2015-12-29 16:44:22
【问题描述】:

下面是一个函数,该函数将被一个主程序调用。我的问题是,如果我不将 challInfo 声明为结构,则在编译时它将返回:

第 5 行第 21 列的 PhotoPoints 页出现错误:“challInfo”未在此范围内声明

同时,如果我声明它(如下所示),它会返回:

第 5 行第 30 列 PhotoPoints 页面上的错误:'.' 标记之前的预期主表达式 第 7 行第 7 列第 52 列 PhotoPoints 页面上的错误:第 8 行 PhotoPoints 页面上的预期主表达式错误, col 28: '.' 之前的预期主表达式第 8 行 PhotoPoints 页面上的令牌错误,col 60: '.' 之前的预期主表达式第 8 行 PhotoPoints 页面上的令牌错误,col 109: 之前的预期主表达式' .' 第 9 行第 31 列 PhotoPoints 页面上的令牌错误:第 9 行第 9 列 PhotoPoints 页面上的预期主表达式出现错误,第 118 列:第 14 行的 PhotoPoints 页上的预期主表达式错误,第 35 列:第 14 行的 PhotoPoints 页上的预期主表达式错误,第 57 列:预期的主表达式之前.' 令牌

我一直在浏览互联网,包括 Stack Overflow,似乎总是针对每种情况给出具体的答案。我承认我迷路了。你能帮忙吗?

float PhotoPoints() {
struct challInfo; 
  bool isFacingOther();
  bool sphereInDark();
  bool isCameraOn = challInfo.camera.cameraOn;
  bool isFacingOtherResult = isFacingOther();
  bool isOppNotInDarkZone = !sphereInDark(challInfo.other.zrState);
  bool myMirror = challInfo.me.mirrorTime != 0 && challInfo.me.mirrorTime + ITEM_MIRROR_DURATION > challInfo.currentTime;
  bool otherMirror = challInfo.other.mirrorTime != 0 && challInfo.other.mirrorTime + ITEM_MIRROR_DURATION > challInfo.currentTime;
  float picturePointValue = 0;
  if (isCameraOn && isFacingOtherResult && isOppNotInDarkZone && !myMirror)
  {
    float bet[3], distance;
    mathVecSubtract(bet, challInfo.me.zrState, challInfo.other.zrState, 3);
    distance = mathVecMagnitude(bet, 3);

    if (distance < PHOTO_MIN_DISTANCE) {
      DEBUG(("Not a good shot: too close to the other satellite | "));
      return 0.0;
    }
    picturePointValue = 2.0 + 0.1/(distance - PHOTO_MIN_DISTANCE + 0.1);
    if(otherMirror){
      picturePointValue = 0;
      DEBUG(("Not a good shot: Opposing mirror active |"));
    }

  }
  else if(!isCameraOn){
    DEBUG(("Not a good shot: camera off |"));
  }
  else if(myMirror){
    DEBUG(("Not a good shot: my mirror's in the way |"));
  }
  else if(!isFacingOtherResult) {
        DEBUG(("Not a good shot: not facing the other satellite | "));
    }
  else if(!isOppNotInDarkZone){
    DEBUG(("Not a good shot: opponent in dark zone |"));
  }
  return picturePointValue;
}

【问题讨论】:

  • struct challInfo 似乎缺少类型名称。或者,如果challInfo 是一个结构类型,它缺少变量名和结构的定义。
  • 我怕没跟上,怎么改?
  • 其他人不可能猜到如何纠正它,但我怀疑该函数应该有一个名为“challInfo”的参数,无论它应该是什么类型。
  • 您接受答案了吗? @NunoViegas
  • 请去掉'mysql'标签;似乎无关。

标签: c++ mysql regex struct


【解决方案1】:

您定义一个struct challInfo,然后将bool isCameraOn 分配给该struct 的数据成员。这是我认为您对structs 有点困惑的问题。 struct 在很多方面就像一个类。例如,我可以将这样的内容放在带有其他类定义的头文件中。

struct Example_struct{
      int data_mem_a;
      bool data_mem_b;
}

然后也许在class原型中使用这个struct

class example_class{
private:
    example_struct struct_instance;
public:
    bool get_a_from_struct(){return struct_instance.data_mem_a;};

在您的情况下,您需要确保在某处定义了 challInfo 结构,然后声明它的一个实例。

 challInfo CI;
 bool isCameraOn = CI.camera.caneraOn;

【讨论】:

    【解决方案2】:
    struct challInfo;
    

    应该有如下定义:

    // Define your camera object
    struct Camera {
            bool isCameraOn;
    }
    // Define the object type of challInfo
    struct PhotographyInfo {
       Camera camera
    }
    

    那么你就可以使用对象了:

    PhotographyInfo challInfo{};
    challInfo.camera = Camera{true};
    

    【讨论】:

      【解决方案3】:

      每次您引用challInfo 的成员时都会出现错误。您需要包含一个完整的声明,以便编译器可以判断结构内部的内容,简单地说它是一个结构。您只提供了前向声明。

      【讨论】:

        猜你喜欢
        • 2021-01-30
        • 1970-01-01
        • 2021-11-27
        • 2021-09-12
        • 1970-01-01
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多