#include <iostream>
using namespace std;
class Street;
class CrossStreet
{
private:
char m_chName;
Street* m_acLoS;
int m_nNoS;
static int m_nCSid;
public:
CrossStreet()
{
Init();
m_chName = m_nCSid;
}
CrossStreet(char chName)
{
Init();
m_chName = chName;
}
/** Problem is here **/
void AddStreet(Street* const cStreet)
{
m_acLoS[++m_nNoS] = cStreet;
}
~CrossStreet()
{
delete[] m_acList;
}
void Init()
{
m_nNoS = 0;
m_acLoS = 0;
m_nCSid++;
}
friend class Street;
};
class Street
{
private:
int m_nDistance;
public:
Street(CrossStreet& cHead, CrossStreet& cTail, int nDistance)
{
CreateStreet(cHead,cTail,nDistance);
}
void CreateStreet(CrossStreet& cHead, CrossStreet& cTail, int nDistance)
{
m_nDistance = nDistance;
cHead.AddStreet(Street* const THIS);
cTail.AddStreet(Street* const THIS);
}
};
when I compile above code, this throw error:
-->>invalid use of incomplete 'struct Street'
-->>forward declaration of 'struct Street'
function AddStreet of an object of ‘CrossStreet’ was called at an object of ‘Street’, to add ‘THIS street object to member variable of CrossStreet object. So I think I can use THIS pointer in this case but it not compile.
There is no such things as a
THISpointer. There is athispointer though. So your code inCreateStreetis invalid (and not only for that). The correct syntax would be:The problem in
AddStreetis that you have definedm_acLoSas a pointer-to-Street. Som_acLoS[x]is of typeStreet, and notStreet*. But you can’t use aStreetobject there since you don’t have a complete definition ofStreetat that point. You can only use pointers to Street inCrossStreet. (Additionally, you’re lacking storage allocation form_acLoS, so even if you could use that type there, and if the assignment was legal, it would still be a bug).Depending on what you’re after, a
std::vector<Street*>orstd::list<Street*>or some other container could be what you’re looking for, instead of that incorrect member array. (But beware that you’re responsible for managing the lifetime of those pointers.)