supraincarcare operatori stream

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
stelian_2681
Junior
Junior
Posts: 36
Joined: 24 Jun 2013, 23:25
Judet: Olt

supraincarcare operatori stream

Post by stelian_2681 » 13 May 2017, 21:24

In codul urmator primesc erori de tipul firstName is private, ....
Daca acest cod il rulez fara clase template, codul ruleaza perfect, in schimb daca implementez clasele template codul nu mai ruleaza.
De ce ?

Code: Select all

#include <iostream>
#include <cstring>
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;

template <class T, class U, class V> class Persoana
{
private:
    T *firstName;
    T *secondName;
    T *job;
    T *email;
    U age;
    V phone;
public:
    Persoana()
    {
        this->firstName = NULL;
        this->secondName = NULL;
        this->job = NULL;
        this->email = NULL;
        this->age = 0;
        this->phone = "";
    }
    Persoana(T firstName[], T secondName[], T job[], T email[], U age, V phone)
    {

        this->firstName = new T[strlen(firstName)+1];
        strcpy(this->firstName, firstName);

        this->secondName = new T[strlen(secondName)+1];
        strcpy(this->secondName, secondName);

        this->job = new T[strlen(job)+1];
        strcpy(this->job, job);

        this->email = new T[strlen(email)+1];
        strcpy(this->email, email);

        this->age = age;

        this->phone = phone;
    }
    ~Persoana()
    {
        if (this->firstName != NULL)
            delete[] firstName;
        firstName = NULL;
        if (this->secondName != NULL)
            delete[] secondName;
        secondName = NULL;
        if (this->job != NULL)
            delete[] job;
        job = NULL;
        if (this->email != NULL)
            delete[] email;
        email = NULL;
    }
    T *getFirstName()
    {
        return firstName;
    }
    T *getSecondName()
    {
        return secondName;
    }
    T *getJob()
    {
        return job;
    }
    T *getEmail()
    {
        return email;
    }
    U getAge()
    {
        return age;
    }
    V getPhone()
    {
        return phone;
    }
    template <class X, class Y, class Z> friend istream& operator>> (istream& in, Persoana<X,Y,Z> &box);
    template <class X, class Y, class Z> friend ostream& operator<< (ostream& out, const Persoana<X,Y,Z> &box);

};

template <class X, class Y, class Z> istream& operator>> (istream& in, Persoana<X,Y,Z> &obj)
{
    char temp[50];

    cout << "\n Firstname: ";
    if (obj.firstName != NULL)
        delete[] obj.firstName;
    in >> temp;
    obj.firstName = new char[strlen(temp)+1];
    strcpy(obj.firstName, temp);


    cout << "\n Secondname: ";
    if (obj.secondName != NULL)
        delete[] obj.secondName;
    in >> temp;
    obj.secondName = new char[strlen(temp)+1];
    strcpy(obj.secondName, temp);

    cout << "\n Age: ";
    in >> obj.age;

    cout << "\n Job: ";
    if (obj.job != NULL)
        delete[] obj.job;
    in >> temp;
    obj.job = new char[strlen(temp)+1];
    strcpy(obj.job, temp);

    cout << "\n Email: ";
    if (obj.email != NULL)
        delete[] obj.email;
    in >> temp;
    obj.email = new char[strlen(temp)+1];
    strcpy(obj.email, temp);

    cout << "\n Phone: ";
    in >> obj.phone;

    return in;
}

template <class X, class Y, class Z> ostream& operator<< (ostream& out, Persoana<X,Y,Z> &obj)
{
    out << "\n Firstname: " << obj.firstName << endl;
    out << "\n Secondname: " << obj.secondName << endl;
    out << "\n Age: " << obj.age << endl;
    out << "\n Job: " << obj.job << endl;
    out << "\n Email: " << obj.email << endl;
    out << "\n Phone: " << obj.phone << endl;
    return out;
}

int main()
{
    Persoana<char, int, string> obj;
    cin >> obj;
    cout << obj;
    return 0;
}



Viorel
Microsoft MVP
Microsoft MVP
Posts: 286
Joined: 13 Jul 2007, 12:26

Re: supraincarcare operatori stream

Post by Viorel » 14 May 2017, 10:40

Adaugă const:

Code: Select all

template <class X, class Y, class Z> 
ostream & operator << ( ostream & out, const Persoana<X,Y,Z> & obj )
{
   . . .
}

Post Reply