944,030 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3945
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 16th, 2005
0

Class Vectorization requirements

Expand Post »
My project continues... But at least I'm getting better at using C/C++ and MFC.

Now, I'm at a critical point: dynamic memory allocation fundamentals.

I have a data class with a number of unsigned char arrays and longs that are addressed in structs (encapsulated by the class).

I'd like to make a vector of this data class. I have no use for the '==' and '<' operators, but I'll overload them anyway, but my problem is this:
I've got long arrays of unsigned chars (1000+spaces), and I intend to have about 250 different instances of this class in memory during regular operation.

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?


Additionally, the file where this is declared is #included by two files that I'm intentionally keeping apart for ease of coding.

I'm using the
C++ Syntax (Toggle Plain Text)
  1. #ifndef
  2. #def
  3.  
  4.  
  5. #endif
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.

How can I fix these problems?

Placing the overloads inside the class seems to fix the redefinition problem, but they now claim that the binary operator < has too many parameters. Doesn't 'bi' translate to two? As in, the two parameters that I'm trying to explicitly overload?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 16th, 2005
0

Re: Class Vectorization requirements

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!
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 16th, 2005
0

Re: Class Vectorization requirements

Ahh, yes. I knew that question was on the way.

Problem is: I'm an intern. Working with what should be considered legacy code, because it's from a tool that's several years old. The work is with ethernet packets, stripping headers and using sizeof() in a fascinating way to collect the data in c-style file operations. It works, I've gotten it working for a single packet, and I want to get this GUI working without gutting the entire code by switching all the C over to C++.

That's mainly because the original was written in C that was way over my head, and deals with the company's proprietary technology. I feel like I should leave things more or less as I found them, so that when I'm gone, this tool will still be used and maintainable by people who know (drumroll, please!) C much more strongly than C++.

If it were my project entirely, and not something I was doing for a resume, sure, I'd post the code and switch everything to C++. But as it stands, I am rather leery of making such a fundamental change in the short amount of (paid) time remaining.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 16th, 2005
0

Re: Class Vectorization requirements

Quote originally posted by Drowzee ...
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?
C++ Syntax (Toggle Plain Text)
  1. class myclass
  2. {
  3. ...
  4. char* array;
  5. // or
  6. char array[1000];
  7. }


Quote originally posted by Drowzee ...
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:
C++ Syntax (Toggle Plain Text)
  1. // myclass.h
  2. #ifndef _MYCLASS_H
  3. #define _MYCLASS_H
  4. class myclass
  5. {
  6. public:
  7. myclass();
  8. virtual ~myclass();
  9. // overload operator
  10. bool operator<(const char* str);
  11. bool operator==(const char* str);
  12. // global overloaded operator that is not part of the class
  13. friend bool operator<(myclass& mc,const char*s);
  14.  
  15. protected:
  16. char array[1024];
  17. };
  18. #endif // _MYCLASS_H
  19.  
  20. // myclass.cpp
  21. myclass::myclass()
  22. {
  23. // initialize array
  24. memset(array,0,sizeof(array));
  25. }
  26.  
  27.  
  28. bool myclass::operator<(const char*s)
  29. {
  30. return (strcmp(array,s) < 0) ? true : false;
  31. }
  32.  
  33. bool operator<(myclass& mc,const char* s)
  34. {
  35. return strcmp(mc.array,s) == 0 ? true : false;
  36. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 17th, 2005
0

Re: Class Vectorization requirements

After an intial success, the vector is giving me problems. So, since I can show this public-domain info...

C++ Syntax (Toggle Plain Text)
  1. #ifndef CPackethdr
  2. #define CPackethdr
  3. class CPacket //Encapsulates data structs and associated information for easier instantiation.
  4. {
  5. public:
  6. CPacket();
  7. virtual ~CPacket();
  8.  
  9. bool operator<(const char* str); //Included for the purpose of vectorization, but not used.
  10. bool operator==(const char* str);
  11.  
  12. struct PeekPacket7
  13. {
  14. unsigned short fProtoSpec;
  15. unsigned short fPacketLength;
  16. unsigned short fSliceLength;
  17. unsigned char fFlags;
  18. unsigned char fStatus;
  19. unsigned long fTimeStampHi;
  20. unsigned long fTimeStampLo;
  21. } ppacket;
  22.  
  23. struct PacketHeader
  24. {
  25. unsigned char fDestAddr[6];
  26. unsigned char fSrcAddr[6];
  27. unsigned short fProtocol;
  28. unsigned char fPacketData[1500]; /* max packet size */
  29. } enetpacket;
  30.  
  31. int length;
  32. unsigned long packetCount;
  33. char timeString[16];
  34.  
  35. };
  36. #endif

With the following in the .cpp...

C++ Syntax (Toggle Plain Text)
  1.  
  2. CPacket::CPacket()
  3. {
  4. ppacket.fProtoSpec = 0;
  5. ppacket.fPacketLength = 0;
  6. ppacket.fSliceLength=0;
  7. ppacket.fFlags = 0;
  8. ppacket.fStatus = 0;
  9. ppacket.fTimeStampHi= 0L;
  10. ppacket.fTimeStampLo= 0L;
  11.  
  12. memset(enetpacket.fDestAddr, 0, sizeof(enetpacket.fDestAddr));
  13. memset(enetpacket.fSrcAddr,0,sizeof(enetpacket.fSrcAddr));
  14. enetpacket.fProtocol= 0;
  15. memset(enetpacket.fPacketData,0,sizeof(enetpacket.fPacketData));
  16.  
  17. length = 0;
  18. packetCount = 0;
  19. memset(timeString,0,sizeof(timeString));
  20. }
  21.  
  22. CPacket::~CPacket()
  23. {}
  24.  
  25.  
  26. bool CPacket::operator<(const char* str)
  27. {
  28. return TRUE;
  29. }
  30.  
  31. bool CPacket::operator==(const char* str)
  32. {
  33. return TRUE;
  34. }

And I'd like to declare the vector in another class in the same header file. Why? Because this is an MFC application, and doesn't have a 'main' function to make things simple to place.
When I call
C++ Syntax (Toggle Plain Text)
  1. vector<CPacket> packlist(40);
In the second class of the header file, the compiler lists the following errors:
Quote ...
error C2143: syntax error : missing ';' before '<'
error C2501: 'vector' : missing storage-class or type specifiers
error C2059: syntax error : '<'
error C2238: unexpected token(s) preceding ';'
Is this because of my friendless operator overloading?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 17th, 2005
0

Re: Class Vectorization requirements

Did you #include <vector>?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 17th, 2005
0

Re: Class Vectorization requirements

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.
C++ Syntax (Toggle Plain Text)
  1. memset(&ppacket,0,sizeof(ppacket));
  2. 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:
C++ Syntax (Toggle Plain Text)
  1. class CMyDocument : public CDocument
  2. {
  3. ...
  4. private:
  5. vector<CPacket> packlist;
  6. };
  7.  
  8. // class constructor
  9. CMyDocument::CMyDocument()
  10. {
  11. packlist.resize(40);
  12.  
  13.  
  14. };
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 17th, 2005
0

Re: Class Vectorization requirements

Thanks for the advice regarding the initalizers. That certainly cleans things up nicely

Quote originally posted by Dave Sinkula ...
Did you #include <vector>?
Yes.

And the vector declaration is in the CDocument derived class, which is why I'm confused.

I have tried initalizing the vector in the class then resizing, and it doesn't work.
When I try to put the declaration in the CDoc derived class constructor, it gives the errors:
Quote ...
error C2065: 'vector' : undeclared identifier
error C2275: 'CPacket' : illegal use of this type as an expression
see declaration of 'CPacket'
error C2065: 'packlist' : undeclared identifier
However: when I wrote the vector call, the microsoft tooltip/little helperbox came up with:
Quote ...
<typename _TY, typename _A=allocator<_TY>>
and could respond to my request to go to the defintion of vector (though it brought up a dialog box to resolve ambiguity).
I think that means it knows dang well what I want.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 17th, 2005
0

Re: Class Vectorization requirements

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;
}
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 17th, 2005
0

Re: Class Vectorization requirements

Hm. Looks like the addition of std:: was what it wanted.
Sorry about being such a stubborn sumbit with giving out the information...

Frikkin' Sweet. It's working.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: libtiff
Next Thread in C++ Forum Timeline: hh:mm:ss format api





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC