我试图在Visual Studio中构建以下代码,并在最后10行中得到以下错误:错误:表达式必须具有指向对象的指针类型。

有人可以看一下,告诉我这里缺少什么。

这是代码:

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

struct emptyPeak
{
 unsigned int t;
 int accel;
 }peaks[10];

 struct ts0
{
   int dt;
   int N;
   float NValid;
   double t0;
   int data[36];
   int v[36];
 };

struct emptyPeakConstant
{
  int minSeparation_ms;
  int minLocalRange;
  float minRangeFraction;
  int localRangeHalfWidth_ms;
};

 int findAccExtrema()
 {
   int tStart = 10, tEnd = 5;
   unsigned int candidate_t;
   int type = 0;
   int NPeaks=2;
   int n, m, L;
   struct emptyPeak peaks;
   struct emptyPeakConstant pc;
   struct ts0 ts0copy;
   pc.minSeparation_ms = 325;
   pc.minLocalRange=696;
   pc.minRangeFraction=0.62;
   pc.localRangeHalfWidth_ms=250;
   if(ts0copy.dt==0 || ts0copy.N<3 || ts0copy.NValid<3)
   {
      return 0;
   }

   int findMaxima;
   if(type == 0 || type == 2)
   {
     findMaxima = 1;
    }
    else
   {
    findMaxima=0;
    }

    int findMinima;
    if(type == 1 || type == 2)
    {
       findMinima = 1;
     }
     else
     {
       findMinima=0;
      }

 L=round(pc.localRangeHalfWidth_ms/ts0copy.dt);

int minAcc=ts0copy.data[0];
int maxAcc=ts0copy.data[0];
int mnAcc=0;
int K=0;
for(n=0;n<=ts0copy.N-1;n++)
{
  if(~ts0copy.v[n])
{
  continue;
}
if(ts0copy.data[n] > maxAcc)
{
  maxAcc=ts0copy.data[n];
}
if(ts0copy.data[n] < minAcc)
{
   minAcc=ts0copy.data[n];
}
 mnAcc=mnAcc+ts0copy.data[n];
 K=K+1;
 }

  if(K==0)
  {
  return 0; // Instead of return in the matlab code
  }
  mnAcc=mnAcc/K;

  float thresholdMaxima = minAcc + pc.minRangeFraction*(maxAcc - minAcc);
  float thresholdMinima = maxAcc - pc.minRangeFraction*(maxAcc - minAcc);

  for(n=1;n<=ts0copy.N-2;n++)
  {
   candidate_t=ts0copy.t0 + n*ts0copy.dt;

   if(candidate_t<tStart || candidate_t > tEnd || ~ts0copy.v[n])
   {
    continue;
    }

   int hasMaxima;
   if(findMaxima && (ts0copy.data[n] >= thresholdMaxima) && (ts0copy.data[n]                                         >=ts0copy.data[n-1]) && (ts0copy.data[n] >= ts0copy.data[n+1]))
   {
        hasMaxima = 1;
    }
    else
    {
     hasMaxima = 0;
     }

     int hasMinima;

    if(findMinima && (ts0copy.data[n] <= thresholdMinima) && (ts0copy.data[n]  <=ts0copy.data[n-1]) && (ts0copy.data[n] <= ts0copy.data[n+1]))
    {
     findMinima = 1;
     }
     else
     {
       findMinima = 0;
     }

     if(!hasMaxima && !hasMinima)
    {
       continue;
     }

   int maxDelta = 0;

    for(m=n-L;m<=n+L;m++)
    {
      if(m<0 || m>(ts0copy.N-1))
     {
        continue;
     }

      unsigned int delta = abs(ts0copy.data[m] - ts0copy.data[n]);

    if(ts0copy.v[m] && delta > maxDelta)
    {
     maxDelta=delta;
    }
    }

if(maxDelta < pc.minLocalRange)
{
  continue;
 }

if(NPeaks == 0 || (candidate_t - peaks[NPeaks-1].t) > pc.minSeparation_ms)
{
    NPeaks=NPeaks+1;
}
else if(abs(ts0copy.data[n]-mnAcc) < abs(peaks[NPeaks-1].accel-mnAcc))
{
    continue;
}
}

peaks[NPeaks-1].t = candidate_t;
peaks[NPeaks-1].accel=ts0copy.data[n];
return NPeaks;
}

最佳答案

代码中存在一些问题(不一定解决实际问题)。

1)你真的想做吗

    struct emptyPeak
{
 unsigned int t;
 int accel;
 }peaks[10];




还是你的意思

struct emptyPeak
{
 unsigned int t;
 int accel;
 peaks[10];
}


2)如果您确实正确声明了上述结构,那么您是否还意味着在函数findAccExtrema()中声明一个变量,如下所示:

struct emptyPeak peaks;


因为现在您正在对一个不是数组的结构进行操作,就好像它是数组一样。这可能就是为什么您看到自己存在的问题。

关于c++ - 在Visual Studio中执行C程序时,表达式必须具有指向对象类型ERROR的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29617641/

10-13 08:25