我正在为我的C ++类编写一个汽车类程序,而Visual Studio不会使用的唯一部分是" string和make"。我评论了他们,程序运行得很好。我们的老师要求我们将其拆分为3个单独的页面。标题中发布的错误是MISSING TYPE SPECIFIER。事实证明,所需要的只是"使用命名空间std;"在car.h文件中。
感谢您的帮助。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include "Car.h"
using namespace std;
int main()
{
// Declare car object
Car myCar;
int speed = 0, year;
string make;
cout << "This program will help you with your Car!" << endl; // intro message
cout << "Please enter the Year" << endl; // store year
cin >> year;
cout << "Please enter the Make of the car" << endl; // store make
cin >> make;
// call member functions to set car year and make.
myCar.setYear(2013);
myCar.setMake("Mustang GT");
// call member functions to get car year and make
cout << "Your car's current year is: " << myCar.getYear() << endl;
cout << "Your car's current make is: " << myCar.getMake;
cout << "Your car's current speed is : " << speed << " mph" << endl;
return 0;
}
这是Car.h
#pragma once
#ifndef CAR_H
#define CAR_H
// Car class declaration
class Car
{
private:
string make;
int year;
int speed;
public:
int myCar(int year, string make, int speed);
int setYear(int carYear);
int setSpeed(int carSpeed);
string setMake(string carMake);
int getYear();
int getSpeed();
string getMake();
void accelerate();
void brake();
};
这是Car.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include "Car.h"
using namespace std;
//***
//myCar
//**
int Car::myCar(int year, string make, int speed)
{
int setYear(int year);
string setMake(string make);
return year, make, speed;
}
//**********
// setYear
//*********
int Car::setYear(int carYear)
{
year = carYear;
return carYear;
}
//************
// setMake
//************
string Car::setMake(string carMake)
{
make = carMake;
}
//***
//***
//setSpeed
//***
int Car::setSpeed(int carSpeed)
{
speed = carSpeed;
return carSpeed;
}
//***
//***
//getYear
//***
int Car::getYear()
{
return year;
}
//***
//***
//getMake
//***
string Car::getMake()
{
return make;
}
//***
//getSpeed
//***
int Car::getSpeed()
{
return speed;
}
//***
//***
// Accelerate
//***
void Car::accelerate()
{
int speed = 0;
speed += 5;
}
//***
//***
// Brake
//***
void Car::brake()
{
int speed = 0;
speed -= 5;
}
//***
答案 0 :(得分:0)
在setMake
函数中,它被声明为字符串但不返回任何值。我相信你忘了回复过去的价值。
//************
// setMake
//************
string Car::setMake(string carMake)
{
make = carMake;
return make; // This line should do the trick
}
我建议下次仔细查看编译器输出的内容,因为它可能会给你一个行号,甚至是问题的函数名称。
旁注:通常设置的函数声明为void,因为它们通常不需要返回任何内容。