4 Replies - 388 Views - Last Post: 10 August 2011 - 06:54 PM Rate Topic: -----

#1 DownUnder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-September 10

Problem linking a generic smart pointer template to a main method.

Posted 10 August 2011 - 04:11 AM

Hi there,

I am implementing a generic smart pointer program, it works with its own internal main method. But I want to link another c++ file and have all the main code in there. But after i thought I had done this correctly i get the error

"smart_pointer_main_simple.c:16:20: error: variable ‘smart_pointer<int> r’ has initializer but incomplete type"


Any help as to why this doesn't work would be appreciated, I am new to c++ so is most likely a simple error.


Thanks John.

This is the smart_pointer.h file

#ifndef MY_CLASS_H
#define MY_CLASS_H

template < typename T > class smart_pointer;
class RC;

#endif



This is the smart_pointer.c file, with the working generic smart_pointer template.
#include <iostream>
#include "stdio.h"

   class RC
    {
        public:
        int count; // Reference count
      
        void AddRef()
        {
            // Increment the reference count

            count++;
        }

        int Release()
        {
            // Decrement the reference count and

            // return the reference count.

            return --count;
        }
	 int getCount()
        {
            return count;
        }
    };



    template < typename T > class smart_pointer
    {
    public:
        T*    pData;       // pointer
        RC* reference; // Reference count

        smart_pointer(T* pValue) : pData(pValue), reference(0)
        {
            // Create a new reference
            reference = new RC();
            // Increment the reference count
            reference->AddRef();
        }

        smart_pointer(const smart_pointer<T>& smart) : pData(smart.pData), reference(smart.reference)
        {
            // Copy constructor
            // Copy the data and reference pointer

            // and increment the reference count
	    
            reference->AddRef();
        }

        ~smart_pointer()
        {
            // Destructor

            // Decrement the reference count

            // if reference become zero delete the data

            if(reference->Release() == 0)
            {
                delete pData;
                delete reference;
            }
        }
	 int getCount()
        {
            return reference->getCount();
        }


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

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


        smart_pointer<T>& operator = (const smart_pointer<T>& smart)
        {
            // Assignment operator

            if (this != &smart) // Avoid self assignment

            {
                // Decrement the old reference count

                // if reference become zero delete the old data

                if(reference->Release() == 0)
                {
                    delete pData;
                    delete reference;
                }

                // Copy the data and reference pointer
                // and increment the reference count
                pData = smart.pData;
                reference = smart.reference;
                reference->AddRef();
            }
            return *this;
        }
    };

    //Another test main method
        int main()
    {
	smart_pointer<int> p =new int();
	smart_pointer<int> r = new int();
	*r = 18;
	*p = 5;
	unsigned int addr=(unsigned int) &p;
	std::cout << "new p=" << *p << std::endl;
	std::cout << "p's address=" << addr << std::endl;
	std::cout << "p =" << p.getCount() << std::endl;



	return 0;
    }



And this is the external main file that I'm trying to link to the smart_pointer file.

#include <iostream>
#include "stdio.h"
#include "smart_pointer.h"


int main(){

smart_pointer<int> p = new int();

*p = 5;

smart_pointer<int> q =  new int();


smart_pointer<int> r = new int();


p = r;

q = p;

q = p = r = 0;

return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem linking a generic smart pointer template to a main method.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Problem linking a generic smart pointer template to a main method.

Posted 10 August 2011 - 05:20 AM

When using templates both the definition and implementation must be in the same file.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Problem linking a generic smart pointer template to a main method.

Posted 10 August 2011 - 08:37 AM

explicitly instantiate the template definitions.

example

SmartPointer.h
#ifndef SMART_POINTER_H
#define SMART_POINTER_H

template <typename T> class smart_pointer {
	public:
	smart_pointer(void);
};

#endif



SmartPointer.cpp
#include "SmartPointer.h"

template class smart_pointer<int>;      // explicit instantiation
template class smart_pointer<double>;      // explicit instantiation
template class smart_pointer<char>;      // explicit instantiation

template<typename T> smart_pointer::smart_pointer(void) {
}



However, this limits the template types in which you can instantiate your class. In the example above, the smart_pointer can only be instantiated with int, double, or char. Puttig the declaration and definition of smart_pointer all within the same header file can avoid linker issues as well.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2242
  • View blog
  • Posts: 9,228
  • Joined: 18-February 07

Re: Problem linking a generic smart pointer template to a main method.

Posted 10 August 2011 - 01:17 PM

Just a note -- it seems that you are potentially dangerously mixing C and C++

name your files .cpp or .cc not .c -- .c is for C programs and do not have templates or other fancy C++ features.

use the C++ standard headers for the C library. To do this you drop the .h and prefix a 'c' char.

#include <stdio.h> becomes #include <cstdio>
#include <stdlib.h> becomes #include <cstdlib>
#include <string.h> becomes #include <cstring>
#include <ctype.h> becomes #include <cctype>

Also for stanard headers don't use the double quotes. Rather use the < >'s
Was This Post Helpful? 0
  • +
  • -

#5 DownUnder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-September 10

Re: Problem linking a generic smart pointer template to a main method.

Posted 10 August 2011 - 06:54 PM

Hey thanks for your help, with a combination of your answers I have solved the problem and gained some knowledge about c++

Thanks so much :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1