Creating a query function receiving an unmodifiable reference

Hi forum,

I need to create a bool function that checks to see if an object referenced is compatible (more below) with the current object.

So, here I have a Passenger instance called David,

Passenger david("David", "Toronto", 2018, 4, 20);

I've also created a few other passengers,
1
2
3
4
5
6
7
8
9
	Passenger friends[] = {
		Passenger("Vanessa", "Toronto", 2018, 4, 20),
		Passenger("John", "Toronto", 2018, 4, 20),
		Passenger("Alice", "Toronto", 2018, 4, 20),
		Passenger("Bob", "Paris", 2018, 1, 20),
		Passenger("Jennifer", "Toronto", 2018, 4, 20),
		Passenger("Mike", "Toronto", 2018, 4, 20),
		Passenger("Sarah", "Toronto", 2018, 4, 20)
	};


and my constructor is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	Passenger::Passenger(const char* nameString, const char* destinationString, const int yy, const short mm, const short dd)
	{
		if (nullptr == nameString || nullptr == destinationString
			|| '\0' == nameString || '\0' == destinationString
			|| (2017 > yy || 2020 < yy)
			|| (1 > mm || 12 < mm)
			|| (1 > dd || 31 < dd))
		{
			setEmpty();
		}
		else
		{
			strcpy(name, nameString);
			strcpy(destination, destinationString);
			year = yy;
			month = mm;
			day == dd;
		}
	}


where setEmpty() is defined as
1
2
3
4
5
6
7
8
	void Passenger::setEmpty()
	{
		name[0] = '\0';
		destination[0] = '\0';
		year = 0;
		month = 0;
		day = 0;
	}


I have a boolean function with the declaration,
 
bool Passenger::canTravelWith(const Passenger&) const


This should allow me to run something along the lines of,

1
2
3
4
    for (int i = 0; i < 7; ++i) {
        if (david.canTravelWith(friends[i]))
            cout << david.nameOf() << " can travel with " << friends[i].nameOf() << endl;
    }


I have defined the function as such:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	bool Passenger::canTravelWith(const Passenger&) const
	{
		for (int i = 0; i < strlen(this->destination); i++)
		{
			if (this->destination[i] == Passenger::destination[i])
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}


The issue is that is returns true in every instance. I have always struggled between the difference and implementation of references, pointers and addresses-of. Can someone please explain to me what is wrong with my canTravelWith() function? TIA (please note, the indicated prototype is something that cannot be changed for the purposed of my assignment).
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Passenger::canTravelWith( const Passenger& another ) const
{
    int i = 0 ;
    // for each character position till we reach the end of either one of the two cstrings
    for( ; this->destination[i] != 0 && another.destination[i] != 0 ; ++i )
    {
        // return false if there is a mismatch at this character position
        if( this->destination[i] != another.destination[i] ) return false ;
    }

    // return true if we have reached the end of both strings without a mismatch
    return this->destination[i] == another.destination[i] ; 
}


Or the shorter version:
1
2
3
4
bool Passenger::canTravelWith( const Passenger& another ) const
{
    return std::strcmp( this->destination[i], another.destination[i] ) == 0 ;
}
Hi JLBorges,

Thanks for the reply. VS runs the code just fine and I get the desired output, however I also get the error

warning: control reaches end of non-void function [-Wreturn-type],
referring to the bool Passenger::canTravelWith( const Passenger& another ) const function and Im not sure how to fix it.. Any ideas?

Here's my implementation (i forgot to mention that the date must also be the same)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	bool Passenger::canTravelWith(const Passenger& another) const
	{		
		for (int i = 0; '\0' != this->destination[i] && '\0' != another.destination[i]; ++i)
		{
			if ((this->destination[i] == another.destination[i])
				&& this->year == another.year
				&& this->month == another.month
				&& this->day == another.day)
			{
				return this->destination[i] == another.destination[i];
			}
			else
			{
				return false;
			}
		}
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
bool Passenger::canTravelWith(const Passenger& another) const
{
    // return true if 
    return ( std::strcmp( this->destination, another.destination ) == 0 ) // destinations are the same
                && this->year == another.year // and the year is the same 
                && this->month == another.month // and the month is the same
                && this->day == another.day ; // and the day is the same
    
    // ie. return false if even one of these conditions do not hold        
}
Youre a genius, my friend, thank you for the help.
Topic archived. No new replies allowed.