you are using c++ -- why are you mixing c-style char arrays when you could be using std::string class that will do all the allocation for you. Post your class and we'll see how to get rid of those char arrays!
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
How do I make a constructor for those unsigned char arrays? Should I make a loop, or can I just initialize the first element of an array and call it good?
c-style char arrays do not have constructors. you must allocate the memory in the container class. Are the arrays pointers what need to be allocated? or arrays of hard-coded size?
class myclass
{
...
char* array;
// or
char array[1000];
}
directives to isolate the class to one defintion, but for some reason...If I put this class in there, only the class itself is protected by the preprocessor. If I put the class constructor outside the terminating bracket for the class, I get "already defined in suchandsuch.obj" errors.
And it's the same with the overloaded operator functions.
you don't put executable code (except inline code) in header files for the very reason that you state -- duplicate functions. Instead, put the class declaration inside the defines and put the implementation code in a *.cpp file. Same for overloaded operators.
You need to post some code before anyone can give you exact answers to your questions. Here is an example:
// myclass.h
#ifndef _MYCLASS_H
#define _MYCLASS_H
class myclass
{
public:
myclass();
virtual ~myclass();
// overload operator
bool operator<(const char* str);
bool operator==(const char* str);
// global overloaded operator that is not part of the class
friend bool operator<(myclass& mc,const char*s);
protected:
char array[1024];
};
#endif // _MYCLASS_H
// myclass.cpp
myclass::myclass()
{
// initialize array
memset(array,0,sizeof(array));
}
bool myclass::operator<(const char*s)
{
return (strcmp(array,s) < 0) ? true : false;
}
bool operator<(myclass& mc,const char* s)
{
return strcmp(mc.array,s) == 0 ? true : false;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
here's an easier way to initialize the structures. It's not necessary to initialize each field individually, just flood the entire structure with 0s.
memset(&ppacket,0,sizeof(ppacket));
memset(&enetpacket,0,sizeof(enetpacket));
In MFC application you can put that vectory array in any of the MFC classes -- most probably either CWin-derived or CDocument-derived class, depending what all needs access to the vector. you may not be able to initialize the vector's size like you posted, but do it in two stages:
class CMyDocument : public CDocument
{
...
private:
vector<CPacket> packlist;
};
// class constructor
CMyDocument::CMyDocument()
{
packlist.resize(40);
};
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
works for me. Maybe your problem is that you did not include CPacket.h in the document.h. I just put them in the same file, but you can also have them in two different files and include cpacket.h in the other .h.
#if !defined(AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_)
#define AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <vector>
class CPacket //Encapsulates data structs and associated information for easier instantiation.
{
public:
CPacket();
virtual ~CPacket();
bool operator<(const char* str); //Included for the purpose of vectorization, but not used.
bool operator==(const char* str);
struct PeekPacket7
{
unsigned short fProtoSpec;
unsigned short fPacketLength;
unsigned short fSliceLength;
unsigned char fFlags;
unsigned char fStatus;
unsigned long fTimeStampHi;
unsigned long fTimeStampLo;
} ppacket;
struct PacketHeader
{
unsigned char fDestAddr[6];
unsigned char fSrcAddr[6];
unsigned short fProtocol;
unsigned char fPacketData[1500]; /* max packet size */
} enetpacket;
int length;
unsigned long packetCount;
char timeString[16];
};
class CTry5Doc : public CDocument
{
protected: // create from serialization only
CTry5Doc();
DECLARE_DYNCREATE(CTry5Doc)
// Attributes
public:
std::vector<CPacket> theList;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTry5Doc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CTry5Doc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CTry5Doc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_)
BOOL CTry5Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
theList.resize(40);
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
One of the employees informed me that nesting structs inside the class was not good coding practice. How should I clean it up?
Here is my first reaction to the suggestion.
Instead of this:
#if !defined(AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_)
#define AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <vector>
class CPacket //Encapsulates data structs and associated information for easier instantiation.
{
public:
CPacket();
virtual ~CPacket();
bool operator<(const char* str); //Included for the purpose of vectorization, but not used.
bool operator==(const char* str);
struct PeekPacket7
{
unsigned short fProtoSpec;
unsigned short fPacketLength;
unsigned short fSliceLength;
unsigned char fFlags;
unsigned char fStatus;
unsigned long fTimeStampHi;
unsigned long fTimeStampLo;
} ppacket;
struct PacketHeader
{
unsigned char fDestAddr[6];
unsigned char fSrcAddr[6];
unsigned short fProtocol;
unsigned char fPacketData[1500]; /* max packet size */
} enetpacket;
int length;
unsigned long packetCount;
char timeString[16];
};
Do this:
#if !defined(AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_)
#define AFX_TRY5DOC_H__BA63042D_EFBE_45CF_A864_A01C9FB8E010__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <vector>
struct PeekPacket7
{
unsigned short fProtoSpec;
unsigned short fPacketLength;
unsigned short fSliceLength;
unsigned char fFlags;
unsigned char fStatus;
unsigned long fTimeStampHi;
unsigned long fTimeStampLo;
};
struct PacketHeader
{
unsigned char fDestAddr[6];
unsigned char fSrcAddr[6];
unsigned short fProtocol;
unsigned char fPacketData[1500]; /* max packet size */
};
class CPacket //Encapsulates data structs and associated information for easier instantiation.
{
public:
CPacket();
virtual ~CPacket();
bool operator<(const char* str); //Included for the purpose of vectorization, but not used.
bool operator==(const char* str);
struct PeekPacket7 ppacket;
struct PacketHeader enetpacket;
int length;
unsigned long packetCount;
char timeString[16];
};
Or further move things by defining the structs in their own headers and #include them.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Would it be worthwhile to typedef them as well?
In C++ that's already done automatically. And in C I prefer not to.I use sizeof later on in grabbing information, so... might as well simplify the call, correct?Why? Usingsizeof on the object rather than it's presumed type is safer anyway.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
One last question:
One of the employees informed me that nesting structs inside the class was not good coding practice. How should I clean it up?
that's a matter of opinion, program design, and corporate coding standards. coding a structure inside a class limits the structure's scope to that class. I, for one, see nothing wrong with that and have used it myself.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343