Class Vectorization requirements

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Class Vectorization requirements

 
0
  #1
Aug 16th, 2005
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
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Class Vectorization requirements

 
0
  #2
Aug 16th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Class Vectorization requirements

 
0
  #3
Aug 16th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Class Vectorization requirements

 
0
  #4
Aug 16th, 2005
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?
  1. class myclass
  2. {
  3. ...
  4. char* array;
  5. // or
  6. char array[1000];
  7. }


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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Class Vectorization requirements

 
0
  #5
Aug 17th, 2005
After an intial success, the vector is giving me problems. So, since I can show this public-domain info...

  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...

  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
  1. vector<CPacket> packlist(40);
In the second class of the header file, the compiler lists the following errors:
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class Vectorization requirements

 
0
  #6
Aug 17th, 2005
Did you #include <vector>?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Class Vectorization requirements

 
0
  #7
Aug 17th, 2005
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.
  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:
  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. };
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Class Vectorization requirements

 
0
  #8
Aug 17th, 2005
Thanks for the advice regarding the initalizers. That certainly cleans things up nicely

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:
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:
<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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Class Vectorization requirements

 
0
  #9
Aug 17th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Class Vectorization requirements

 
0
  #10
Aug 17th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC