如何调试"ISO C++ forbids declaration of 'car' with no type"?



我正试图弄清楚为什么在Dev C++上一直出现这个错误。当我使用学校的电脑时,我似乎没有任何错误,所以我想知道我是否遗漏了什么。

〔错误〕ISO C++禁止声明没有-f类型的"car"允许

代码:

#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

构造函数的名称必须与类的名称相同。

#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

相关内容

  • 没有找到相关文章

最新更新