与“运营商>>”不匹配

no match for ‘operator>>’

我正在尝试实现朋友 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;
}

因为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);

而且,在任何需要获取或设置这些字段值的地方,都应该使用间接寻址。阅读有关指针及其使用的信息(例如here or here


而且我认为您不需要在 class 中使用指针。很容易出错!直接使用数据即可,ie:

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

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

您将没有内存可以自己管理,因此可能出现的错误更少。


但如果你真的需要它,请查看 smart pointer which respect RAIIstd::shared_pointerboost::shared_pointerstd::unique_pointerboost::unique_pointer