【问题标题】:What causes the error #18 expected a ")" on a MSP430导致错误 #18 在 MSP430 上出现“)”的原因
【发布时间】:2021-09-12 07:47:09
【问题描述】:

使用 TI Code Composer 为 MSP430 编译以下 C++14 代码时出现以下错误:

subdir_rules.mk:14:目标“main.obj”的配方失败 “Interface.h”,第 75 行:错误 #18:应为“)” “Derived.h”,第 91 行:错误 #18:预期为“)”

这适用于以下代码结构,其中Interface.h 类是一个可以为 STM32 目标编译良好的库。

#include <cstdint>

namespace NS
{
  class Interface 
  {
    public:
      // Other pure virtual functions such as:
      virtual void advance() = 0;

      // This is the problem function
      virtual void advance(const uint32_t N) = 0;
  };
}

接下来在 MSP430 项目中,该接口用于多个对象。这是多个实现的一个示例,它们都给出相同的错误。

#include "Interface.h"

class Derived : public ::NS::Interface
{
  public:
    // Overriding virtual functions with implementations in cpp file.
    void advance() override;

    // The problematic function
    void advance(const uint32_t N) override;

  private:
    uint32_t index;
};

cpp 文件:

#include "Derived.h"

Derived::advance() 
{
  ++index;
}

Derived::advance(const uint32_t N)
{
  index += N;
}

现在检查代码中的任何“有趣”字符(如希腊问号)都没有产生任何结果。我尝试替换文本,再次输入等等,没有结果

注释掉函数 advance(const uint32_t N) 解决了问题,因此它不是文件中的其他内容。

什么可能导致这个问题?

【问题讨论】:

  • 你是#include &lt;stdint.h&gt; 还是uint32_t?请告诉我们minimal reproducible example,而不仅仅是摘录。
  • @thebusybee 好点,在原文中包含,将其添加到问题中。
  • 如果您使用#include &lt;cstdint&gt;(而不是#include &lt;stdint.h&gt;),您应该使用像std::uint32_t 这样的限定名称,因为C++ 标准不要求这些名称在全局命名空间IIRC 中可用。
  • @heapunderrun 我明白你的意思。我不明白的是Interfaceuint8_t 的其他功能怎么可能没有同样的问题。在Deriveduint32_t作为变量使用没有任何问题。
  • 错误信息参考 Interface.h 中的第 75 行和 Derived.h 中的第 91 行。指出这些行号指的是哪一个。然而,可能的罪魁祸首是出现在这些行之前的任何内容,使编译器期望一个右括号 - 包括错误行之前的任何嵌套包含中的错误。

标签: c++ embedded c++14 msp430


【解决方案1】:

问题确实如@Clifford 所述。 N 已在 MSP430 代码中的某处定义。将Derived::advance(const uint32_t N) 重命名为Derived::advance(const uint32_t N_bytes) 即可解决问题。

【讨论】:

  • 您可以而且应该接受您自己的答案。在查看明显莫名其妙的错误时,您必须记住编译器在应用预处理后报告代码 - 而不是源文本,因此您必须考虑该上下文中的代码。预处理器宏不受与 C 代码相同的范围规则的约束,N 显然是一个糟糕的宏名称,并且作为变量/参数名称是不明智的 - 一场不明智的命名实践的完美风暴导致了这一点。
猜你喜欢
  • 2013-05-27
  • 2013-10-14
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 2022-11-18
  • 2011-10-08
  • 2012-06-19
  • 1970-01-01
相关资源
最近更新 更多