【问题标题】:Xcode error compiling c++ Expected member name or ';' after declaration specifiersXcode 错误编译 c++ 预期的成员名称或';'在声明说明符之后
【发布时间】:2015-10-18 08:19:52
【问题描述】:

我在尝试在 Xcode 中编译的 c++ 库 (openNN) 中的检查方法存在问题。我将使用其中一种方法的示例,因为我怀疑它们都是由同一个问题引起的。

我得到错误的标题声明:
预期的成员名称或';'在声明说明符之后。

void check(void) const;

函数定义:

void InverseSumSquaredError::check(void) const
{
    std::ostringstream buffer;

    // Neural network stuff

    if(!neural_network_pointer)
    {
       buffer << "OpenNN Exception: InverseSumSquaredError class.\n"
              << "void check(void) const method.\n"
              << "Pointer to neural network is NULL.\n";

       throw std::logic_error(buffer.str().c_str());      
    }

    const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();

    if(!multilayer_perceptron_pointer)
    {
       buffer << "OpenNN Exception: InverseSumSquaredError class.\n"
              << "void check(void) const method.\n"
              << "Pointer to multilayer perceptron is NULL.\n";

       throw std::logic_error(buffer.str().c_str());      
    }

    const unsigned int inputs_number = multilayer_perceptron_pointer->count_inputs_number();
    const unsigned int outputs_number = multilayer_perceptron_pointer->count_outputs_number();

    if(inputs_number == 0)
    {
       buffer << "OpenNN Exception: InverseSumSquaredError class.\n"
              << "void check(void) const method.\n"
              << "Number of inputs in multilayer perceptron object is zero.\n";

       throw std::logic_error(buffer.str().c_str());      
    }

    if(outputs_number == 0)
    {
       buffer << "OpenNN Exception: InverseSumSquaredError class.\n"
         << "void check(void) const method.\n"
         << "Number of outputs in multilayer perceptron object is zero.\n";

       throw std::logic_error(buffer.str().c_str());      
    }

    // Mathematical model stuff

    if(!mathematical_model_pointer)
    {
        buffer << "OpenNN Exception: InverseSumSquaredError class.\n"
               << "void check(void) const method.\n"
               << "Pointer to mathematical model is NULL.\n";

        throw std::logic_error(buffer.str().c_str());     
    }

    // Data set stuff 

    if(!data_set_pointer)
    {
       buffer << "OpenNN Exception: InverseSumSquaredError class.\n"
              << "void check(void) const method.\n"
              << "Pointer to data set is NULL.\n";

       throw std::logic_error(buffer.str().c_str());      
    }

    // Final solutions error stuff

}

如果我将标题中的定义更改为
void InverseSumSquaredError::check(void) const;
我最终得到了错误:
成员“检查”的额外资格

我目前正在使用方言 C++98 和库 libc++。 Xcode 设置为将源代码编译为 Objective-C++,它负责处理大多数其他错误。

我想不出与此问题相关的任何其他内容,这让我困惑了好几个小时,因此非常感谢任何类型的帮助。

【问题讨论】:

  • 这不是'c'所以请去掉'c'标签
  • 这会用作纯 C++ 吗?还是您想为 OSX/iOS/Cocoa 使用混合模式?
  • 它将与 OS X 上的 Objective-c 混合,可能是 iOS
  • 改函数名有用吗?也许check 在某处是#defined。另外,您能否在标题声明之前显示几行,您可能会缺少分号
  • check 似乎没有在其他地方定义。尽管更改后它编译得很好。我猜 Xcode 不喜欢方法名。

标签: c++ xcode objective-c++


【解决方案1】:

不知道你的确切设置,但是当源代码引用AssertMacros.h 并且还定义了check 方法时,已经有historical reports。在我的测试代码中:

#include <stdio.h>
#include <AssertMacros.h>

class InverseSumSquaredError {
public:
   void check(void) const;
}

我观察到同样的错误。包括行:

#define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0

在包含 AssertMacros.h 之前修复了该问题。

【讨论】:

  • 我发现在 OpenCV 3 中,交换头文件的顺序 include 以便在任何 Cocoa 头文件之前包含 opencv2/opencv.hpp 也可以避免编译器错误。
  • 对于不同的标题组合有同样的问题。
  • 在包含opencv.hpp 之前使用#undef check 可以解决该错误。这在混合 Swift/obj-C 环境中很有用,在这种环境中您不能总是打乱包含的顺序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 2022-11-10
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多