In: Computer Science
(C++) I'm getting a few errors that prevents the program from running what is causing this?
"
routes3.cpp:202:9: warning: add explicit braces to avoid dangling else
[-Wdangling-else]
else {
^
"
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cstring>
using namespace std;
class Leg{
const char* const startCity;
const char* const endCity;
const int dist;
public:
Leg(const char* const, const char* const, int);
Leg& operator= (const Leg&);
int getDist() const {return dist;}
void output(ostream&) const;
friend class Route;
friend class ShortestRoute;
};
class Route{
friend class ShortestRoute;
vector<const Leg*> legs;
const int dist;
public:
Route(const Leg&);
Route(const Route&, const Leg&);
Route& operator=(const Route&);
int getDistance() const {return dist;}
void output(ostream&) const;
friend bool operator<(const Route&, const Route&);
};
class ShortestRoute
{
static const int legSize;
static const Leg legs[];
public:
static const Route anyRoute(const char* const, const char*
const);
static const Route shortestRoute(const char* const, const char*
const);
};
const Leg ShortestRoute::legs[] =
{
Leg("Denver", "Kansas City", 395),
Leg("Philadelphia", "New York", 96),
Leg("Oklahoma City", "Kansas City", 347),
Leg("San Jose", "Las Vegas", 382),
Leg("San Fransisco", "San Jose", 42),
Leg("San Jose", "Los Angeles", 306),
Leg("Albuquerque", "Dallas", 646),
Leg("Pittsburgh", "New York", 371),
Leg("Los Angeles", "Phoenix", 372),
Leg("Phoenix", "Albuquerque", 421),
Leg("Las Vegas", "Albuquerque", 573),
Leg("Sacramento", "Reno", 138),
Leg("Memphis", "Atlanta", 383),
Leg("Albuquerque", "Kansas City", 617),
Leg("Albuquerque", "Oklahoma City", 541),
Leg("Oklahoma City", "St Louis", 498),
Leg("Cincinnati", "Columbus", 102),
Leg("Cincinnati", "Cleveland", 217),
Leg("Oklahoma City", "Memphis", 467),
Leg("Dallas", "Memphis", 451),
Leg("Richmond", "Washington", 109),
Leg("Chicago", "Indianapolis", 183),
Leg("Indianapolis", "Cincinnati", 115),
Leg("Los Angeles", "Las Vegas", 263),
Leg("Los Angeles", "Albuquerque", 789),
Leg("Louisville", "Cincinnati", 100),
Leg("St Louis", "Indianapolis", 243),
Leg("Washington", "Baltimore", 39),
Leg("Baltimore", "Philadelphia", 105),
Leg("Las Vegas", "Denver", 754),
Leg("Las Vegas", "Salt Lake City", 420),
Leg("Reno", "Salt Lake City", 518),
Leg("Salt Lake City", "Denver", 519),
Leg("Kansas City", "Chicago", 510),
Leg("Memphis", "Louisville", 383),
Leg("San Fransisco", "Sacramento", 90),
Leg("Memphis", "Nashville", 210),
Leg("Kansas City", "St Louis", 249),
Leg("Nashville", "Charlotte", 409),
Leg("Atlanta", "Charlotte", 226),
Leg("Detroit", "Cleveland", 169),
Leg("Cleveland", "Pittsburgh", 134),
Leg("Louisville", "Richmond", 563),
Leg("Pittsburgh", "Washington", 241),
Leg("Pittsburgh", "Philadelphia", 304),
Leg("Chicago", "Detroit", 283),
Leg("Columbus", "Pittsburgh", 142),
Leg("Charlotte", "Richmond", 291),
//SF to NY SuperHighway
Leg("San Fransisco", "New York", 21000)
};
int main(){
cout << endl << "Programmer: \n";
cout << "Course: \n";
const Route route1 = ShortestRoute::anyRoute("San Francisco",
"New York City");
route1.output(cout);
const Route route2 = ShortestRoute::shortestRoute("San
Francisco", "New York City");
route2.output(cout);
return 0;
}
const Route ShortestRoute::anyRoute(const char* const start,
const char* const end) {
for (int i = 0; i < ShortestRoute::legSize; i++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0) {
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route
x(ShortestRoute::anyRoute(start,ShortestRoute::legs[i].startCity),legs[i]);
return x;
}
}
}
throw "Can't find a possible route!";
}
const Route ShortestRoute::shortestRoute(const char* const
start, const char* const end){
set<Route> s;
for (int i = 0; i < ShortestRoute::legSize; i++) {
for (int j = 0; j < ShortestRoute::legSize; j++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0)
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route x(ShortestRoute::anyRoute(start,
ShortestRoute::legs[i].startCity), legs[i]);
s.insert(x);
}
}
}
return *(s.begin());
}
bool operator<(const Route& a, const Route& b)
{
return a.dist < b.dist;
}
Leg& Leg::operator=(const Leg& copy){
Leg& host = *this;
if(this != ©){
const_cast<const char*&>(host.startCity) =
copy.startCity;
const_cast<const char*&>(host.endCity) =
copy.endCity;
const_cast<int&>(host.dist) = copy.dist;
}
return host;
}
void Leg::output(ostream& os) const{
os << startCity << " to " << endCity << "
is " << dist << " miles" << endl;
}
Route::Route(const Leg& leg) : dist(leg.getDist()){
legs.push_back(&leg);
}
Route::Route(const Route& route, const Leg& leg)
: legs(route.legs), dist(route.dist+leg.dist){
if (strcmp(route.legs.back()->endCity, leg.startCity) !=
0)
throw "Mismatch";
legs.push_back(&leg);
}
void Route::output(ostream& os) const{
string output = "Route: ";
string temp = " to ";
int totalDist = 0;
for(int i = 0; i < legs.size(); i++){
output += legs[i]->startCity;
if(i != legs.size()-1) {
output += temp;
}
else{
output += (temp + legs[i]->endCity);
}
totalDist += legs[i]->getDist();
}
output += (" "+to_string(totalDist)+" miles\n");
os << output;
}
Route& Route::operator=(const Route& copy){
Route& host = *this;
if(this != ©){
const_cast<int&>(host.dist) = copy.getDistance();
host.legs = copy.legs;
}
return host;
}
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cstring>
using namespace std;
class Leg{
const char* const startCity;
const char* const endCity;
const int dist;
public:
Leg(const char*, const char*,
int);
Leg& operator= (const Leg&);
int getDist() const {return dist;}
void output(ostream&) const;
friend class Route;
friend class ShortestRoute;
};
Leg::Leg(const char* s, const char* e, int d):
startCity(s), endCity(e), dist(d) {
}
class Route{
friend class ShortestRoute;
vector<const Leg*> legs;
const int dist;
public:
Route(const Leg&);
Route(const Route&, const Leg&);
Route& operator=(const Route&);
int getDistance() const {return dist;}
void output(ostream&) const;
friend bool operator<(const Route&, const Route&);
};
class ShortestRoute
{
static const int legSize;
static const Leg legs[];
public:
static const Route anyRoute(const char* const, const char*
const);
static const Route shortestRoute(const char* const, const char*
const);
};
const Leg ShortestRoute::legs[] =
{
Leg("Denver", "Kansas City", 395),
Leg("Philadelphia", "New York", 96),
Leg("Oklahoma City", "Kansas City", 347),
Leg("San Jose", "Las Vegas", 382),
Leg("San Fransisco", "San Jose", 42),
Leg("San Jose", "Los Angeles", 306),
Leg("Albuquerque", "Dallas", 646),
Leg("Pittsburgh", "New York", 371),
Leg("Los Angeles", "Phoenix", 372),
Leg("Phoenix", "Albuquerque", 421),
Leg("Las Vegas", "Albuquerque", 573),
Leg("Sacramento", "Reno", 138),
Leg("Memphis", "Atlanta", 383),
Leg("Albuquerque", "Kansas City", 617),
Leg("Albuquerque", "Oklahoma City", 541),
Leg("Oklahoma City", "St Louis", 498),
Leg("Cincinnati", "Columbus", 102),
Leg("Cincinnati", "Cleveland", 217),
Leg("Oklahoma City", "Memphis", 467),
Leg("Dallas", "Memphis", 451),
Leg("Richmond", "Washington", 109),
Leg("Chicago", "Indianapolis", 183),
Leg("Indianapolis", "Cincinnati", 115),
Leg("Los Angeles", "Las Vegas", 263),
Leg("Los Angeles", "Albuquerque", 789),
Leg("Louisville", "Cincinnati", 100),
Leg("St Louis", "Indianapolis", 243),
Leg("Washington", "Baltimore", 39),
Leg("Baltimore", "Philadelphia", 105),
Leg("Las Vegas", "Denver", 754),
Leg("Las Vegas", "Salt Lake City", 420),
Leg("Reno", "Salt Lake City", 518),
Leg("Salt Lake City", "Denver", 519),
Leg("Kansas City", "Chicago", 510),
Leg("Memphis", "Louisville", 383),
Leg("San Fransisco", "Sacramento", 90),
Leg("Memphis", "Nashville", 210),
Leg("Kansas City", "St Louis", 249),
Leg("Nashville", "Charlotte", 409),
Leg("Atlanta", "Charlotte", 226),
Leg("Detroit", "Cleveland", 169),
Leg("Cleveland", "Pittsburgh", 134),
Leg("Louisville", "Richmond", 563),
Leg("Pittsburgh", "Washington", 241),
Leg("Pittsburgh", "Philadelphia", 304),
Leg("Chicago", "Detroit", 283),
Leg("Columbus", "Pittsburgh", 142),
Leg("Charlotte", "Richmond", 291),
//SF to NY SuperHighway
Leg("San Fransisco", "New York", 21000)
};
int main(){
cout << endl << "Programmer: \n";
cout << "Course: \n";
const Route route1 = ShortestRoute::anyRoute("San Francisco", "New
York City");
route1.output(cout);
const Route route2 = ShortestRoute::shortestRoute("San Francisco",
"New York City");
route2.output(cout);
return 0;
}
const Route ShortestRoute::anyRoute(const char* const start, const
char* const end) {
int SIZE =
sizeof(ShortestRoute::legs)/sizeof(ShortestRoute::legs[0]);
for (int i = 0; i < SIZE; i++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0)
{
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route
x(ShortestRoute::anyRoute(start,ShortestRoute::legs[i].startCity),legs[i]);
return x;
}
}
}
throw "Can't find a possible route!";
}
const Route ShortestRoute::shortestRoute(const char* const start,
const char* const end){
int SIZE =
sizeof(ShortestRoute::legs)/sizeof(ShortestRoute::legs[0]);
set<Route> s;
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE; i++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0) {
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route x(ShortestRoute::anyRoute(start,
ShortestRoute::legs[i].startCity), legs[i]);
s.insert(x);
}
}
}
}
return *(s.begin());
}
bool operator<(const Route& a, const Route& b)
{
return a.dist < b.dist;
}
Leg& Leg::operator=(const Leg& copy){
Leg& host = *this;
if(this != ©){
const_cast<const char*&>(host.startCity) =
copy.startCity;
const_cast<const char*&>(host.endCity) =
copy.endCity;
const_cast<int&>(host.dist) = copy.dist;
}
return host;
}
void Leg::output(ostream& os) const{
os << startCity << " to " << endCity << "
is " << dist << " miles" << endl;
}
Route::Route(const Leg& leg) : dist(leg.getDist()){
legs.push_back(&leg);
}
Route::Route(const Route& route, const Leg& leg)
: legs(route.legs), dist(route.dist+leg.dist){
if (strcmp(route.legs.back()->endCity, leg.startCity) !=
0)
throw "Mismatch";
legs.push_back(&leg);
}
void Route::output(ostream& os) const{
string output = "Route: ";
string temp = " to ";
int totalDist = 0;
for(unsigned int i = 0; i < legs.size(); i++){
output += legs[i]->startCity;
if(i != legs.size()-1) {
output += temp;
}
else{
output += (temp + legs[i]->endCity);
}
totalDist += legs[i]->getDist();
}
output += (" "+to_string(totalDist)+" miles\n");
os << output;
}
Route& Route::operator=(const Route& copy){
Route& host = *this;
if(this != ©){
const_cast<int&>(host.dist) = copy.getDistance();
host.legs = copy.legs;
}
return host;
}
**************************************************
The first warning is because eevry if statement must
have a pair of curly braces after it.. If you avoid it, it becomes
confusing.. Hence the error.. I have corrected it.. The other error
says: "Leg::Leg(char const*, char const*, int)" is not
defined, which means, you have declared a constructor for Leg class
in your class declaration, but never provided its definition in
file...
For you reference,
void output(ostream&) const; => this is a declaration in Leg class..
void Leg::output(ostream& os) const{ => This is he definition of method of Leg class, which is later after declaration
===========================================================
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.