Click here to Skip to main content
15,899,026 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: strcmp for Last five characters Pin
Hristo-Bojilov25-May-10 8:04
Hristo-Bojilov25-May-10 8:04 
GeneralRe: strcmp for Last five characters Pin
David Crow25-May-10 8:22
David Crow25-May-10 8:22 
GeneralRe: strcmp for Last five characters Pin
asadullah ansari25-May-10 16:35
asadullah ansari25-May-10 16:35 
QuestionPlease Explain One Instruction Pin
computerpublic24-May-10 17:58
computerpublic24-May-10 17:58 
AnswerRe: Please Explain One Instruction Pin
Stephen Hewitt24-May-10 18:03
Stephen Hewitt24-May-10 18:03 
AnswerRe: Please Explain One Instruction Pin
CPallini24-May-10 20:45
mveCPallini24-May-10 20:45 
AnswerRe: Please Explain One Instruction Pin
ThatsAlok25-May-10 1:52
ThatsAlok25-May-10 1:52 
QuestionObject Oriented Problem. Pin
RobNO24-May-10 13:49
professionalRobNO24-May-10 13:49 
Hey everyone, my problem is I keep getting compile errors and i can not pin point the problem
errors:
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2> c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(71) : see reference to class template instantiation 'CountedPtr<T>' being compiled
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C2143: syntax error : missing ',' before '&'
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2> c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\refsem1.cpp(11) : see reference to class template instantiation 'CountedPtr<T>' being compiled
2> with
2> [
2> T=int
2> ]
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C2143: syntax error : missing ',' before '&'

.hpp file
//ect/countprt.hpp

#ifndef COUNTED_PTR_HPP
#define COUNTED_PTR_HPP

/*class template for smart pointer with reference semantics
 *	-destroys the object that is referred to when the last CountedPTr that refers to it is destroyed
 */
template <typename T>
class CountedPtr
{
private:
	T* ptr;				//pointer to the actual object
	long* count;		//reference to the number of pointers that refer to it

public:
	//initialize with ordinary pointer
	//	-p has to be a value returned by new
	explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
	{

	}

	//copy constructor
	CountedPtr(const CountedPtr<T>& p) throw() : ptr(p.ptr), count(p.count)		//copy object and counter
	{
		++count;																//increment number of references
	}

	//destructor
	~CountedPtr() throw()
	{
		release();		//release reference to the object
	}

	//assignment
	CountedPtr<T>& operator= (const CounterPtr<T>& p) throw ()
	{
		if (this != &p)		//if not a reference to itself
		{
			release();		//release reference to old object
			ptr = p.ptr;	//copy new object
			count = p.count;//copy counter
			++*count;
		}
		return *this;
	}

	//access to the object
	T& operator*() const throw ()
	{
		return *ptr;
	}

	//acces to a member of the object
	T* operator->() const throw()
	{
		return ptr;
	}

private:
	void release()
	{
		++*count;			//decrement number of references
		if (*count == 0)	//if last reference
		{
			delete count;	//destroy counter
			delete ptr;		//destroy object
		}
	}
};

#endif		//COUNTED_PTR_HPP


.cpp file
//ect_1/refsem1.cpp

#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include "countptr.hpp"
using namespace std;

void printCountedPtr(CountedPtr<int> elem)
{
		cout << *elem << ' ';
}

int main()
{
	//type for smart pointer for this purpose
	typedef CountedPtr<int> IntPtr;

	//two different sets
	deque<IntPtr> coll1;
	list <IntPtr> coll2;

	//array of initial ints
	static int values [] = {3,5,9,1,6,4};

	/*insert newly created ints in the sets with reference semantics
	 *	-same sequence in coll1
	 *	-reversed sequence in coll2
	 */
	for (unsigned i = 0; i < sizeof(values)/sizeof(values[0]); ++i)
	{
		IntPtr ptr(new int(values[i]));
		coll1.push_back(ptr);
		coll2.push_front(ptr);
	}

	//output content of both sets
	for_each(coll1.begin(), coll1.end(), printCountedPtr);
	cout << endl;
	for_each(coll2.begin(), coll2.end(), printCountedPtr);
	cout << endl;

	/*modify elements of the sets in different places
	 *	-square third value in coll1
	 *	-negate first value in coll1
	 *	-set first value in coll2 to 0
	 */
	*coll1[2] *= *coll1[2];
	(**coll1.begin()) *= -1;
	(**coll2.begin()) = 0;

	//output content of both sets again
		for_each(coll1.begin(), coll1.end(), printCountedPtr);
	cout << endl;
	for_each(coll2.begin(), coll2.end(), printCountedPtr);
	cout << endl;

}

Thanks For all the help, Big Grin | :-D
RobNO
AnswerRe: Object Oriented Problem. Pin
Stephen Hewitt24-May-10 14:08
Stephen Hewitt24-May-10 14:08 
GeneralRe: Object Oriented Problem. Pin
RobNO24-May-10 14:11
professionalRobNO24-May-10 14:11 
JokeRe: Object Oriented Problem. Pin
Peter_in_278024-May-10 14:13
professionalPeter_in_278024-May-10 14:13 
GeneralRe: Object Oriented Problem. Pin
Stephen Hewitt24-May-10 14:14
Stephen Hewitt24-May-10 14:14 
GeneralRe: Object Oriented Problem. Pin
ThatsAlok25-May-10 1:53
ThatsAlok25-May-10 1:53 
AnswerRe: Object Oriented Problem. Pin
Peter_in_278024-May-10 14:12
professionalPeter_in_278024-May-10 14:12 
AnswerRe: Object Oriented Problem. Pin
Sarath C24-May-10 14:14
Sarath C24-May-10 14:14 
QuestionNot able to drag the pane when creating a splitter in MDI Applicaton. Pin
janaswamy uday24-May-10 5:44
janaswamy uday24-May-10 5:44 
Questionusing std::find in while loop Pin
b-rad31124-May-10 5:09
b-rad31124-May-10 5:09 
AnswerRe: using std::find in while loop Pin
josda100024-May-10 5:15
josda100024-May-10 5:15 
GeneralRe: using std::find in while loop Pin
b-rad31124-May-10 5:19
b-rad31124-May-10 5:19 
GeneralRe: using std::find in while loop Pin
josda100024-May-10 5:27
josda100024-May-10 5:27 
GeneralRe: using std::find in while loop Pin
b-rad31124-May-10 5:28
b-rad31124-May-10 5:28 
AnswerRe: using std::find in while loop Pin
David Crow24-May-10 5:28
David Crow24-May-10 5:28 
GeneralRe: using std::find in while loop Pin
b-rad31124-May-10 5:45
b-rad31124-May-10 5:45 
AnswerRe: using std::find in while loop Pin
Richard MacCutchan24-May-10 5:33
mveRichard MacCutchan24-May-10 5:33 
AnswerRe: using std::find in while loop Pin
Aescleal24-May-10 6:01
Aescleal24-May-10 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.