友元类与Pimpl应用报错

nyeboy123 2009-06-19 11:06:37
//CBase类是一个一般的类,而Foreign类要以友元的形式访问CBase中的私有成员
//现在问题是在main中声明的变量CForeign mybase2能访问CBase中私有成员age,而不能访问pImp指向的name和val。
//其中,pImp采用了Pimpl技术,实现隐藏数据成员机制,但就是不懂为什么同样是私有,就不能运行了
//我是在VS2005的控制台下调试运行
//望各位前来指教
//////////////////////////Base.h头文件///////////////////////
#pragma once
#include <iostream>
#include <string>
using namespace std;

class CForeign;
class CBase
{
friend class CForeign;

public:
CBase(void);
~CBase(void);

void DisplayData();

void SetValues(const string& _Str,const int& _Value);
private:
int age;
class CBaseImp;
CBaseImp* pImp;
};

//////////////////////////Base.cpp源文件文件///////////////////////

#include <iostream>
#include <string>


#include "Base.h"
using namespace std;


class CBase::CBaseImp
{
public:
string name;
int val;

CBaseImp()
{
name="";
val=0;
}
};

CBase::CBase(void)
{
pImp=new CBase::CBaseImp();
}

CBase::~CBase(void)
{
delete pImp;
}

void CBase::DisplayData()
{
cout<<pImp->name<<" "<<pImp->val<<" "<<age<<endl;
}

void CBase::SetValues(const string& _Str,const int& _Value)
{
pImp->name=_Str;
pImp->val=_Value;
}
//////////////////////////Foreign.h头文件//////////////////////
#pragma once
#include <iostream>
#include <string>
#include "Base.h"
using namespace std;

class CForeign
{
public:
CForeign(void);
~CForeign(void);
CBase* pBase;


void SetBaseName(const string& _Str);

void SetBaseValue(const int& _Value);

void SetAge(const int& _Age);


};


//////////////////////////Foreign.cpp源文件//////////////////////
#include "Foreign.h"

CForeign::CForeign(void)
{
pBase=new CBase();
}

CForeign::~CForeign(void)
{
delete pBase;
}

void CForeign::SetAge(const int& _Age)
{
pBase->age=_Age;
}

void CForeign::SetBaseName(const string& _Str)
{
//pBase->pImp->name=_Str;// 取消注释,编译不通过
}

void CForeign::SetBaseValue(const int& _Value)
{
//pBase->pImp->val=_Value;// 取消注释,编译不通过
}

//////////////////////////Main.h主文件//////////////////////
#include <iostream>
#include "Base.h"
#include "Foreign.h"

using namespace std;

void main()
{

CForeign mybase2;
mybase2.pBase->DisplayData();
mybase2.SetAge(30);
//mybase2.SetBaseName("Tom");
//mybase2.SetBaseValue(33);
mybase2.pBase->DisplayData();
}
...全文
104 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
nyeboy123 2009-06-19
  • 打赏
  • 举报
回复
能编译额,那太好了
这个问题终于搞定了
今天公司里的项目遇到一个此类的问题。呵呵
谢谢了!楼上的
老邓 2009-06-19
  • 打赏
  • 举报
回复
VC10编译也通过:
0 -842150451
Tom 33 30

Process returned 0 (0x0) execution time : 0.093 s
Press any key to continue.
老邓 2009-06-19
  • 打赏
  • 举报
回复
应该是VC2005对标准支持不好吧。
GCC 4.4.1编译通过!


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

class CForeign;
class CBase
{
friend class CForeign;

public:
CBase(void);
~CBase(void);

void DisplayData();

void SetValues(const string& _Str, const int& _Value);
private:
int age;
class CBaseImp;
CBaseImp* pImp;
};


class CBase::CBaseImp
{
public:
string name;
int val;

CBaseImp()
{
name = "";
val = 0;
}
};

CBase::CBase(void)
{
pImp = new CBase::CBaseImp();
}

CBase::~CBase(void)
{
delete pImp;
}

void CBase::DisplayData()
{
cout << pImp->name << " " << pImp->val << " " << age << endl;
}

void CBase::SetValues(const string& _Str, const int& _Value)
{
pImp->name = _Str;
pImp->val = _Value;
}


class CForeign
{
public:
CForeign(void);
~CForeign(void);
CBase* pBase;


void SetBaseName(const string& _Str);

void SetBaseValue(const int& _Value);

void SetAge(const int& _Age);


};

CForeign::CForeign(void)
{
pBase = new CBase();
}

CForeign::~CForeign(void)
{
delete pBase;
}

void CForeign::SetAge(const int& _Age)
{
pBase->age = _Age;
}

void CForeign::SetBaseName(const string& _Str)
{
pBase->pImp->name=_Str;// 取消注释,编译不通过
}

void CForeign::SetBaseValue(const int& _Value)
{
pBase->pImp->val=_Value;// 取消注释,编译不通过
}

int main()
{

CForeign mybase2;
mybase2.pBase->DisplayData();
mybase2.SetAge(30);
mybase2.SetBaseName("Tom");
mybase2.SetBaseValue(33);
mybase2.pBase->DisplayData();
return 0;
}

运行:
0 0
Tom 33 30

Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧