【问题标题】:Turbo C++ compile error "too many types in declaration"Turbo C++ 编译错误“声明中的类型太多”
【发布时间】:2017-07-09 05:42:44
【问题描述】:

每当我编译这个程序时,我都会收到一条错误消息“第 13 行 decleration 中的类型太多”。我没有看到任何可能的语法错误,但我仍然面临这个问题。

#include<iostream.h>
#include<conio.h>

class currency
{
    private:
    int rupee,paise;
    int total;
    public:
    void getdata(int r,int p);
    void display();

}
void currency::getdata(int r, int p){

    rupee=r;
    paise=p;
    total=r*100+p;

}
void currency::display(){

cout<<rupee<<" Rupees"<<" and "<<paise<<"Paise"<<endl;
cout<<"Converted value="<<total;

}

int main(){

    currency c;
        c.getdata(5,25);
        c.display();
        getch();
        return 0;

}

【问题讨论】:

  • 第 13 行是哪一行?
  • 最近有很多 Turbo C++ 问题,这真的很奇怪。是什么激发了这种复兴?
  • 对于初学者,class currency 的正文后缺少一个分号。
  • 不要使用 TurboC++(一个过时的、不符合标准的编译器)。至少使用 C++11 甚至更好的 C++17 和最近的编译器,如 GCCClang/LLVM。两者都是免费软件。
  • 确实如此,但是在学习 C++ 时,C++11 和旧标准(以及 TurboC++ 支持的标准)之间存在巨大差异。 C++11和C++17的区别要小很多

标签: c++ types turbo-c++


【解决方案1】:

需要有一个分号来结束类定义:

}  ;   // semicolon needed here.
void currency::getdata( ...

否则,编译器看起来像这样:

class blahblah {int etc, etc1; int etc2; } void currency::getdata (...

【讨论】:

  • 哦,谢谢,我在不知不觉中跳过了分号,因为我习惯使用 JavaScript,在大多数情况下分号不是必需的。
【解决方案2】:

我猜测,根本问题是class currency 的正文后缺少分号。

回想一下,与在 C/C++ 中相比,您可以将对象定义为类或结构定义的一部分,如下所示:

struct foo
{
    int bar;
} FooObj;

这会创建一个FooObj 类型的变量struct foo

所以在你的代码中,如下:

class currency
{
    /* ... */
}
void currency::getdata(int r, int p){
    /* ... */
}

...等价于:

class currency
{
    /* ... */
} void currency::getdata(int r, int p){
    /* ... */
}

...相当于这个:

class currency
{
    /* ... */
};

currency void currency::getdata(int r, int p){
    /* ... */
}

所以看起来你给函数getdata 提供了两种返回类型,这可以解释错误。

【讨论】:

    【解决方案3】:

    我猜你在类定义后漏掉了一个分号; 类不像函数,而是类似于 C 中的 struct 定义,你需要在 } 后面加上一个分号。 如:

    class A {};
    

    【讨论】:

      【解决方案4】:

      如果您在结束类货币{}后添加分号";",您的程序将被编译并且您将获得正确的输出。

      class abc
      {
         ...
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-06
        • 1970-01-01
        • 2018-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        相关资源
        最近更新 更多