|
Actually in want to store it in vector or list or any other data structure. For that, I want to constructor class.
|
|
|
|
|
Then you need to think about the actual requirements of your class and create a design that will match those requirements. With such a vague question it is impossible to suggest much more.
|
|
|
|
|
That's a lot of overhead, presumes a Windows platform, and pulls in a lot of GUI-related dependencies just to store some data.
|
|
|
|
|
Not so, it's a quite simple way of creating the structure that the OP asked about. There is no GUI overhead if the control is never displayed.
|
|
|
|
|
There is code size overhead for pulling in a GUI library which may not be needed. For instance, a in a command-line application, linking in Treeview (and all classes it depends on) could significantly affect the size of the executable, the time to start up, and it would also affect portability of the code.
|
|
|
|
|
All I did in my initial response was to suggest a possible solution to what was a fairly vague question. I leave it to the OP to decide whether that helps to solve their problem.
|
|
|
|
|
Assuming you have string leafs (e.g. file names) in your branches, the minimal structure is:
struct StringTreeNode
{
std::string name;
std::vector<StringTreeNode> branches;
}
A leaf is just a branch with no subnodes.
|
|
|
|
|
Check out http://tree.phi-sci.com/index.html[^]. The documentation offers some insight into its proper use with the two types of iterators it provides.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
You can use a struct or a class with nested vector of same type structs:
#include <vector>
#include <string>
#include <iostream>
struct TreeNode {
std::string payload;
std::vector<TreeNode> children;
TreeNode(const std::string &p): payload(p) {}
TreeNode() {} };
void walk_tree(TreeNode &node, int indent = 0) {
std::cout << std::string(indent * 4, ' ')
<< node.payload << std::endl;
for (size_t i = 0; i < node.children.size(); ++i)
walk_tree(node.children[i], indent + 1);
}
int main() {
TreeNode a("home");
a.children.push_back(TreeNode("peter"));
a.children.push_back(TreeNode("jane"));
a.children[0].children.push_back(TreeNode("game.exe"));
a.children[1].children.push_back(TreeNode("homework.txt"));
walk_tree(a);
return 0;
}
|
|
|
|
|
I hope I can explain this.
I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack).
class COpenTrack
{
public:
CTrackSwitch trackSwitch[10];
COpenTrack(void);
….
I can initialize each array class using constructors, each with different parameters.
But initialization only works when done in a method of the “mother” class.
I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something.
boolean COpenTrack ::SwitchRoute( int iTrack)
{
// TODO works as local variable only - not as COpenTrack variable
CTrackSwitch trackSwitch[] = {
CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
CTrackSwitch(4,6,10,6),// normal reverse, input , switch
CTrackSwitch(1,3,9,3),// normal reverse, input , switch
CTrackSwitch(5,7,11,5),// normal reverse, input , switch
CTrackSwitch(8,10,12,2),// normal reverse, input , switch
CTrackSwitch(9,11,13,1),// normal reverse, input , switch
CTrackSwitch(12,13,14,0)// normal reverse, input , switch
};
…..
Any help would be appreciated.
Cheers Vaclav
|
|
|
|
|
if you can make trackSwitch static, you can do this:
class COpenTrack
{
public:
static CTrackSwitch _trackSwitch[10];
COpenTrack(void)
{
}
};
CTrackSwitch COpenTrack::_trackSwitch[] =
{
CTrackSwitch(0,2,8,4), CTrackSwitch(4,6,10,6), CTrackSwitch(1,3,9,3), CTrackSwitch(5,7,11,5), CTrackSwitch(8,10,12,2), CTrackSwitch(9,11,13,1), CTrackSwitch(12,13,14,0) };
|
|
|
|
|
Boy that was fast. Thanks.
I will try your way first.
I think I'll punish myself and see if I can initialize it using constructor same way as I did the array.
Cheers Vaclav
|
|
|
|
|
What about something like:
COpenTrack::COpenTrack()
{
trackSwitch[0] = CTrackSwitch(0,2,8,4),
trackSwitch[1] = CTrackSwitch(4,6,10,6),
...
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
David,
it works, but it is "not cool", just "plain" variable initialization.
I did try to initialize the whole array in default constructor and it did not fly.
I would probably get more descriptive error messages from some other IDE to figure it out.
It should be same syntax I used for the CTrackSwitch, but I thing the array is messing it up.
....
int iNormal; // passing thru
int iReverse; // turning
int iInputTrack; //
int iSwitch; // may be usefull elesewhere
// initialize using constructor
CTrackSwitch(int iNormalInit,int iReverseInit,int iInputTrackInit, int iSwitchInit )
{
iNormal = iNormalInit;
iReverse = iReverseInit;
iInputTrack = iInputTrackInit;
iSwitch = iSwitchInit;
}
....
This is one attempt which actually compiled
COpenTrack(class CTrackSwitch() )
{
trackSwitch[10] = CTrackSwitch();
}
but I could not get the constructor to work.
Oh well, thanks for your help.
Cheers Vaclav
|
|
|
|
|
In C++11 you should be able to use an initializer list just as you did in your local variable example. See http://stackoverflow.com/questions/161790/initialize-a-const-array-in-a-class-initializer-in-c[^]
That said, of course your compiler needs to support it before you can use that syntax.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I have a function which recursively scans an array for a matching value and when found processes it and returns. Since it works recursively a single return obviously won't do.
I was hoping I could have a static counter of the recursive calls but have no idea how to implement multiple returns.
I am doing this on Arduino, so KISS is important to me.
Any help would be appreciated.
Thanks Vaclav
|
|
|
|
|
Just make the function return true if a matching value is found and false otherwise. Then, when a return of true is received, simply exit the function without further processing and this will unwind the stack.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It is not really obvious if recursion needs multiple return statements.
It all really depends on the logic being implemented.
So if you've implemented it and are not getting the expected results, posting the relevant code should get the good people here to help you figure out the problem.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
You may be right that I am not sure about the "returns".
But debugging in IDE which lacks debugger you kinda flying blindfolded.
I'll try different way to skin this cat!
|
|
|
|
|
1. It is always possible to make do with a single return. I'm not sure what makes you think otherwise.
2. searching an array for a matching value is a trivial operation - why did you implement it recursively? What is so difficult in your search algorithm that it prevents you from implementing it in a simple loop?
3. having a static counter for depth of recursion is a common method, and it helps catching endless recursions. But, generally speaking, static variables can cause many issues - if there is even a remote chance you're going to use multithreading sometime in the future, just forget about it! Either way, a safer method is to create a context and pass a reference (i. e. pointer in C/C++) to the recursive function. That context object may contain a counter or any other mechanism to control recursion depth, but it won't be static: it lives in the context of the thread that initialiy called the recursive function, and it won't cause conflicts when that function is called from different clients at the same time.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Thanks Stefan,
I will take another look at the process to see if I can do it using loop(s).
|
|
|
|
|
And if your array is sorted, your search can go from O(N) to O(log N).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Have a look at this: "Recursive Functions"[^].
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Does anybody have an idea about how to separate or detect overlapped objects? I am currently working on a project aiming at the detection crops in the late stage of growth. I did the first step, which is the segmentation (segment the plants from the background), but I got stack with detection stage because of overlapping between two crops or more (cauliflower). By the way, I am working on Opencv and visual C++. I will be grateful for your help.
modified 22-Oct-14 12:21pm.
|
|
|
|
|
HAMUDA78 wrote: Does anybody have an idea about how to separate or detect overlapped objects?
I actually, I am currently working on a project aiming at the detection crops in the late stage of growth. I did the first step, which the segmentation (segment the plants from the background), but I got stuck with detection stage because of the overlapping between the two crops or more (cauliflower). How is this a C/C++/MFC question?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|