C++ 声明终止在 cpp 中的以下代码中出现错误错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21686274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:46:35  来源:igfitidea点击:

Declaration terminated Incorrectly error in following code in cpp

c++observer-patterncompile-time

提问by Manu Coder

Iam trying to develop C++ program for observer pattern but i am getting these errors. Here's my CPP code , and i getting error continuously : "Declaration termination incorrectly" ! Thanks in Advance Help me please i am desperate.

我正在尝试为观察者模式开发 C++ 程序,但我收到了这些错误。这是我的 CPP 代码,我不断收到错误消息:“声明终止不正确”!预先感谢帮助我,我很绝望。

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

class Subject{
public :        virtual ~Subject();
        virtual float attach()=0;
        virtual int notify()=0;
};

class Observer{
public :        virtual ~Observer();
        virtual void update(int type, float amount, float bal)=0;
};

class Account : public Subject
{
public:  float attach()
 {
    char name[12];
    int account_no;
    float bal;
    cout<<"Enter the Name of Account Holder : ";
    cin>>name;
    cout<<"Enter the Account No. : ";
    cin>>account_no;
    cout<<"Enter the Balance of his account : ";
    cin>>bal;
    cout<<"The Name of Account Holder : "<<name;
    cout<<"The Account No. : "<<account_no;
    cout<<"The Balance of his account : "<<bal;
    return bal;
}
int notify()
{
    int type;
    cout<<"\nMenu :\n\n1) Deposit\n2)Withdrawl\n";
    cout<<"Enter the type  for transition : \n";
    cin>>type;
    return type;
}
public: void update(int type, float amount, float bal)
{
    char name[12];
    int account_no;
    if(type==1)
        bal=bal+amount;
    else if(type==2)
        bal=bal-amount;
    else
        cout<<"Oops! Transition Type is invalild....";
    cout<<"\nThe Details of Account Holder after Transition     :-\n";
    cout<<"The Name of Account Holder : "<<name;
    cout<<"The Account No. : "<<account_no;
    cout<<"The Balance of his account : "<<bal;
}
};

class obpt{
public : static void main()
{
    Account ac;
    //AccountUpdate au;
    float balance, amt;
    int type;
    clrscr();
    cout<<"\nWelcome To The Program of Observer Pattern of Account Transition\n";
    cout<<"\nEnter the Details of Account Holder :-\n";
    balance = ac.attach();
    cout<<"\nCall notification for Deposit or Withdrawl Transition\n";
    type=ac.notify();
    cout<<"\nEnter the amount for transition : \n";
    cin>>amt;
    cout<<"\nAfter The transition the Main balance : \n";
    ac.update(type, amt, balance);
    getch();
}
}

回答by erenon

You are missing a ;at the end of the class declaration. Correct:

;在类声明的末尾缺少一个。正确的:

class Foo
{
    /*...*/
};

In C++, mainshould be a free function, the obptclass is wrong.

C++main应该是自由函数,obpt类错了。

int main()
{
  /* ... */
}

回答by user7594349

The error message you are receiving is coming from the compiler. It is stating that a declaration statement has not been ended correctly.

您收到的错误消息来自编译器。它表明声明语句没有正确结束。

Somewhere in your code you are missing a semi-colon ; perhaps at the end of a class declaration or after a variable is defined.

在您的代码中,您缺少一个分号;可能在类声明的末尾或在定义变量之后。

Without seeing your code, there is no way for me to pinpoint the problem, but the error message should take you to the line if you double click it!

没有看到您的代码,我无法查明问题,但是如果您双击它,错误消息应该会将您带到该行!

Regards, Rakesh.

问候,拉克什。