不符合'operator>>'

时间:2016-03-24 10:29:35

标签: c++ oop friend-class

我正在尝试实现朋友类。当我尝试运行代码时,我得到的错误就像。

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;
}

1 个答案:

答案 0 :(得分:2)

由于nameheightweighttelephone_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);

而且,无论您需要获取或设置这些字段的值,您都应该使用间接。阅读有关指针及其使用的指针(例如herehere

我认为你不需要在你的班级中使用指针。这很容易出错!只需直接使用数据

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

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

您将无法自行管理内存,因此可能存在较少的错误。

但如果您确实需要,请查看尊重smart pointerstd::shared_pointerboost::shared_pointerstd::unique_pointerboost::unique_pointer的{​​{3}}