473,763 Members | 8,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Smart Pointer help

Here's a non intrusive reference counting smart pointer class I'm
working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
declaration of `SmrtPtrDB' with no type" error.

Code:

SmrtPtr.hpp

#pragma once

template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
{
DataBase.add();
for(;;)
{
if(isInvalid())
delete this;
}
}
SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.ob j){DataBase.add ()}
~SmrtPtr(){dele te ptr; DataBase.sub()}
T& operator*(){ret urn *ptr;}
T* operator->(){return ptr;}
T** operator&(){ret urn &ptr;}
private:
static SmrtPtrDB<TData Base;
bool isInvalid()
{
for(;;)
if(!DataBase.st atus())
return true;
else return false;
}
T* ptr;
};
SmrtPtrDB.hpp

#pragma once
#include "SmrtPtr.hp p"

template<class T>
class SmrtPtrDB
{
public:
SmrtPtrDB():num (0){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--);
int status(){return num;}
private:
int num;
};

Could you help me out on this? I'm not even sure if I'm coding the
non-intrusive reference counting correctly. Thanks!!!!!

Jul 4 '06 #1
13 2101

Protoman wrote:
Here's a non intrusive reference counting smart pointer class I'm
working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
declaration of `SmrtPtrDB' with no type" error.

Code:

SmrtPtr.hpp

#pragma once

template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
{
DataBase.add();
for(;;)
{
if(isInvalid())
delete this;
}
}
SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.ob j){DataBase.add ()}
~SmrtPtr(){dele te ptr; DataBase.sub()}
T& operator*(){ret urn *ptr;}
T* operator->(){return ptr;}
T** operator&(){ret urn &ptr;}
private:
static SmrtPtrDB<TData Base;
bool isInvalid()
{
for(;;)
if(!DataBase.st atus())
return true;
else return false;
}
T* ptr;
};
SmrtPtrDB.hpp

#pragma once
#include "SmrtPtr.hp p"

template<class T>
class SmrtPtrDB
{
public:
SmrtPtrDB():num (0){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--);
int status(){return num;}
private:
int num;
};

Could you help me out on this? I'm not even sure if I'm coding the
non-intrusive reference counting correctly. Thanks!!!!!
You use the name 'SmrtPtrDB' in file 'SmrtPtr.hpp' before any
declaration.

HTH

Jul 4 '06 #2
TB
Protoman skrev:
Here's a non intrusive reference counting smart pointer class I'm
working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
declaration of `SmrtPtrDB' with no type" error.

Code:
<snip>
>
SmrtPtrDB.hpp

#pragma once
#include "SmrtPtr.hp p"

template<class T>
class SmrtPtrDB
{
public:
SmrtPtrDB():num (0){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--);
void sub() { num--; }

--
TB @ SWEDEN
Jul 4 '06 #3
Protoman wrote:
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
{
DataBase.add();
for(;;)
{
if(isInvalid())
delete this;
}
}
SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.ob j){DataBase.add ()}
~SmrtPtr(){dele te ptr; DataBase.sub()}
T& operator*(){ret urn *ptr;}
T* operator->(){return ptr;}
T** operator&(){ret urn &ptr;}
private:
static SmrtPtrDB<TData Base;
bool isInvalid()
{
for(;;)
if(!DataBase.st atus())
return true;
else return false;
}
T* ptr;
};
Is there a particular reason that you do not use whitespace?

I corrected several minor typos. Your error message arises since you use
SmrtPtrDB before defining it. Put both classes in the same file:

template<class T>
class SmrtPtrDB{
public:

SmrtPtrDB () :num (0) {}

~SmrtPtrDB () {}

void add() { num++; }

void sub() { num--; }

int status() { return num; }

private:

int num;

};

template<class T>
class SmrtPtr {
public:

explicit SmrtPtr ( T* obj )
: ptr ( obj )
{
DataBase.add() ;
for (;;) {
if ( isInvalid() ) {
delete this;
}
}
/*
What is this loop supposed to accomplish? Why would it terminate?
*/
}

SmrtPtr ( const SmrtPtr<T>& rhs)
:ptr (rhs.obj)
{
DataBase.add();
}

~SmrtPtr() {
delete ptr;
DataBase.sub();
}

T& operator*() { return *ptr; }

T* operator->() { return ptr; }

T** operator&() { return &ptr; }

private:

static SmrtPtrDB<TData Base;
/*
static? Why do you want to have one counter per type. One would expect a
counter per object.
*/
bool isInvalid()
{
for (;;) {
if (!DataBase.stat us() ) {
return true;
} else {
return false;
}
}
/*
This loop will never loop more than once.
*/
}

T* ptr;

};
What is this smart-pointer class supposed to accomplish?

Best

Kai-Uwe Bux
Jul 4 '06 #4

Protoman wrote:
Here's a non intrusive reference counting smart pointer class I'm
working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
declaration of `SmrtPtrDB' with no type" error.

Code:

SmrtPtr.hpp

#pragma once
non-standard pragma.
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
reasonable so far although most smart-pointers have an implicit
constructor from the pointer type. Allows you to do this:

SmrtPtr< T getT()
{
return new T( params );
}
{
DataBase.add();
for(;;) // never ending loop, there is no "break"
{
if(isInvalid())
delete this;
}
delete this can be used only on classes created on the heap (i.e. with
new). Most smart pointers are created on the stack. Self-deletion would
be undefined. Note that this line will not cause loop termination.
}
SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.ob j){DataBase.add ()}
~SmrtPtr(){dele te ptr; DataBase.sub()}
T& operator*(){ret urn *ptr;}
T* operator->(){return ptr;}
These two should possibly be const functions. Not that they will return
pointers to const. (If you want that you use SmrtPtr< const T >) but
because it allows you to use these on temporaries.
T** operator&(){ret urn &ptr;}
very unusual to overload this.
private:
static SmrtPtrDB<TData Base;
There will be a DataBase for each type T, not for each object being
pointed to.
bool isInvalid()
another non-const function that probably should be const.
{
for(;;)
this loop at least will end beacuse you return in the middle.
if(!DataBase.st atus())
return true;
else return false;
}
If you are going to test a boolean condition then return the result
directly, thus:

return !Database.statu s();
T* ptr;
};
SmrtPtrDB.hpp

#pragma once
#include "SmrtPtr.hp p"

template<class T>
class SmrtPtrDB
{
public:
SmrtPtrDB():num (0){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--);
int status(){return num;}
private:
int num;
};
Could you help me out on this? I'm not even sure if I'm coding the
non-intrusive reference counting correctly. Thanks!!!!!
But you're reference counting the wrong thing. If you're not actually
going to use tr1::shared_ptr / boost::shared_p tr or Loki then at least
look up the source for boost or Loki to see how it's done. If their
code in places looks rather complex, that is because writing a good
non-intrusive smart-pointer is not as trivial as it first seems.
(Actually some of the complexity in boost comes from sharing code with
other types of smart-pointer. Much of the complexity also comes from
custom-deleters, automatic type-conversion and portability across
libraries).

Jul 4 '06 #5

Earl Purple wrote:
Protoman wrote:
Here's a non intrusive reference counting smart pointer class I'm
working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
declaration of `SmrtPtrDB' with no type" error.

Code:

SmrtPtr.hpp

#pragma once

non-standard pragma.
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)

reasonable so far although most smart-pointers have an implicit
constructor from the pointer type. Allows you to do this:

SmrtPtr< T getT()
{
return new T( params );
}
{
DataBase.add();
for(;;) // never ending loop, there is no "break"
{
if(isInvalid())

delete this;
}

delete this can be used only on classes created on the heap (i.e. with
new). Most smart pointers are created on the stack. Self-deletion would
be undefined. Note that this line will not cause loop termination.
}
SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.ob j){DataBase.add ()}
~SmrtPtr(){dele te ptr; DataBase.sub()}
T& operator*(){ret urn *ptr;}
T* operator->(){return ptr;}

These two should possibly be const functions. Not that they will return
pointers to const. (If you want that you use SmrtPtr< const T >) but
because it allows you to use these on temporaries.
T** operator&(){ret urn &ptr;}
very unusual to overload this.
private:
static SmrtPtrDB<TData Base;

There will be a DataBase for each type T, not for each object being
pointed to.
bool isInvalid()

another non-const function that probably should be const.
{
for(;;)

this loop at least will end beacuse you return in the middle.
if(!DataBase.st atus())
return true;
else return false;
}

If you are going to test a boolean condition then return the result
directly, thus:

return !Database.statu s();
T* ptr;
};
SmrtPtrDB.hpp

#pragma once
#include "SmrtPtr.hp p"

template<class T>
class SmrtPtrDB
{
public:
SmrtPtrDB():num (0){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--);
int status(){return num;}
private:
int num;
};
Could you help me out on this? I'm not even sure if I'm coding the
non-intrusive reference counting correctly. Thanks!!!!!

But you're reference counting the wrong thing. If you're not actually
going to use tr1::shared_ptr / boost::shared_p tr or Loki then at least
look up the source for boost or Loki to see how it's done. If their
code in places looks rather complex, that is because writing a good
non-intrusive smart-pointer is not as trivial as it first seems.
(Actually some of the complexity in boost comes from sharing code with
other types of smart-pointer. Much of the complexity also comes from
custom-deleters, automatic type-conversion and portability across
libraries).
OK, now I'm getting errors like:

4 C:\Dev-Cpp\9.cpp expected nested-name-specifier before "namespace"
6 C:\Dev-Cpp\SmrtPtrDB.h pp class `SmrtPtrDB' does not have any field
named `num'
8 C:\Dev-Cpp\SmrtPtrDB.h pp `num' undeclared (first use this function)

Here's the code:

SmrtPtr.hpp

#pragma once
#include "SmrtPtrDB. hpp"

template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
{
DataBase.add();
for(;;)
{
if(isInvalid())
{
this->~SmrtPtr();
break;
}
}
}
SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.ob j){DataBase.add ()}
~SmrtPtr(){dele te ptr; DataBase.sub()}
T& operator*()cons t{return *ptr;}
T* operator->()const{retu rn ptr;}
T** operator&()cons t{return &ptr;}
private:
static SmrtPtrDB DataBase;
bool isInvalid()cons t
{
for(;;)
return!DataBase .status();
}
T* ptr;
};

SmrtPtrDB.hpp

#pragma once

class SmrtPtrDB
{
public:
SmrtPtrDB():num (0){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--);
int status(){return num;}
private:
int num;
};

9.cpp //main

#include <iostream>
#include <cstdlib>
#include "SmrtPtr.hp p"
using namespace std;

int main()
{
SmrtPtr<intptr( new int);
SmrtPtr<intptr2 (ptr);
delete ptr2;
system("PAUSE") ;
return EXIT_SUCCESS;
}

I have no idea what's the problem now. Thanks!!!!

Jul 4 '06 #6
Protoman wrote:
Earl Purple wrote:
>>Protoman wrote:
>>>Here's a non intrusive reference counting smart pointer class I'm
working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
declaratio n of `SmrtPtrDB' with no type" error.
>
I have no idea what's the problem now. Thanks!!!!
You use non-standard pragmas, omit whitespace and don't fix the typos
identified in previous responses?

--
Ian Collins.
Jul 4 '06 #7

Protoman wrote:
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
{
DataBase.add();
for(;;)
{
if(isInvalid())
{
this->~SmrtPtr();
break;
}
}
}
You should only explicitly call a destructor when you have constructed
with placement new. Why should your smart-pointer have been constructed
this way?

You haven't really fixed your problem.

Just look at boost and loki to see how to write smart-pointers. And
then only write your own if you really need something that boost and
loki don't already support.

Jul 5 '06 #8

Earl Purple wrote:
Protoman wrote:
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr(T* obj):ptr(obj)
{
DataBase.add();
for(;;)
{
if(isInvalid())
{
this->~SmrtPtr();
break;
}
}
}

You should only explicitly call a destructor when you have constructed
with placement new. Why should your smart-pointer have been constructed
this way?

You haven't really fixed your problem.

Just look at boost and loki to see how to write smart-pointers. And
then only write your own if you really need something that boost and
loki don't already support.
I'm writing this for the learning experience, not b/c I need it.

Jul 5 '06 #9
Protoman posted:

>Just look at boost and loki to see how to write smart-pointers. And
then only write your own if you really need something that boost and
loki don't already support.

I'm writing this for the learning experience, not b/c I need it.

With that attitude you'll become a very proficient programmer indeed.
--

Frederick Gotham
Jul 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1805
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles, various books and ... whatever I could find on the subject :-) I'll leave out the reference counter part because its pretty basic, but my reference counter class is called xObject. My smart pointer class is called xPointer.
24
2201
by: Christopher Benson-Manica | last post by:
Is there anything wrong with my attempt (below) at implementing something resembling a smart pointer? template < class T > class SmartPointer { private: T *t; public:
27
3410
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
5
3042
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart) pointer, or reference Date: 27 Aug 2005 15:13:23 -0400 From: Neal Coombes <nealc@trdlnk.com> Organization: Aioe.org NNTP Server To: (Usenet) Newsgroups: comp.lang.c++.moderated
8
5153
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very large project. I ran the Doxygen program, and it ran for over 16 hours, before I had
92
5118
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5078
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
4
1741
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
50
4504
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.