1
#ifndef ASSETS_H_INCLUDED
#define ASSETS_H_INCLUDED
#include <vector>
#include string.h>

const int ID_Max = 100;
typedef char ID[ID_Max];

struct node;

struct people{
std::vector<ID> T_ID;
std::vector<node*> Nodes;
people(ID t, node* person){
    T_ID.push_back(t);
    Nodes.push_back(person);
}
people(){}
};

struct node {
ID T_ID;
node* Parent;
people* leftChildren;
node* rightChild;
node(ID t, node* p, node* l, node* r) :I_ID(t), Parent(p), rightChild(r) 
{leftChildren = new people(); }
};

#endif // ASSETS_H_INCLUDED

My problem is this it is interpreting ID as a char pointer when in the constructor so this is the constructor people::people(char*, node*) when I want people::people(char[ID_Max], node*) same for node. If you have advise it would be very appreciated.

thealbinosmurf
  • 561
  • 2
  • 7
  • 13
  • 5
    Why not use a `std::string` instead of a `char[100]` ? – hmjd May 12 '12 at 08:30
  • the method for comparison I am using for my project is memcmp and so I wanted to set the base size – thealbinosmurf May 12 '12 at 08:34
  • ...so the next obvious question is why are you using `memcmp` to compare two string values? – Cody Gray - on strike May 12 '12 at 08:35
  • `std::string` has `==`, among other useful features. – hmjd May 12 '12 at 08:36
  • it has to do with the search method I want to implement – thealbinosmurf May 12 '12 at 08:38
  • You can compare ranges in c++ if that's what you're trying to do ... – ScarletAmaranth May 12 '12 at 08:38
  • If you're asketic enough (which you are since you're using char* and memcmp), you can simply pass pointer + sizeof(type) * size of array - 1, which will return the address of the last element if that's what you're really asking for. – ScarletAmaranth May 12 '12 at 08:43
  • No I am not. In my class I am required to use several methods of searching on one single data set, which are all int but some happen to be over 64bits, this one is the 7 tries method so I wanted to use 'memcmp' but that limited me to 'char[]' with a set bound. – thealbinosmurf May 12 '12 at 08:47
  • You can also get access to the internal `char` array within `std::string`, via `c_str()`, and you can pass that to `memcmp` if you really want to. – hmjd May 12 '12 at 08:47
  • I wanted to do that but then they might not have the same number of bytes to compare – thealbinosmurf May 12 '12 at 08:56
  • Compare on the shortest length then. – hmjd May 12 '12 at 09:45
  • 1
    Your type `ID` isn't assignable, so you can't use it in standard library containers. – Kerrek SB May 12 '12 at 09:54
  • Actually, array types are probably banned from *all* container types, since they're not allocable (essentially because [array-placement-new is unusable](http://stackoverflow.com/questions/8720425/array-placement-new-requires-unspecified-overhead-in-the-buffer)). – Kerrek SB May 12 '12 at 09:58

1 Answers1

2

If you write a function signature with an array type in it, it's the same as using a pointer, e.g. this:

void f(char p[]);

is the same as this:

void f(char *p);

That looks like it's the root of your problem here. You might be better off with e.g. a std::array<char,ID_Max> (in C++11), or a std::vector<char> (in C++98). You can then get a pointer to the start of the contiguous memory it contains using &cont[0]. As a minor nit, I seem to recall that the memory for vector wasn't strictly guaranteed to be contiguous in C++98, but it always was contiguous in practice (you could rely on it). The wording was fixed in C++03.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • It has always been guaranteed for vectors, it's for strings that the guarantee was added by c++11. – stefaanv May 12 '12 at 10:50
  • @stefaanv: http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/ (specifically the bit where he says "contiguity is in fact part of the vector abstraction. It’s so important, in fact, that when it was discovered that the C++98 standard didn’t completely guarantee contiguity, the C++03 standard was amended to explicitly add the guarantee.") – Stuart Golodetz May 12 '12 at 14:04
  • 1
    I stand corrected. I misread your answer, I thought you said "fixed in C++11" – stefaanv May 12 '12 at 15:37
  • 1
    This is what I ended up with thanks. I'm sorry I had forgotten to accept till now. – thealbinosmurf Aug 16 '13 at 17:56