Click here to Skip to main content
16,005,473 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: FillBuffer Pin
David Crow23-Jun-05 9:43
David Crow23-Jun-05 9:43 
GeneralRe: FillBuffer Pin
Andrew Admire23-Jun-05 9:45
Andrew Admire23-Jun-05 9:45 
Generalclass referencing problem Pin
zildjohn0123-Jun-05 7:40
zildjohn0123-Jun-05 7:40 
GeneralRe: class referencing problem Pin
Chris Losinger23-Jun-05 7:42
professionalChris Losinger23-Jun-05 7:42 
GeneralRe: class referencing problem Pin
John Simon23-Jun-05 7:52
John Simon23-Jun-05 7:52 
GeneralRe: class referencing problem Pin
Jose Lamas Rios23-Jun-05 7:48
Jose Lamas Rios23-Jun-05 7:48 
Generalnew problem Pin
John Simon23-Jun-05 9:35
John Simon23-Jun-05 9:35 
GeneralRe: new problem Pin
Jose Lamas Rios23-Jun-05 10:34
Jose Lamas Rios23-Jun-05 10:34 
The way you are mixing #includes and forward declarations is wrong. For example, take a look at what the compiler is seeing when you ask it to compile class2.cpp


  • finds an include for "class2.h", so it goes for it.
  • finds an include for "class1.h", so it goes for it.
  • compiles the declaration for class1. Nothing else in this file, so it returns to class2.h
  • finds an include for "struct1.h", so it goes for it.
  • finds an include for "class2.h", but it's already started processing that file, and noted a "pragma once" directive, so it ignores this include.
  • finds a forward declaration for class2, so it says to itself "I now know class2 is a class, even if I still don't know all the details"
  • starts compiling the declaration for struct1. Finds the declaration of "item" as a pointer to a "class2". At this point, the compiler only needs to know wether the type of "item" is valid, and how much space it takes to store it. It does know the type is valid because of the forward declaration. And it does know how much space is needed for "item", because all pointers are the same size. Compilation goes on.
  • It now reaches the line with "new class2()" in it, so it needs to create a call to some constructor of class2 with no parameters. The problem is it hasn't actually seen the complete declaration of class2 yet, so it doesn't know if such constructor exists


Reorganize your code as follows:

////////////////////////////////////////////////////////
// class1.h - Put class declaration here

#pragma once

class class1
{
public:
   class1();

// Other members' declarations
};


////////////////////////////////////////////////////////
// class1.cpp - Put class implementation here

#include "class1.h"

class1::class1()
{
}

// Other members' implementations


////////////////////////////////////////////////////////
// class2.h - Put class declaration here

#pragma once

#include "class1.h"

struct struct1;

class class2 : public class1
{
private:
   struct1* cfg;

public:
   class2();

   void Init(struct1* data);
};


////////////////////////////////////////////////////////
// class2.cpp - Put class implementation here

#include "class2.h"

class2::class2()
{
}

void class2::Init(struct1* data)
{
   // Consider moving this to the constructor and
   // removing this function from the class,
   // so that you don't need to always call Init()
   // after the creation of each and every object.
   cfg = data;
}


////////////////////////////////////////////////////////
// struct1.h - Put struct declaration here

#pragma once

class class2;

struct struct1
{
private:
   class2* item;

public:
   struct1();
};


////////////////////////////////////////////////////////
// struct1.cpp - Put struct implementation here

#include "struct1.h"
#include "class2.h"

struct1::struct1()
{
   item = new class2(); /* ERROR SHOULD NOT OCCUR HERE ANYMORE */
}


////////////////////////////////////////////////////////
// main.cpp

#include "class2.h"
#include "struct1.h"

void main()
{
   struct1* config;
   config->item = new class2();
   config->item->Init(config);
}


I didn't actually compiled the above code, so it may still fail due to some typo, but I hope you get the idea.



--
jlr
http://jlamas.blogspot.com/[^]
GeneralRe: new problem Pin
John Simon23-Jun-05 11:37
John Simon23-Jun-05 11:37 
Generaltext box Pin
trigger9123-Jun-05 6:43
susstrigger9123-Jun-05 6:43 
GeneralRe: text box Pin
Trollslayer23-Jun-05 6:48
mentorTrollslayer23-Jun-05 6:48 
GeneralRe: text box Pin
David Crow23-Jun-05 6:54
David Crow23-Jun-05 6:54 
GeneralRe: text box Pin
Trollslayer23-Jun-05 7:33
mentorTrollslayer23-Jun-05 7:33 
GeneralRe: text box Pin
David Crow23-Jun-05 8:02
David Crow23-Jun-05 8:02 
GeneralRe: text box Pin
Trollslayer23-Jun-05 8:32
mentorTrollslayer23-Jun-05 8:32 
GeneralRe: text box Pin
David Crow23-Jun-05 9:11
David Crow23-Jun-05 9:11 
GeneralRe: text box Pin
Trollslayer23-Jun-05 9:46
mentorTrollslayer23-Jun-05 9:46 
GeneralRe: text box Pin
David Crow23-Jun-05 9:48
David Crow23-Jun-05 9:48 
GeneralRe: text box Pin
trigger9123-Jun-05 7:03
susstrigger9123-Jun-05 7:03 
GeneralRe: text box Pin
Trollslayer23-Jun-05 7:38
mentorTrollslayer23-Jun-05 7:38 
GeneralRe: text box Pin
David Crow23-Jun-05 8:03
David Crow23-Jun-05 8:03 
GeneralI want to conact Mr.Andrew Peace Pin
suroor45323-Jun-05 6:26
suroor45323-Jun-05 6:26 
GeneralRe: I want to conact Mr.Andrew Peace Pin
Chris Losinger23-Jun-05 6:39
professionalChris Losinger23-Jun-05 6:39 
GeneralUDP connections Pin
Ergo23-Jun-05 6:03
Ergo23-Jun-05 6:03 
GeneralRe: UDP connections Pin
Scozturk23-Jun-05 8:29
professionalScozturk23-Jun-05 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.