problem with templates

This is a discussion on problem with templates within the C++ Programming forums, part of the General Programming Boards category; i have code: Code: //main.cpp #include "src/shm_obj.h" struct __DATA{ int index; char *value[200]; }; typedef __DATA data; int main(int argc,char ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    problem with templates

    i have code:
    Code:
    //main.cpp
    #include "src/shm_obj.h"
    
    struct __DATA{
    	int index;
    	char *value[200];
    };
    
    typedef __DATA data;
    
    int main(int argc,char **argv){
    	data *d=new data;
    	d->index=123;
    	strcpy((char*)d->value,"qwerty");
    	shm_obj<data>::create(d); //!!! undefined reference to 'shm_obj<__DATA>::create(__DATA*)'
    	return EXIT_SUCCESS;
    }
    Code:
    //shm_obj.h
    #ifndef SHM_OBJ_H_
    #define SHM_OBJ_H_
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    template<typename T>
    class shm_obj{
    private:
    	T *obj;
    	static shm_obj<T> *__instance;
    private:
    	shm_obj(T *__obj);
    public:
    	static shm_obj<T> create(T*);
    	static T *get();
    	virtual ~shm_obj();
    };
    
    #endif /*SHM_OBJ_H_*/
    Code:
    //shm_obj.cpp
    #include "shm_obj.h"
    
    template<typename T>
    shm_obj<T>::shm_obj(T *__obj){
    	this->obj=__obj;
    }
    
    template<typename T>
    shm_obj<T> shm_obj<T>::create(T *__obj){
    	shm_obj<T>::__instance=new shm_obj<T>;
    	return shm_obj<T>::__instance;
    }
    
    template<typename T>
    T *shm_obj<T>::get(){
    	return __instance->obj;
    }
    
    template<typename T>
    shm_obj<T>::~shm_obj(){
    }
    where is error??
    help me please...
    Last edited by valery; 07-07-2009 at 06:27 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,533
    Generally, to use templates, the compiler needs to see the definition of the template functions. In other words, the compiler, when compiling your main.cpp, needs to see the content of shm_obj.cpp. This is one of the cases where you might want to actually #include a .cpp file, rather than compiling it separately.

    There are exceptions to this (depending on your compiler) but I'll bet your compiler isn't one of the exceptions.

    There is also a problem that names beginning with an underscore are reserved for use by the implementation (i.e. the compiler and library) in some way. In other words, you shouldn't name anything as __DATA. [Specifically, if you're not afraid of the details, names with two leading underscores or one underscore and an uppercase letter are reserved to the implementation. Other names beginning with an underscore are reserved for the implementation as a name in the global namespace.]
    Last edited by grumpy; 07-07-2009 at 06:50 AM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    thanks to grumpy
    i consistent that what

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Valery, my compiler tells me you are calling a default constructor with
    shm_obj<T>::__instance=new shm_obj<T>;
    but you have no such constructor.
    Did you mean to write:
    shm_obj<T>::__instance=new shm_obj<T>(__obj);

    Also, the create method should return shm_obj<T>*

    I hope that helps.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Code:
    strcpy((char*)d->value,"qwerty");
    Nothing to do with templates, but that's wrong. value is an array of pointers to char. You must not copy a string there. Yes, you've put a cast there presumably to get rid of the warning the compiler is screaming at you, but that's like ripping the label off a can of rat poison and sticking on a new label that says "delicious anchovies"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I'd advise you use std::string instead of char* and you had better have a really good reason to cast something in C++. I do not consider this is one of those things.
    Besides that, you never delete what you new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21