|
|
hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code::
main.cpp
#include "class1.h"
void main() {
class1 test;
}
class1.h
#pragma once
#include "class2.h"
class class1 {
private:
class2 *child1;
class2 *child2;
public:
class1() {
child1 = new class2();
child1->Init();
child2 = new class2();
child2->Init();
}
};
class1.cpp
#include "class1.h"
class2.h
#pragma once
#include "class1.h"
class class2 {
private:
class1 *parent;
public:
void Init(class1 *the_parent) {
parent = the_parent;
}
};
class2.cpp
#include "class2.h"
Here is a snapshot of compiler output:
Compiling...
class2.cpp
class1.h(5) : error C2143: syntax error : missing ';' before '*'
class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers
class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers
class1.h(6) : error C2143: syntax error : missing ';' before '*'
class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers
class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers
main.cpp
class2.h(5) : error C2143: syntax error : missing ';' before '*'
class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers
class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers
class2.h(7) : error C2061: syntax error : identifier 'class1'
class1_1.cpp
class2.h(5) : error C2143: syntax error : missing ';' before '*'
class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers
class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers
class2.h(7) : error C2061: syntax error : identifier 'class1'
one class's signature must be declared before the other, but they both reference each other.. how can this be done?
|
|
|
|
|
class1.h
#pragma once
#include "class2.h"
class class2;
class class1 {
private:
class2 *child1;
class2 *child2;
public:
class1()
{
child1 = new class2();
child1->Init();
child2 = new class2();
child2->Init();
}
};
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
thanks so much! first time i got an answer in a forum in only 2 minutes
|
|
|
|
|
You need to use forward declarations.
Try this:
<br />
#pragma once<br />
<br />
class class2;<br />
<br />
class class1<br />
{<br />
private:<br />
class2* child1;<br />
class2* child2;<br />
<br />
public:<br />
class1()<br />
{<br />
child1 = new class2(this);<br />
child2 = new class2(this);<br />
}<br />
};<br />
<br />
#pragma once<br />
<br />
class class1;<br />
<br />
class class2<br />
{<br />
private:<br />
class1* parent;<br />
<br />
public:<br />
class2(class1* the_parent) : parent(the_parent)<br />
{<br />
}<br />
};<br />
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
that appeared to solve my problem, but now i have a new one.
i get error C2512, no default constructor available, but there is a public constructor clearly defined with no parameters for both classes. this error occurs in the constructor for struct1. here is the code:
main.cpp
#include "class2.h"
void main() {
struct1 *config;
config->item = new class2();
config->item->Init(config);
}
struct1.h
#pragma once
#include "class2.h"
class class2;
struct struct1 {
private:
class2 *item;
public:
struct1() {
item = new class2(); /* ERROR OCCURS HERE while
compiling main.cpp and class2.cpp */
}
};
class1.h
#pragma once
class class1 {
public: class1(){}
};
class2.h
#pragma once
#include "class1.h"
#include "struct1.h"
struct struct1;
class class2 : public class1 {
private:
struct1 *cfg;
public:
class2() {}
void Init(struct1 *data) {
cfg = data;
}
};
struct1.cpp
#include "struct1.h"
class1.cpp
#include "class1.h"
class2.cpp
#include "class2.h"
|
|
|
|
|
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:
#pragma once
class class1
{
public:
class1();
};
#include "class1.h"
class1::class1()
{
}
#pragma once
#include "class1.h"
struct struct1;
class class2 : public class1
{
private:
struct1* cfg;
public:
class2();
void Init(struct1* data);
};
#include "class2.h"
class2::class2()
{
}
void class2::Init(struct1* data)
{
cfg = data;
}
#pragma once
class class2;
struct struct1
{
private:
class2* item;
public:
struct1();
};
#include "struct1.h"
#include "class2.h"
struct1::struct1()
{
item = new class2();
}
#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/[^]
|
|
|
|
|
ok thank you so much, my code compiles and runs now
|
|
|
|
|
Hi,
I am doing a project in vc++ win32...dialog based app.
I want display the content from a text file in a text box in my dialog.
say for eg. License agreement..
how do i do that.What the properties should I set for the text box.
Thanks a lot.
|
|
|
|
|
If it is a simple edit box (plain text) then use Object.SetWindowText(CString) .
You can use a LPCSTR pointer etc., a null terminated string etc.
For rich text it's a bit more difficult and involves callbacks etc., if so let me know and I will send a code sample.
Elaine
The tigress is here
|
|
|
|
|
Trollslayer wrote:
For rich text it's a bit more difficult
How so? SetWindowText() also works for rich edit controls.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
That works fine until you reach the buffer limit, tried it a couple of weeks ago.
After that you have to use StreamIn with a callback.
Elaine
The tigress is here
|
|
|
|
|
Trollslayer wrote:
That works fine until you reach the buffer limit...
Which is what?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
It isn't specified, but in my was case just over 4KB (4196 bytes if i remember correctly). This is the buffer size passed over to the StreamIn callback function. I assume that WriteWindowText uses the same internal mechanism.
The tigress is here
|
|
|
|
|
You obviously did something wrong. A regular edit control can handle 64KB of text. A rich edit control can handle several MB of text. I just tried it with a 791KB text file (it's the biggest one I could find within a few seconds) and it worked fine.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
|
Trollslayer wrote:
Which version ?
I am using VC++ v6.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Thanks for tha reply..It is simple edit box...not rich text..
but how I get the content of text file in cstring..
Thanks a lot
|
|
|
|
|
|
Use CEdit::GetWindowText() . One of them takes a reference to a CString object.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
|
|
Hello!
Question about UDP connections.
Is there a way to determine the destination port/address from an(y) active open UDP socket on my system?
Maybe a workaround or simple solution without using winpcap.dll.
<view next="">
|
|
|
|
|
I'm not sure but google on raw sockets I think you will find some info...
Well... I am a beginner ...
|
|
|
|
|