-2

I've had to make a program which shows the name, surname and the year of birth of a child. Everything is working well, excepting the friend function, which have to access the private/protected variable birth_year and to show 0 if his birth_year is bigger than 2007, else to show 1.

Here's the code:

#include <iostream>
#include <string.h>

using namespace std;

class child{
protected:

    char name[20],surname[20];
    int birth_year;
public:

    child(char*,char*,int);
    getyear();
    show();
    friend void itsolder();
};

child::child(char* n, char* p, int a)
{

    strcpy(name,n);
    strcpy(surname,p);
    birth_year=a;
}

child::getyear()
{

    return birth_year;
}

child::show()
{

    cout << "Name: " << name << endl;
    cout << "Surname: " << surname << endl;
    cout << "Year of birth: " << birth_year << endl;
}

void itsolder(child&year)
{

    if (year.birth_year>2007)
        cout << "0" << endl;
    else
        cout << "1" << endl;
}

int main()
{

    child c1("Richard", "John", 1997);
    c1.show();
    cout << c1.getyear() << endl;
    itsolder(c1.birth_year)
    return 0;
}

Here are the errors:

  1. int child::birth_year is protected;
  2. "within this context" - is where I've put the condition in the friend function and where I called it, into main();
  3. invalid initialization of reference of type child& from expression of type int
Stargateur
  • 24,473
  • 8
  • 65
  • 91
system32
  • 15
  • 5
  • `child::getyear()` is an error (if this is meant to introduce a function body, there must be a return type specified beforehand) – M.M Jun 27 '17 at 08:04

2 Answers2

2

A declaration of friend void itsolder(); doesn't match a definition of

void itsolder(child&year)
{
    if (year.birth_year>2007)
        cout << "0" << endl;
    else
        cout << "1" << endl;
}

Change it to friend void itsolder(child&); and pass the parameter accordingly:

itsolder(c1);
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

I agree with what @StoryTeller has written in his answer, but in addition to that your program is invalid since C++11. String literals are of type const char[n], which decays into const char*, but by default compilers like g++ and clang++ accepts this code for backward compatibility reasons. If you use -pedantic-errors command line option, you will get compiler error.

Addition to this, return type of getyear() and show() member function is missing. You must explicitly specify return type of each and every function.

See live demo here. Observe the warnings given by the compiler:

g++ -O2 -std=c++11 -Wall -Wextra   main.cpp && ./a.out

main.cpp: In function 'int main()':

main.cpp:53:37: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

     child c1("Richard", "John", 1997);

                                     ^

main.cpp:53:37: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

So your correct code should be like this.

ninja
  • 3
  • 4