2016-03-24 3 views
-2

Я пытаюсь реализовать класс друзей. Когда я пытаюсь запустить код, я получаю такие ошибки.нет совпадения для 'operator >>'

a3.cpp:85:5: error: `no match for ‘operator>>’` (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘int*’) 
    cin>>this->telephone_number; 
    ^
a3.cpp:85:5: note: candidates are: 
In file included from /usr/include/c++/4.8/iostream:40:0, 
      from a3.cpp:9: 
/usr/include/c++/4.8/istream:120:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] 
    operator>>(__istream_type& (*__pf)(__istream_type&)) 

Мой код:

#include <iostream> 
#include <string.h> 
using namespace std; 

class PI 
{ 
    private: 
      string *name;        //dynamic allocation 

      static int count;              //static memeber 
    public: 
      PI();         //default constructor 
      //void getdata(); 

      inline static void displaycount()  //inline static member function PI::displaycount() 
      { 
       cout<<"\nCandidate Count: "<<count; 
      }    

      ~PI()         //destructor 
      { 
       cout<<"DESTRUCTOR CALLED"; 
      } 
      PI(PI &);        //copy constructor 
      friend class PI1; 
}; 

class PI1 
{ 
    private: 
      int *height, *weight,*telephone_number; 
    public: 
      PI1(PI &); 
      void getdata(PI &); 
      void putdata(PI &);  


}; 

int PI :: count = 0; 
PI :: PI() 
{ 
    name = new string(); 
    count=count+1; 
} 
PI1 :: PI1(PI) 
{ 
    height = new int; 
    weight = new int; 

    telephone_number = new int; 

    obj.count=obj.count+1; 
} 

void PI1 :: getdata(PI &obj) 
{ 
    //cin.ignore(); 
    cout<<"\nEnter Name: "; 
    getline(cin,obj.name); 

    cout<<"\nEnter Height: "; 
    cin>>this->height; 
    cout<<"\nEnter Weight: "; 
    cin>>this->weight; 

    cout<<"\nEnter Telephone Number: "; 
    cin>>this->telephone_number; 

} 

void PI1 :: putdata(PI &obj) 
{ 

    cout<<"\nName: "<<obj.name; 

    cout<<"\nHeight: "<<this->height; 
    cout<<"\nWeight: "<<this->weight; 

    cout<<"\nTelephone Number: "<<this->telephone_number; 


} 

int main() 
{ 
    PI p; 
    PI1 p1(p); 

    p1.getdata(p); 
    p1.putdata(p); 
    PI :: displaycount(); 
    return 0; 
} 
+2

Почему все указатели? Они бессмысленны. – molbdnilo

+1

Вам действительно нужны указатели – DimChtz

+0

@molbdnilo Я действительно хотел динамически распределять память для членов класса. Вот почему я объявил участников как указатели – daemon7osh

ответ

2

Поскольку name, height, weight и telephone_number являются указателем, вы должны сделать косвенность. Таким образом, вы должны написать:

getline(cin,*(obj.name)); 
cout<<"\nEnter Height: "; 
cin >> *(this->height); 
cout<<"\nEnter Weight: "; 
cin >> *(this->weight); 
cout<<"\nEnter Telephone Number: "; 
cin >> *(this->telephone_number); 

А также, везде вам нужно получить или установить значение этих полей, вы должны использовать окольные. Читайте об указателях и его использовали (here или here, например)


И я думаю, что вам не нужно использовать указатель внутри класса. Это склонно к ошибкам! Просто используйте непосредственно данные, т.е.:

class PI 
{ 
    private: 
      string name; 
.... 

и

class PI1 
{ 
    private: 
     int height, weight, telephone_number; 

У Вас не будет никакой памяти, чтобы управлять собой, тем меньше возможных жука.


Но если вам действительно нужно, взгляните на smart pointer, которые уважают RAII: std::shared_pointer или boost::shared_pointer, std::unique_pointer или boost::unique_pointer

+0

Вы можете предложить лучший способ реализовать класс друзей? – daemon7osh

+1

@PTosh Здесь вы не используете класс друзей, поэтому я не понимаю ваш вопрос. – Garf365

+0

@ Garf365: Похоже, что OP неправильно учили, что этот шаблон _whole_ - необработанные указатели-члены и все - это «класс друга». –

Смежные вопросы