【问题标题】:Getting " ISO C++ forbids declaration ..." error.?得到“ISO C++ 禁止声明......”错误。?
【发布时间】:2012-10-31 22:58:15
【问题描述】:

以下代码只是我的头文件的一部分

    double calculateDistance(const wp, &CWaypoint);
    void print(int format);
    bool less(wp_right, const &CWaypoint); 
    CWaypoint add(wp_right, const &CWaypoint);

错误是:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Waypoint.o ..\src\Waypoint.cpp
In file included from ..\src\Waypoint.cpp:15:0:
..\src\/Waypoint.h:45:33: error: 'wp' does not name a type
..\src\/Waypoint.h:45:33: error: ISO C++ forbids declaration of 'parameter' with no type
..\src\/Waypoint.h:47:12: error: 'wp_right' has not been declared
..\src\/Waypoint.h:48:16: error: 'wp_right' has not been declared 

附: : 我是 C++ 初学者

【问题讨论】:

  • 您的标题仅显示部分错误消息。请在您的问题正文中向我们展示整个消息。
  • g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Waypoint.o ..\src\Waypoint.cpp 在 ..\src\Waypoint.cpp 包含的文件中:15:0: ..\src\/Waypoint.h:45:33: 错误:'wp' 没有命名类型 ..\src\/Waypoint.h:45:33: 错误:ISO C++ 禁止声明'parameter' 没有类型 ..\src\/Waypoint.h:47:12: 错误: 'wp_right' 没有被声明 ..\src\/Waypoint.h:48:16: 错误: 'wp_right' 没有被宣布

标签: c++ syntax-error


【解决方案1】:

我想你是说

double calculateDistance(const wp, CWaypoint&);

等等

& 放在类型之后而不是之前。您可能还有其他错误,很难确定。通常我会在函数原型中同时使用类型和变量名,尽管变量名是可选的。

根据下面 cmets 中的代码确定,您似乎想要

class CWaypoint
{
...
    double calculateDistance(const CWaypoint& wp); 
    void print(int format); 
    bool less(const CWaypoint& wp_right);
    CWaypoint add(const CWaypoint& wp_right);
};

我不确定您为什么将参数名称放在类型之前,或者为什么您使用逗号分隔参数名称和类型。您已使用 getAllDataByPointer 和 getAllDataByReference 等其他方法正确完成。

规则是逗号分隔方法参数,因此如果您的方法采用单个参数,则不应有逗号,如果采用两个参数,则两个参数声明之间应有一个逗号,等等。

【讨论】:

  • 这就是我想要做的;类 CWaypoint { 私有:字符串 m_name;双 m_latitude;双 m_longitude; void transformLongitude2degmmss(int &deg, int &mm, double &ss); void transformLatitude2degmmss(int &deg, int &mm, double &ss);
  • public: CWaypoint(字符串名称, 双纬, 双经); void set(字符串名称,双纬度,双经度);字符串 getName();双 getLatitude();双 getLongitude(); void getAllDataByPointer(string *pname,double *platitude, double *plongitude); void getAllDataByReference(string &pname,double &platitude, double &plongitude);双计算距离(常量 wp,CWaypoint&);无效打印(整数格式); bool less(wp_right, const CWaypoint&); CWaypoint add(wp_right, const CWaypoint&); };
【解决方案2】:

您的函数声明中有 3 个错误。 gcc 报告的第一个错误是:wp 没有类型:你说const wp,好的wp 是一个常量,但是等待常量是什么??

第二个错误是您将& 放在类型之前,这也是一个错误。

第三,你把参数的名字放在输入之前,所以最后你应该有:

double calculateDistance(const CWaypoint& wp);
bool less(const CWaypoint& wp_right);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2010-12-16
    • 2015-10-31
    相关资源
    最近更新 更多