Два класса, имеющих указатели друг на друга
От: blacksun  
Дата: 30.09.08 14:44
Оценка:
cls1.h
#pragma once

#include "cls2.h"

class cls2;

class cls1
{
public:
    cls1() { test1 = 5; }
    int test1;
    cls2 * pCls2;

    void setp(cls2 *p);
};



cls2.h
#pragma once

#include "cls1.h"

class cls1;

class cls2
{
public:
    cls2() { pCls1 = NULL; }
    int test2;
    cls1 *pCls1;

    void setp(cls1 *p);
    void prnt() { if (pCls1) printf("%d\n", pCls1->test1); } // <-- проблема здесь
};



cls1.cpp
#include "stdafx.h"
#include "cls1.h"

void cls1::setp(cls2 *p)
{
    pCls2 = p;
}


cls2.cpp

#include "stdafx.h"
#include "cls2.h"

void cls2::setp(cls1 *p)
{
    pCls1 = p;
}


simple.cpp
#include "stdafx.h"

#include "cls1.h"
#include "cls2.h"


int _tmain(int argc, _TCHAR* argv[])
{

    cls1 c1;
    cls2 c2;

    c2.setp(&c1);

    return 0;
}




1>------ Build started: Project: simple, Configuration: Debug Win32 ------
1>Compiling...
1>simple.cpp
1>d:\sources\itrium\simple\simple\cls2.h(15) : error C2027: use of undefined type 'cls1'
1> d:\sources\itrium\simple\simple\cls2.h(5) : see declaration of 'cls1'
1>d:\sources\itrium\simple\simple\cls2.h(15) : error C2227: left of '->test1' must point to class/struct/union/generic type
1>cls1.cpp
1>d:\sources\itrium\simple\simple\cls2.h(15) : error C2027: use of undefined type 'cls1'
1> d:\sources\itrium\simple\simple\cls2.h(5) : see declaration of 'cls1'
1>d:\sources\itrium\simple\simple\cls2.h(15) : error C2227: left of '->test1' must point to class/struct/union/generic type
1>Generating Code...
1>Build log was saved at "file://d:\Sources\Itrium\simple\simple\Debug\BuildLog.htm"
1>simple — 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



такое ощущение, что компилятор не знает, что у cls1 есть переменная test1.
подскажите, как побороть проблему?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.