std::vector pointing

What I want to do is create a single function that will store any of my vector types to a temporary binary file.
Here's a basic example of what I want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Vect : public std::vector {};
class Obj1V : public Vect< Obj1 > {};
class Obj2V : public Vect< Obj2 > {};
bool SaveDataB( Vect* vect, wxFile& file, size_t objSize )
{
// This function will be more bulky than this, I simply ignored the other stuff because you don't need to see it
  size_t vSize = objSize * vect->size();
  return ( file.Write( &( *vect[ 0u ] ), vSize ) == vSize );
}
/*
 Some function calls SaveDataB( &vObj1, file, sizeof( Obj1 ) );
  where vObj1 is an instance of Obj1V
 Some function calls SaveDataB( &vObj2, file, sizeof( Obj2 ) );
  where vObj2 is an instance of Obj2V
*/

It's that Vect* part that gets me because CodeBlocks doesn't show any members using that kind of initialization so I'm assuming that it's wrong like that, any help on this would be appreciated. I want to do it like this because it easier to debug than pre-processor code.

I've got to go to work now so it won't be until midnight at least before I can reply.
I'm not entirely clear what you're asking, but if all you're asking is whether or not you can have a pointer to a std::vector, then, yes, you can. An object of type std::vector is an object like any other, and you can use pointers to it, and references to it.

What problems are you seeing when you try it?
Vect* vect is a pointer to a Vect object. Without going into a whole lot, there isn't much difference in the way you access it's members. With a typical object, you use the dot operator or "." but with object pointers (like vect) you use (I honestly don't know the name of it, pointer operator?) this "->" to access the members. A quick example using strings:
1
2
3
4
5
std::string myString("Hello, World!");                       // Typical object
std::string *myStringPtr = new std::string("Hello, World!"); // Object pointer

std::cout << "myString's length is: " << myString.length() << "\n";
std::cout << "myStringPtr's length is: " << myStringPtr->length() << "\n";
myString's length is: 13
myStringPtr's length is: 13


Sorry if I made any mistakes, but it looks right...I need to stop programming once the sun starts to come up.
Last edited on
Well when I try using the vect-> none of the members showed up even though on vObj1 etc they do, is that just the IDEs fault?
Code::Blocks has some issues with code completion. It's not always it's fault, sometimes it's a slow computer, sometimes it hasn't parsed it yet. Are your object vectors being declared as pointers or objects though? There is a lot that I don't see in the code so I can't say for certain, but code completion on my machine is almost non existent and it's killing me. vect-> is right, however if the code completion is a necessity for you, I'd try over on the forums there.

Are you running a nightly build or the release?
class Vect : public std::vector

1. Don't inherit from std::vector, it's not polymorphic. (it's also not a type, so this won't compile, but I guess you're showing pseudocode)

file.Write( &( *vect[ 0u ] ), vSize )

2. vectors (and classes derived from them) are not trivially-copyable, which means they cannot be serialized with wxFile::Write().

If the elements held by the vector (Obj1 and Obj2?) are in fact trivially-copiable, you could use file.write( &(*vect)[0], vect->size() * objSize);, that is, take the address of the element at position zero, and write sizeof that element times size of the vector, if you really want to write some unportable binary data. What's wrong with text, anyway?
Last edited on
The point in doing it as binary is to skip the analyzing of text to provide a speedup during runtime after it has already been analyzed, the lists themselves have potential to get really large hence the need to keep them out of runtime memory whenever there is no read/write action on them, as for the polymorphic and that I didn't know so thanks.
This is what I want to make smaller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
bool G::CheckBin~( Text& path, int inMode )
{
	if ( inMod < 0 )
		return false;
	bool isCfg = false;
	Text hackPath;
	TxtA dirA;
	int listMode = m_ListNow;
	m_ListNow = inMode;
	if ( !CheckPaths( isCfg, path, hackPath, dirA ) )
		return false;
	m_ListNow = listMode;
	return true;
}
bool G::SaveOrgB( OrgV& vOrg )
{
	Text path;
	CheckBin~( path, HEX_LIST_ORG );
	wxFile file;
	if ( !file.Create( path + wxT("org.bin~"), true ) )
		return false;
	size_t vSize = ( sizeof( Org ) * vOrg.size() );
	if ( file.Write( &vOrg[ 0u ],  vSize ) != vSize )
		return false;
	return file.Close();
}
bool G::SavePfmB( PfmV& vPfm )
{
	Text path;
	CheckBin~( path, HEX_LIST_PFM );
	wxFile file;
	if ( !file.Create( path + m_org.fileOld + wxT(".bin~"), true ) )
		return false;
	size_t vSize = ( sizeof( Pfm ) * vPfm.size() );
	if ( file.Write( &vPfm[ 0u ],  vSize ) != vSize )
		return false;
	return file.Close();
}
bool G::SaveBinB( BinV& vBin )
{
	Text path;
	CheckBin~( path, HEX_LIST_BIN );
	wxFile file;
	if ( !file.Create( path + m_pfm.fileOld + wxT(".bin~"), true ) )
		return false;
	size_t vSize = ( sizeof( Bin ) * vBin.size() );
	if ( file.Write( &vBin[ 0u ],  vSize ) != vSize )
		return false;
	return file.Close();
}
bool G::SavePflB( PflV& vPfl )
{
	Text path;
	CheckBin~( path, HEX_LIST_PFL );
	wxFile file;
	if ( !file.Create( path + m_bin.fileOld + wxT(".bin~"), true ) )
		return false;
	size_t vSize = ( sizeof( Pfl ) * vPfl.size() );
	if ( file.Write( &vPfl[ 0u ],  vSize ) != vSize )
		return false;
	return file.Close();
}
bool G::SaveDatB( Vect* vDat, int inMode )
{

}
bool G::SaveHackB( HckV& vHck )
{
	Text path;
	CheckBin~( path, HEX_LIST_HCK );
	wxFile file;
	if ( !file.Create( path + m_pfl.fileOld + wxT(".bin~"), true ) )
		return false;
	size_t vSize = ( sizeof( Hack ) * vHck.size() );
	if ( file.Write( &vHck[ 0u ],  vSize ) != vSize )
		return false;
	return file.Close();
}

And this is the header code it relies on ( ignoring the irrelevant stuff ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Org
{
public:
	Text fileNow;
	Text fileOld;
	Text nameNow;
	Text nameOld;
};

class Pfm : public Org
{
public:
	Pfm( void );
	int endian;
};

class Ram
{
public:
	Ram( void );
	ui64 addr;
	ui08 depth;
	ui64 bytes;
	Text name;
};

class Bin : public Org
{
public:
	Bin( void );
	~Bin( void );
	Ram& operator[] ( unsigned int );
	Vect< Ram > ram;
	Text name;
	Text path;
	ui08 type;
};

class Pfl : public Org
{
public:
	ui16 profile;
	Text serial;
	Text notes;
};

class OrgV : public Vect< Org > {};
class PfmV : public Vect< Pfm > {};
class BinV : public Vect< Bin > {};
class PflV : public Vect< Pfl > {};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Code : public Vect< ui08 >
{
public:
	Code( void );
	void cdeClear( void );
	// TSRPLLLL
	ui08 cdeType;
	ui08 cdeSize;
	ui08 cdeRam;
	ui08 cdeDepth; // Depth of pointer
	ui16 cdeLoop;
	// HHMMIIII
	ui08 cdeHex; // How to interpret the value
	ui08 cdeMode; // 0 = Normal ( Plain write / copy ), 1 = Display ( Read out current value into GUI, write what is displayed in GUI, special code types blocked )
	ui16 cdeInfo;
	/* VVVVVVVV XXXXXXXX Or
	XXXXXXXX XXXXXXXX
	VVVVVVVV VVVVVVVV in this order:
	Address, Value, Modify Address, Modify Value */
	ui64 cdeAddr[2]; // 2nd Value used for modifying addresss
};

class Hack : public Vect< Code >
{
public:
	Hack( void );
	void hckClear( void );
	Text hckName;
	/* PPPPHHHH UUTTOOOO
	Profile Id, Hack Id, Use & / or Radio List, Code Total ( Uncounted will be added still ), Parent(Owner) Id */
	ui16 hckParent;
	ui08 hckUse;
	TrId hckItem;
};

class xsDLL HckV : public Vect< Hack > {};
Just updated to Code::Blocks v12rc1 and I'm no longer having the issue so it must have been IDE specific
Topic archived. No new replies allowed.