如何调试“ISO C++ 禁止声明没有类型的‘汽车’”?
Posted
技术标签:
【中文标题】如何调试“ISO C++ 禁止声明没有类型的‘汽车’”?【英文标题】:How to debug "ISO C++ forbids declaration of 'car' with no type"? 【发布时间】:2018-11-28 04:56:42 【问题描述】:我试图弄清楚为什么我在 Dev C++ 上不断收到此错误。当我使用学校的计算机时,我似乎没有遇到任何错误,所以我想知道我是否在这里遗漏了什么。
[错误] ISO C++ 禁止声明没有类型 -f 的“汽车” 允许的
代码:
#include <iostream>
#include <string>
using namespace std;
class Car //class definition
private: int modelYr; string model; int speed;
public:
car(int year, string brand)
modelYr=year; model=brand; speed=0; //Constructor
void accelerate() speed = speed + 5; //accelerate +5mph (mutator)
void brake() speed = speed - 5; //brake -5mph (mutator)
int get_modelYr() const return modelYr; //returns model year (accessor)
string get_model() const return model; //returns model name (accessor)
int getspeed() const return speed; //returns current speed (accessor)
;//end of class definition (note the semicolon ;)
int main()
Car vehicle; //create a vehicle from class Car
string carModel;//car model to be input by user
int carYear; //car year to be input by user
const string line = "\n-------------------\n"; //used to display a line
cout <<"Enter car model year (2000-current): ";
while(! (cin >> carYear) || (carYear < 2000) ) //carYear validation
//checking for numeric year 2000 or later
cin.clear();cin.ignore(10000,'\n');
cout<<"sorry you must enter car year 2000 or later :"<<flush;
//while loop validation ends
cout <<"Enter car model (example: GMC): "; cin >> carModel;
vehicle.car(carYear,carModel);
cout << line << "Starting speed for your " << vehicle.get_modelYr()
<< " " << vehicle.get_model() << " is " << vehicle.getspeed() << " mph" <<line;
for(int count=0;count<5;count++) //accelerate five times
vehicle.accelerate();
cout << "***ACCELERATING***\nSpeed is currently:"<< vehicle.getspeed() << endl;
//end acceleration
for ( int count=0;count<5 ;count++ )
vehicle.brake();
cout << "***BRAKING***\nSpeed is currently: " << vehicle.getspeed() << endl;
cout << line << "Ending speed for your " << vehicle.get_modelYr()
<< " " << vehicle.get_model() << " is " << vehicle.getspeed() <<" mph"<< line;
return(0);
//end of main program
【问题讨论】:
根据它的标签描述,Dev-C++是一个IDE。但是您的问题不是关于 IDE,对吗?这不是一个沼泽标准c++
问题吗?
作为新的注册会员,您可能应该阅读登录时建议的tour,而不是跳过它。您可能还想浏览Help center - 例如,阅读如何找到更好的标题以及如何正确格式化您的帖子。
【参考方案1】:
您的构造函数的名称必须与您的类相同。
#include <iostream>
#include <string>
using namespace std;
class Car //class definition
private: int modelYr; string model; int speed;
public:
Car(int year, string brand)
modelYr=year; model=brand; speed=0; //Constructor
void accelerate() speed = speed + 5; //accelerate +5mph (mutator)
void brake() speed = speed - 5; //brake -5mph (mutator)
int get_modelYr() const return modelYr; //returns model year (accessor)
string get_model() const return model; //returns model name (accessor)
int getspeed() const return speed; //returns current speed (accessor)
;//end of class definition (note the semicolon ;)
int main()
Car vehicle; //create a vehicle from class Car
string carModel;//car model to be input by user
int carYear; //car year to be input by user
const string line = "\n-------------------\n"; //used to display a line
cout <<"Enter car model year (2000-current): ";
while(! (cin >> carYear) || (carYear < 2000) ) //carYear validation
//checking for numeric year 2000 or later
cin.clear();cin.ignore(10000,'\n');
cout<<"sorry you must enter car year 2000 or later :"<<flush;
//while loop validation ends
cout <<"Enter car model (example: GMC): "; cin >> carModel;
vehicle.car(carYear,carModel);
cout << line << "Starting speed for your " << vehicle.get_modelYr()
<< " " << vehicle.get_model() << " is " << vehicle.getspeed() << " mph" <<line;
for(int count=0;count<5;count++) //accelerate five times
vehicle.accelerate();
cout << "***ACCELERATING***\nSpeed is currently:"<< vehicle.getspeed() << endl;
//end acceleration
for ( int count=0;count<5 ;count++ )
vehicle.brake();
cout << "***BRAKING***\nSpeed is currently: " << vehicle.getspeed() << endl;
cout << line << "Ending speed for your " << vehicle.get_modelYr()
<< " " << vehicle.get_model() << " is " << vehicle.getspeed() <<" mph"<< line;
return(0);
//end of main program
【讨论】:
以上是关于如何调试“ISO C++ 禁止声明没有类型的‘汽车’”?的主要内容,如果未能解决你的问题,请参考以下文章