由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个nested class的问题
相关主题
What is wrong with the code?关于c++的constructor的面试题
奇怪的问题:关于一个简单的malloc()小程序 (转载)谁给说说bitwise operation
计算组合数C(m,n)请问一个exception题目
请教一道题 (转载)Use Visual .NET for C++ programming
大侠们救命, C++ operator new 问题三个C syntax 弱问题
C++ online Test 又一题 (转载)这个C++程序为什么不能运行
一个C语言的面试题,有点乱,麻烦看一下一个读用户输入的小问题
shared_ptr处理stack上面的指针a template counting - anybody understand this?
相关话题的讨论汇总
话题: holder话题: void话题: int话题: pointer话题: nested
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
nested_class.cpp: In member function ‘void A::B::print_b()’:
nested_class.cpp:14: error: cannot call member function ‘void
A::print()’ without object
#include
#include
using namespace std;
class A {
public:
A(int a): _a (a) {}
void print() { cout << _a;}
class B {
public:
B(int b): _b (b) {}
void print_b() { cout << _b << ' '; print();}
private:
int _b;
};
private:
int _a;
};
int main (int argc, const char* argv[]) {
if ( argc != 3 )
return -1;
int x = atoi(argv[1]);
int y = atoi(argv[2]);
A a (x);
A::B b (y);
a.print();
return 0;
}
P********e
发帖数: 2610
2
在B里面,添加一个A a;
然后拿a.print();

【在 g*********s 的大作中提到】
: nested_class.cpp: In member function ‘void A::B::print_b()’:
: nested_class.cpp:14: error: cannot call member function ‘void
: A::print()’ without object
: #include
: #include
: using namespace std;
: class A {
: public:
: A(int a): _a (a) {}
: void print() { cout << _a;}

r*******y
发帖数: 1081
3
thinking in c++ 里面有关于 nested friend class的一个例子,不知道有没有帮助。
//: C05:NestFriend.cpp
// Nested friends
#include
#include // memset()
using namespace std;
const int sz = 20;
struct Holder {
private:
int a[sz];
public:
void initialize();
struct Pointer;
friend struct Pointer;
struct Pointer {
private:
Holder* h;
int* p;
public:
void initialize(Holder* h);
// Move around in the array:
void next();
void previous();
void top();
void end();
// Access values:
int read();
void set(int i);
};
};
void Holder::initialize() {
memset(a, 0, sz * sizeof(int));
}
void Holder::Pointer::initialize(Holder* rv) {
h = rv;
p = rv->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() {
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++) {
cout << "hp = " << hp.read()
<< ", hp2 = " << hp2.read() << endl;
hp.next();
hp2.previous();
}
} ///:~

【在 g*********s 的大作中提到】
: nested_class.cpp: In member function ‘void A::B::print_b()’:
: nested_class.cpp:14: error: cannot call member function ‘void
: A::print()’ without object
: #include
: #include
: using namespace std;
: class A {
: public:
: A(int a): _a (a) {}
: void print() { cout << _a;}

1 (共1页)
进入Programming版参与讨论
相关主题
a template counting - anybody understand this?大侠们救命, C++ operator new 问题
a question on C++ stringC++ online Test 又一题 (转载)
定义的struct数组很大时,为什么会出现奇怪的大数字?一个C语言的面试题,有点乱,麻烦看一下
char ** pt1和 char * pt2[] 的区别在哪?shared_ptr处理stack上面的指针
What is wrong with the code?关于c++的constructor的面试题
奇怪的问题:关于一个简单的malloc()小程序 (转载)谁给说说bitwise operation
计算组合数C(m,n)请问一个exception题目
请教一道题 (转载)Use Visual .NET for C++ programming
相关话题的讨论汇总
话题: holder话题: void话题: int话题: pointer话题: nested