c++类的前置声明

class a;
class B
{
public:
    B(){}
    a *c;
};
int i = 9;
class a
{
public:
    void print()
    {
        cout << "hello" << endl;
    }
};

int main(int argc, char** argv)
{
    B b;
    b.c->print();
    cout << i << endl;
    system("pause");
}

a *c;此处只能是指针或引用,才能用类的前置声明。而且 class a 不能在min()函数后面。

// readXml.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "tinyxml\tinyxml.h"
#include <string>
#include <string.h>
#include <time.h>
#include <vector>
#include <map>
#include <fstream>
#define ARRAY_CNT( a ) ( sizeof(a)/(sizeof(a[0])) )	
using namespace std;

class b;
class a
{
public:
	a(){};
	b* f;
	int add(int i, int j)
	{  	
		j = f->up(j);
		return i + j;
	}
};
class b
{
public:
	int up(int c)
	{
		return c + 1;
	}
};

int main(int argc, char** argv)
{	
	a gg;
	cout << gg.f->up(2) << endl;
	cout << gg.add(1, 6) << endl;
	system("pause");
}

第23行要用的时候报错,注意解决。

猜你喜欢

转载自blog.csdn.net/qq_40222602/article/details/78973811
今日推荐