| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1524 人关注过本帖
标题:运载符重载的例子--但不知问题出在哪里
取消只看楼主 加入收藏
GeneralJing
Rank: 2
等 级:论坛游民
帖 子:30
专家分:15
注 册:2011-9-26
收藏
 问题点数:0 回复次数:0 
运载符重载的例子--但不知问题出在哪里
今天学习C++看到了运载符重载,对这个的概念不是很理解,就按照书上的例子写了个代码,但是不能运行。以下是错误提示:
d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber1.cpp(15): error C2248: “PhoneNumber::areaCode”: 无法访问 private 成员(在“PhoneNumber”类中声明)
1>          d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.h(16) : 参见“PhoneNumber::areaCode”的声明
1>          d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.h(12) : 参见“PhoneNumber”的声明
1>d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber1.cpp(17): error C2248: “PhoneNumber::exchange”: 无法访问 private 成员(在“PhoneNumber”类中声明)
1>          d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.h(17) : 参见“PhoneNumber::exchange”的声明
1>          d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.h(12) : 参见“PhoneNumber”的声明
1>d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber1.cpp(19): error C2248: “PhoneNumber::line”: 无法访问 private 成员(在“PhoneNumber”类中声明)
1>          d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.h(18) : 参见“PhoneNumber::line”的声明
1>          d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.h(12) : 参见“PhoneNumber”的声明
1>  PhoneNumber.cpp
1>d:\program files\microsoft visual studio 10.0\练习项目文件夹\phonenumber\phonenumber\phonenumber.cpp(12): error C2678: 二进制“>>”: 没有找到接受“std::istream”类型的左操作数的运算符(或没有可接受的转换)
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1053): 可能是“std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char *)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1060): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,signed char &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1067): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,unsigned char *)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1074): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &&,unsigned char &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1097): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1104): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1111): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1118): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(1128): 或       “std::basic_istream<_Elem,_Traits> &std::operator >><char,std::char_traits<char>,PhoneNumber>(std::basic_istream<_Elem,_Traits> &&,_Ty &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ty=PhoneNumber
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(179): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(185): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(192): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(199): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(218): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(253): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(272): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(298): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(316): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(334): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned long &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(354): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(373): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(392): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(411): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(429): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(447): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\istream(466): 或       “std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)”
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          尝试匹配参数列表“(std::istream, PhoneNumber)”时
1>  正在生成代码...
1>
1>生成失败。
头文件代码如下:
#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <iostream>
using std::ostream;
using std::istream;

#include <string>
using std::string;

class PhoneNumber
{
    friend ostream &operator<<( ostream &, const PhoneNumber & );
    friend istream &operator<<( istream &, PhoneNumber & );
private:
    string areaCode;
    string exchange;
    string line;
};

#endif
成员函数代码如下:

#include <iomanip>
using std::setw;

#include "PhoneNumber.h"

ostream &operator<<( ostream &output, const PhoneNumber &number )
{
    output << "(" << number.areaCode << ")" << number.exchange << "-" << number.line;
    return output;
}

istream &operator>>( istream &input, PhoneNumber &number )
{
    input.ignore();
    input >> setw( 3 ) >> number.areaCode;
    input.ignore( 2 );
    input >> setw( 3 ) >> number.exchange;
    input.ignore();
    input >> setw( 4 ) >> number.line;
    return input;
}
源文件代码如下:

#include <iostream>
using namespace std;

#include "PhoneNumber.h"

int main()
{
    PhoneNumber phone;

    cout << "Enter phone number in the form (123) 456-7890:" << endl;

    cin >> phone;

    cout << "The phone number entered was: ";

    cout << phone << endl;
    return 0;
}
请大侠帮我看看是哪个地方我弄错了,我找了几遍没找到……
搜索更多相关主题的帖子: microsoft 文件夹 private visual 
2011-10-24 15:46
快速回复:运载符重载的例子--但不知问题出在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028104 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved