方法错误“并非所有控制路径都返回值”和方法未返回值[关闭]
Posted
技术标签:
【中文标题】方法错误“并非所有控制路径都返回值”和方法未返回值[关闭]【英文标题】:Method error "not all control paths return a value" and Method not returning a value [closed] 【发布时间】:2020-06-27 14:29:52 【问题描述】:我的代码出现以下错误:
PoliceOfficer.cpp(39):警告 C4715:'PoliceOfficer::patrol':并非所有控制路径都返回值
ParkingTicket.cpp(54):错误 C4716:“GetCarInfo”:必须返回一个值
我已将大部分 PoliceOfficer::patrol 注释掉,以便更容易排除故障。不知道我做错了什么。我已经包含了我所有的文件,以防万一有帮助
Car.h
#pragma once
#ifndef CAR_H
#define CAR_H
#include <string>
using namespace std;
// Car class contains the information about a car
class Car
private:
string make, model, color, licenseNumber;
public:
// Default Constructor
Car();
// Accessors
string getMake();
string getModel();
string getColor();
string getLicense();
// Mutators
void setMake(string);
void setModel(string);
void setColor(string);
void setLicense(string);
// Overload << operator
friend ostream& operator << (ostream& strm, const Car& obj);
;
#endif // CAR_H
Car.cpp
#include "Car.h"
#include <iostream>
#include <string>
using namespace std;
// Default Constructor
Car::Car()
make = "None Set";
model = "None Set";
color = "None Set";
licenseNumber = "None Set";
// Accessors
string Car::getMake()
cout << "Make: " << make;
return make;
string Car::getModel()
cout << "Model: " << model;
return model;
string Car::getColor()
cout << "Color: " << color;
return color;
string Car::getLicense()
cout << "License: " << licenseNumber;
return licenseNumber;
// Mutators
void Car::setMake(string ma)
make = ma;
void Car::setModel(string mo)
model = mo;
void Car::setColor(string c)
color = c;
void Car::setLicense(string l)
licenseNumber = l;
// Overload << operator
// Author note: I'm not sure what I am supposed to be overloading
// this function with.
ostream& operator << (ostream& strm, const Car& obj)
return strm;
ParkedCar.h
#pragma once
#ifndef PARKEDCAR_H
#define PARKEDCAR_H
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
// ParkedCar class simulates a parked car. Knows which type of car is parked and
// the number of minutes that the car has been parked
class ParkedCar
private:
// Private Members
// class Car;
Car carObject;
string make;
string model;
string color;
string licenseNumber;
int minutesParked;
public:
// Constructors
ParkedCar();
ParkedCar(string ma, string mo, string c, string l, int mp);
// Copy Constructor
ParkedCar(ParkedCar& obj);
// Accessors
int getMinutesParked() const;
string getMake();
string getModel();
string getColor();
string getLicense();
// Mutators
int setMinutesParked(int);
void setMake(string ma);
void setModel(string mo);
void setColor(string c);
void setLicense(string l);
;
#endif // PARKEDCAR_H
ParkedCar.cpp
#include "ParkedCar.h"
#include <iostream>
#include <string>
using namespace std;
ParkedCar::ParkedCar() // Default Constructor
minutesParked = 0;
// make, model, color, licenseNumber;
ParkedCar::ParkedCar(string ma, string mo, string c, string l, int mp) // Constructor with 5 parameters
carObject.setMake(ma);
carObject.setModel(mo);
carObject.setColor(c);
carObject.setLicense(l);
minutesParked = mp;
// Copy Constructor
ParkedCar::ParkedCar(ParkedCar& obj)
carObject.setMake(obj.make);
carObject.setModel(obj.model);
carObject.setColor(obj.color);
carObject.setLicense(obj.licenseNumber);
minutesParked = obj.minutesParked;
// Accessors
string ParkedCar::getMake()
return make;
string ParkedCar::getModel()
return model;
string ParkedCar::getColor()
return color;
string ParkedCar::getLicense()
return licenseNumber;
int ParkedCar::getMinutesParked() const
return minutesParked;
// Mutators
void ParkedCar::setMake(string ma)
make = ma;
void ParkedCar::setModel(string mo)
model = mo;
void ParkedCar::setColor(string c)
color = c;
void ParkedCar::setLicense(string l)
licenseNumber = l;
int ParkedCar::setMinutesParked(int mp)
minutesParked = mp;
return 0;
ParkingMeter.h
#pragma once
#ifndef PARKINGMETER_H
#define PARKINGMETER_H
#include <iostream>
#include <string>
using namespace std;
class ParkingMeter
private:
// Private Members
int minutes; // Minutes purchased for parking
public:
// Constructors
ParkingMeter();
ParkingMeter(int);
// Accessors
int getMinutes();
// Mutators
int setMinutes(int);
;
#endif // PARKINGMETER_H
ParkingMeter.cpp
#include "ParkingMeter.h"
#include <iostream>
#include <string>
using namespace std;
// Default Constructor
ParkingMeter::ParkingMeter()
minutes = 0;
// Constructor with 1 parameter
ParkingMeter::ParkingMeter(int i)
minutes = i;
// Accessor
int ParkingMeter::getMinutes()
return minutes;
// Mutator
int ParkingMeter::setMinutes(int m)
minutes = m;
return 0;
// Overload << operator
const ostream& operator << (ostream& strm, const ParkingMeter& obj)
return strm;
PoliceOfficer.h
#include "ParkingMeter.h"
#include <iostream>
#include <string>
using namespace std;
// Default Constructor
ParkingMeter::ParkingMeter()
minutes = 0;
// Constructor with 1 parameter
ParkingMeter::ParkingMeter(int i)
minutes = i;
// Accessor
int ParkingMeter::getMinutes()
return minutes;
// Mutator
int ParkingMeter::setMinutes(int m)
minutes = m;
return 0;
// Overload << operator
const ostream& operator << (ostream& strm, const ParkingMeter& obj)
return strm;
PoliceOfficer.cpp
#include "PoliceOfficer.h"
#include <iostream>
#include <string>
using namespace std;
//Default Constructor
PoliceOfficer::PoliceOfficer()
ParkingTicket* ticketPtr = nullptr;
officerName = " ";
badgeNumber = " ";
// Constructor with 2 elements
PoliceOfficer::PoliceOfficer(string n, string b)
officerName = n;
badgeNumber = b;
ParkingTicket *PoliceOfficer::patrol(ParkedCar car, ParkingMeter meter)
parkedMinutes = car.getMinutesParked();
cout << "Parked Minutes: " << parkedMinutes << endl;
meterMinutes = meter.getMinutes();
cout << "Meter Minutes: " << meterMinutes << endl;
minutes = parkedMinutes - meterMinutes;
cout << "Minutes: " << minutes << endl;
if (minutes <= 0)
return nullptr;
else
ParkingTicket ticket(car, minutes);
;
// Accessors
string PoliceOfficer::getOfficerName()
return officerName;
string PoliceOfficer::getBadgeNumber()
return badgeNumber;
// Mutators
string PoliceOfficer::setOfficerName(string n)
officerName = n;
return 0;
string PoliceOfficer::setBadgeNumber(string b)
badgeNumber = b;
return 0;
// Overload << operator
const ostream& operator << (ostream& out, const PoliceOfficer& obj)
out << "Officer: " << &PoliceOfficer::getOfficerName;
out << " (badge #" << &PoliceOfficer::getBadgeNumber;
out << ")" << endl;
return out;
ParkingTicket.h
#pragma once
#ifndef PARKINGTICKET_H
#define PARKINGTICKET_H
#include <iostream>
#include <string>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "Car.h"
using namespace std;
// ParkingTicket class simulates a parking ticket
// Reports the car information for illegally parked car
// Reports the fine which is $25 for the first hour or part
// of an hour that the car is illegally parked, plus $10
// for every additional hour or part of an hour
class ParkingTicket
private:
// Private Members
// class ParkedCar;
double fine; // The calculated parking fine
double* fineptr = &fine; // fine pointer
int minutes; // The minutes illegally parked
const int firstHour = 25; // first hour fine
const int subHours = 10; // subsiquent hour fine
// Car Info
string make, model, color, licenseNumber;
public:
// Constructors
ParkingTicket();
ParkingTicket(ParkedCar car, int meter);
// Accessors
double getFine();
int getMinutes();
// Mutators
double setFine(double);
int setMinutes(int);
// Method
double fineCalc(); // calculate the fine
string GetCarInfo();
// friend const ostream& operator << (ostream&, const ParkingTicket&);
;
const ostream& operator << (ostream&, const ParkingTicket&);
#endif // PARKINGTICKET_H
ParkingTicket.cpp
#include "ParkingTicket.h"
#include <iostream>
#include <string>
using namespace std;
ParkingTicket::ParkingTicket() // Default Constructor
double fine = 0;
double* fineptr = &fine;
int minutes = 0;
const int firstHour = 25;
const int subHours = 10;
// Constructor with 2 parameters (ParkedCar and an integer)
ParkingTicket::ParkingTicket(ParkedCar car, int min)
minutes = min; // illegally parked minutes
cout << "Minutes: " << endl;
ParkingTicket::fineCalc(); // calculate fine
cout << fine;
// Accessors
double ParkingTicket::getFine()
return fine;
int ParkingTicket::getMinutes()
return minutes;
// Mutators
double ParkingTicket::setFine(double f)
fine = f;
return 0;
int ParkingTicket::setMinutes(int m)
minutes = m;
return 0;
string GetCarInfo()
&Car::getMake;
//&Car::getModel;
//&Car::getColor;
//&Car::getLicense;
// Car information, Time on the meter
double ParkingTicket::fineCalc()
fine = 25;
minutes -= 60;
while (minutes >= 60)
fine += subHours;
minutes -= 60;
if (minutes >= 0)
fine += subHours;
return fine;
// Overload << operator
const ostream& operator << (ostream& out, const ParkingTicket& obj)
out << &ParkingTicket::GetCarInfo;
return out;
main.cpp
#include <iostream>
#include <string>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "ParkingTicket.h"
#include "PoliceOfficer.h"
using namespace std;
int main()
ParkingTicket *ticket = nullptr;
ParkedCar car("Volkswagen", "1972", "Red", "147RHZM", 125);
ParkingMeter meter(60);
PoliceOfficer officer("Joe Friday", "4788");
ticket = officer.patrol(car, meter);
if (ticket != nullptr)
cout << "we made it this far officer" << endl;
cout << officer;
cout << "we are at the ticket" << endl;
cout << *ticket;
delete ticket;
ticket = nullptr;
else
cout << "No crimes were committed.\n";
return 0;
【问题讨论】:
你为policeofficer.h发布了parkingmeter.cpp 这比说明问题所需的代码要多得多。请阅读minimal reproducible example。 Setter 不应该返回任何东西,并且从返回std::string
的函数返回 0
会导致未定义的行为。
【参考方案1】:
GetCarInfo 应该返回一个字符串,但它没有:
string GetCarInfo()
&Car::getMake;
因此出现错误。
GetCarInfo
是ParkingTicket
中的一个函数,因此您还需要在定义中指定它。但我不确定你打算在这里返回什么。您在上下文中没有 Car
。
也许是这样的?
string ParkingTicket::GetCarInfo()
return make; //make is a private member of the class.
反正&Car::getMake;
的语法是一个方法函数指针,指向Car
类中的一个方法。方法函数指针有点奇怪,所以如果你是编程新手,你可能不是有意使用它。如果你打算在Car
中调用getMake
方法,你应该有一个Car
类型的对象来调用:
Car car;
car.getMake();
请注意,PoliceOfficer::patrol
中缺少返回语句仅 会发出 警告(与 GetCarInfo()
中缺少返回的错误相比)。如果您让函数结束而不返回(任何事情都可能发生!),那将是 UB(未定义行为)。因此,您正在寻找警告并希望修复它们,这很好!解决方法是添加 return 语句:
ParkingTicket PoliceOfficer::patrol(ParkedCar car, ParkingMeter meter)
if (minutes <= 0)
return nullptr;
else
return ticket(car, minutes);
;
【讨论】:
【参考方案2】:您只是在这里错过了一个退货声明:
if (minutes <= 0)
return nullptr;
else
ParkingTicket ticket(car, minutes);
// you must return something. I guess it is return ParkingTicket(...
;
同样的事情:
string GetCarInfo()
&Car::getMake;
//&Car::getModel;
//&Car::getColor;
//&Car::getLicense;
您将 GetCarInfo() 声明为一个字符串,您需要返回一个字符串。
【讨论】:
以上是关于方法错误“并非所有控制路径都返回值”和方法未返回值[关闭]的主要内容,如果未能解决你的问题,请参考以下文章