|
Even with only one project, in VS v9, if I select all targets/platforms and put in a preprocessor directive, it simply removes all the current directives and replaces it with what I typed in. This doesn't happen this way for you?
Sorry about the wrong forum, I looked, but the closest I saw was this one having a description of "C, Visual C++ and MFC" and thought it was the closest.
|
|
|
|
|
Craig Longman wrote: it simply removes all the current directives and replaces it with what I typed in.
I just tried that in Visual C++ Express 2010 and it works fine, the new value gets added but nothing gets deleted.
Craig Longman wrote: I looked, but the closest I saw was this one having a description of "C, Visual C++
Hmmm, I would have thought that "Visual Studio" is closest; this question has almost nothing to do with C, C++ or MFC.
I must get a clever new signature for 2011.
|
|
|
|
|
I just did it again in v9 and when All Configurations is selected, it replaces them all. I guess it was fixed.
Richard MacCutchan wrote: Hmmm, I would have thought that "Visual Studio" is closest; this question has almost nothing to do with C, C++ or MFC.
Indeed, I guess I didn't actually point out that the Visual Studio way at the bottom escaped my attention. =)
Thanks anyway, clearly something resolved in the next version, another good reason to upgrade I guess. Just gotta be careful for the time being.
|
|
|
|
|
It's not just you. What's crazy is if you copy preprocessor settings from another project and paste them, it's smart enough to remove duplicates!
|
|
|
|
|
Hi there i am currently facing this problem. I use forward declaration to declare both structs but when doing an assignment to the struct it complains that is undefined.Ex. i have this structs:
struct A;
struct B;
struct A
{
int lolipops;
B* candy;
A(B* ptr)
{
lolipops = 0;
candy = ptr;
}
action(int pass)
{
candy->gums = pass;
}
};
struct B{
int gums;
A* candy;
B(A* ptr)
{
gums = 0;
candy = ptr;
}
action(int pass)
{
candy->lolipops = pass;
}
};
This is a sample not used currently by me but it ilustrates the problem. Can someone tell me what is wrong?
modified on Sunday, February 13, 2011 4:54 PM
|
|
|
|
|
Sorry, my original answer does not work. Something strange about this post, the forward declarations did not show in the posted question.
Please also use <pre></pre> tags round your code - set by the code block button.
The forward declaration will only help you to have pointers to B types in structure A . You will not be able to refer to any of the variables or methods of B as they have not been defined at that point. You will need to modify your design to resolve your problem.
I must get a clever new signature for 2011.
modified on Sunday, February 13, 2011 4:49 PM
|
|
|
|
|
Can you tell me how to solve it. What it needs to be modified?, Please
|
|
|
|
|
There is no fix for what you want, since you have effectively got circular dependencies: A refers to B and B refers to A so one of them will be undeclared in the declaration of the other. You cannot make this work and need to redesign your program to remove such dependencies.
I must get a clever new signature for 2011.
|
|
|
|
|
Not sure it suffices, however within the structs, I would use struct B* instead of just B* , and likewise for A* .
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Nope i tried your suggestion and doesn't work either
|
|
|
|
|
you replaced all of them? it should never throw two errors, when the compiler reaches the second struct it knows all there is to know about the first one.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Keep declaration in a header file. That will make the forward declarations work. Then put the implementation in a cpp-file. Here you can do these kinds of things since both structs are declared properly. Btw, I still think you should reconsider your design if possible.
|
|
|
|
|
That won't make any difference as one of them will still be undefined in the declaration of the other. See my answer above.
I must get a clever new signature for 2011.
|
|
|
|
|
The following is perfectly legal
struct A;
struct B;
struct A
{
int lolipops;
B* candy;
A(B* ptr);
void action(int pass);
};
struct B{
int gums;
A* candy;
B(A* ptr);
void action(int pass);
};
The compiler needs to know the size of a class when it's compiling it. That's the major reason why things must be declared in advance. In this case, the dependency size is known since the member is a pointer to the forward declared class. You can of course not use that pointer until the thing it points to has been properly declared, and that's the problem in the OP.
Edit: When I answered, the members were actually pointers to the other class. That might have changed since your reply?
|
|
|
|
|
OK so declaring in the .h and implementing in the .cpp works but it doesn't if your declare/implement in the same place. Someone knows why?
And also i will to know how to improve the design i want to know what is wrong and to improve. Thanks
|
|
|
|
|
It has nothing to do with it being defined in the header or the cpp file. The issue was with referring to struct variables that have not been declared. In your original question you had a forward declaration of struct B . You then had a declaration of struct A which contained a struct B* ; all fine up to this point. struct A then contained a reference to one of struct B 's variables, which had not yet been defined, hence your compiler error.
If it works now, then you must have changed something.
I must get a clever new signature for 2011.
|
|
|
|
|
I did only changed the following, i just migrate the implementation from the .h to .cpp and it worked.
Look this is what i had originaly at first in a .h, which is the way it did not worked
//base.h file
struct A;
struct B;
struct A
{
int lolipops;
B* candy;
A(B* ptr)
{
lolipops = 0;
candy = ptr;
}
action(int pass)
{
candy->gums = pass;
}
};
struct B
{
int gums;
A* candy;
B(A* ptr)
{
gums = 0;
candy = ptr;
}
action(int pass)
{
candy->lolipops = pass;
}
};
The way it worked is the following i only declared the structs in .h and implemented it on the .cpp file like this:
.h file
struct A;
struct B;
struct A
{
int lolipops;
B* candy;
A(B* ptr);
void action(int pass);
};
struct B
{
int gums;
A* candy;
B(A* ptr);
void action(int pass);
};
in .cpp file
#include "base.h"
A::A(B* ptr)
{
}
void A::action(int pass)
{
candy->gums = pass;
}
B::B(A* ptr)
{
candy = ptr;
}
void B::action(int pass)
{
candy->lolipops = pass;
}
and that is what changed, so trying to do everything in the .h file does not work
|
|
|
|
|
You can make it work even if you put it all in the .h file
struct A;
struct B;
struct A
{
int lolipops;
B* candy;
A(B* ptr) {}
inline void action(int pass);
};
struct B
{
int gums;
A* candy;
B(A* ptr) {}
inline void action(int pass);
};
inline void A::action(int pass)
{
candy->gums = pass;
}
inline void B::action(int pass)
{
candy->lolipops = pass;
}
|
|
|
|
|
this way works perfectly so i have to implement the non-constructor code outside the struct body and it worked, Thanks
|
|
|
|
|
Basically it all comes down to the compiler being incapable of reading ahead: the moment you do something you have to make sure that all neccessary defintions have been made before:
struct A;
struct B;
struct A {
int gummy;
B* myb;
A() : myb(0) {}
void set(B* b) {
myb = b;
gummy = b->bears;
}
};
struct B {
int bears;
A* mya;
B() : mya(0) {}
void set(A* a) {
mya = a;
bears = a->gummy;
}
};
You can skip the forward declaration of A here, but you have to move the implemetation of A::set() to a later point in your code, so the compiler gets a chance to read the declaration of B first:
struct B;
struct A {
int gummy;
B* myb;
A() : myb(0) {}
void set(B* b);
};
struct B {
int bears;
A* mya;
B() : mya(0) {}
void set(A* a) {
mya = a;
bears = a->gummy;
}
};
void A::set( B* b )
{
myb = b;
gummy = b->bears;
}
Does this help?
|
|
|
|
|
Yes it helps, i now understand more of the problem here so this is compiler dependent then. Thanks for your time i have learned from all you!!
|
|
|
|
|
Yes, I know it's legal, but that is not what the OP had in his/her definitions.
I must get a clever new signature for 2011.
|
|
|
|
|
I know you know. There was just great confusion.
|
|
|
|
|
code like this:
class Your
{
public:
My my[3];
};
class My has no default constructor, as:
class My
{
public:
My(int iv)
{
}
};
how do you init class array my[3]?
is it impossible?
|
|
|
|
|
I'm not near a compiler, but it *might* work...
If Your::My was not an array, then you could do this:
Your::Your ()
: My (42)
{
}
I'm wondering if:
Your::Your ()
: My [0](42), My [1](420), My [2](3)
{
}
would work.
Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
|
|
|
|