// test.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <stdio.h>
#include<string.h>
#define SZ 20struct holder{
private:
int a[SZ];
public:
void initialize();
struct pointer{   //首先声明一个struct,然后声明为全局范围内的友元.
private:
holder* h;
int *p;
public:
initialize(holder* H);
void next();
void previous();
void top();
void end();
int read();
void set(int i);
};
friend holder::pointer;
};void holder::initialize()
{
memset(a,0,SZ* sizeof(int));
}void holder::pointer::initialize(holder* H){
h = H;
p = h->a;
}void holder::pointer::next()
{
if(p<&(h->a[SZ-1])) p++;
}void holder::pointer::previous(){
if(p>&(h->a[0])) p--;
}void holder::pointer::top()
{
p = &h->a[0];
}void holder::pointer::end()
{
p = &h->a[SZ-1];
}int holder::pointer::read(){
return *p;
}void holder::pointer::set(int i) {
*p = i;
}int main(int argc, char* argv[])
{
holder h;
holder::pointer hp,hp2;
int i; h.initialize();
hp.initialize(&h);
hp2.initialize(&h);
for(i=0;i<SZ;i++){
hp.set(i);
hp.next();
}
hp.top();
hp2.end();
for(i=0;i<SZ;i++) {
printf("hp = %d,hp2 = %d\n",
hp.read(),hp2.read());
hp.next();
hp2.previous();
}
return 0;
}
=============================================================================
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
G:\CSTUDY\3\test\test.cpp(35) : error C2556: 'void __thiscall holder::pointer::initialize(struct holder *)' : overloaded function differs only by return type from 'int __thiscall holder::pointer::initialize(struct holder *)'
        G:\CSTUDY\3\test\test.cpp(19) : see declaration of 'initialize'
G:\CSTUDY\3\test\test.cpp(35) : error C2371: 'initialize' : redefinition; different basic types
        G:\CSTUDY\3\test\test.cpp(19) : see declaration of 'initialize'
G:\CSTUDY\3\test\test.cpp(74) : error C2264: 'initialize' : error in function definition or declaration; function not called
G:\CSTUDY\3\test\test.cpp(75) : error C2264: 'initialize' : error in function definition or declaration; function not called
Error executing cl.exe.
Creating browse info file...test.exe - 4 error(s), 0 warning(s)