【问题标题】:unknown parse error when testing function测试功能时出现未知的解析错误
【发布时间】:2016-12-29 16:24:11
【问题描述】:

我有下面的函数,它似乎可以正常工作,但是当通过测试程序运行程序时,我得到了错误:解析错误:[int xSwapped = ((255

int dl15(int x, int n, int m){
        // calculates shifts, create mask to shift, combine result
        // get number of bytes needed to shift, multiplying by 8
        // get Masks by shifting 0xff and shift amount
        // shift bits to required position
        // combine results

        int nShift = n<< 3;
        int mShift = m<< 3;

        int nMask = x & (255 << nShift);
        int mMask = x & (255 << mShift);
        nMask = 255 & (nMask >> nShift);
        mMask = 255 & (mMask >> mShift);
        nMask = nMask << mShift;
        mMask = mMask << nShift;

        int xSwapped = ((255 << nShift) | (255 << mShift));

        return (~xSwapped & x) | nMask | mMask;

}

不确定我缺少什么,谢谢。

【问题讨论】:

  • 你使用 MSVC(或 C89 编译器)吗?
  • @BLUEPIXY 不,不是在 Windows 上,测试人员使用 MIT CILK 组的 ANSI C 编译器
  • 如果使用 GCC,请使用 -std=c99 选项。或该行移至int mShift = m&lt;&lt; 3; 之后

标签: c parsing parse-error


【解决方案1】:

看起来您正在使用设置为旧 C 标准的 C 编译器。在 C99 之前,您不能将可执行语句放在声明之前。

您可以通过将 xSwapped 的声明移到顶部来解决此问题:

int nShift = n<< 3;
int mShift = m<< 3;

int nMask = x & (255 << nShift);
int mMask = x & (255 << mShift);
int xSwapped;                                   // Declaration
nMask = 255 & (nMask >> nShift);
mMask = 255 & (mMask >> mShift);
nMask = nMask << mShift;
mMask = mMask << nShift;

xSwapped = ((255 << nShift) | (255 << mShift)); // Assignment

return (~xSwapped & x) | nMask | mMask;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多