Template Class

Discussion in 'C++' started by Rene Ivon Shamberger, Sep 29, 2012.

  1. namespace jme {
    template <class T> class Data {
    private:
    T data;
    int id;
    public:
    Data();
    Data(T, const int);
    virtual ~Data();
    Data(const Data& other);
    Data& operator=(const Data& other);
    const int getId();
    const T& getData() {
    return data;
    }
    };

    template <class T>
    jme::Data<T>::Data() {}

    template <class T>
    jme::Data<T>::Data(T _data, const int _id) {
    this->data = _data;
    this->id = _id;
    }
    template <class T>
    jme::Data<T>::~Data() {}

    template <class T>
    jme::Data<T>::Data(const Data& other) {
    //copy ctor
    }
    template <class T>
    jme::Data<T>&
    jme::Data<T>::eek:perator=(const Data& rhs) {
    //if (this == &rhs) return *this; // handle self assignment
    //assignment operator
    this->T = rhs.T;
    //this->id = rhs.id;
    return *this;
    }
    template <class T>
    const int jme::Data<T>::getId() {
    return id;
    }
    ---
    int main(){
    jme::Data d;
    std::cout << "Hello world!" << std::endl;
    return 0;
    }

    The above program gives me this error message:

    -------------- Build: Debug in data ---------------

    Linking console executable: bin\Debug\list.exe
    obj\Debug\main.o: In function `main':
    C:/.../main.cpp:8: undefined reference to `jme::Data<std::string>::Data()'

    C:/.../main.cpp:10: undefined reference to `jme::Data<std::string>::~Data()'

    C:/.../main.cpp:10: undefined reference to `jme::Data<std::string>::~Data()'

    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    3 errors, 0 warnings



    What am I doing wrong?
     
    Rene Ivon Shamberger, Sep 29, 2012
    #1
    1. Advertising

  2. Rene Ivon Shamberger

    Ian Collins Guest

    On 09/29/12 16:20, Rene Ivon Shamberger wrote:

    <snip>

    What are you trying to do? You keep posting incomplete snippets with
    errors that don't match the code.

    > ---
    > int main(){
    > jme::Data d;


    jme::Data is a template, you have to provide an argument to instantiate it.

    > std::cout<< "Hello world!"<< std::endl;
    > return 0;
    > }
    >
    > The above program gives me this error message:
    >
    > -------------- Build: Debug in data ---------------
    >
    > Linking console executable: bin\Debug\list.exe
    > obj\Debug\main.o: In function `main':
    > C:/.../main.cpp:8: undefined reference to `jme::Data<std::string>::Data()'


    I very much doubt what you posted cased this error.

    > What am I doing wrong?


    Not posting the code to match your errors.

    --
    Ian Collins
     
    Ian Collins, Sep 29, 2012
    #2
    1. Advertising

  3. Rene Ivon Shamberger

    Rui Maciel Guest

    Rene Ivon Shamberger wrote:

    > The above program gives me this error message:


    No, it doesn't. But it still has a hand full of errors, such as:

    - you called std::cout and friends, but you haven't include iostream
    - the declaration of namespace jme is missing a closing tag
    - you tried to declare an object based on class jme::Data without specifying
    the type of typename T


    Rui Maciel
     
    Rui Maciel, Sep 29, 2012
    #3
  4. This code help you?
    I just might be that I tired, but I cannot see anything wrong in this code.
    template <class T> class Data{
    private:
    T data;
    public:
    Data();
    Data(T);
    virtual ~Data();
    };
    template <class T>
    jme::Data<T>::Data() {}

    template <class T>
    jme::Data<T>::Data(T _data) {
    this->data = _data;
    }
    template <class T>
    jme::Data<T>::~Data() {}
    ..............
    using namespace std;
    int main() {
    jme::Data<int> d(1); // this does not work

    jme::Data<int> d; // this does not work
    cout << "Hello world!" << endl;
    return 0;
    }
     
    Rene Ivon Shamberger, Sep 29, 2012
    #4
  5. On 9/29/2012 10:14 AM, Rene Ivon Shamberger wrote:
    > This code help you?
    > I just might be that I tired, but I cannot see anything wrong in this code.


    I think you need to take a break from that code.

    Only a couple of things are wrong. (A) 'jme' is undefined. You've been
    told it already, but you don't seem to pay enough attention, which
    suggests that you need to have a fresh start.

    Fix: Remove all instances of "jme::" from this program.

    (B) 'cout' is undefined.

    Fix: Add #include <iostream> to the top of the code.

    (C) There are two variables named 'd' in the 'main' function.

    Fix: Rename one of them.

    After that your program will compile. Will that help? I am not sure.
    Again, you definitely *need a break*. Use this opportunity, go out, get
    a rest. Get drunk, if that helps. Get laid. Just get your mind off
    that program - it's no use keeping at it if your brain has stopped working.

    > template <class T> class Data{
    > private:
    > T data;
    > public:
    > Data();
    > Data(T);
    > virtual ~Data();
    > };
    > template <class T>
    > jme::Data<T>::Data() {}
    >
    > template <class T>
    > jme::Data<T>::Data(T _data) {
    > this->data = _data;
    > }
    > template <class T>
    > jme::Data<T>::~Data() {}
    > .............
    > using namespace std;
    > int main() {
    > jme::Data<int> d(1); // this does not work
    >
    > jme::Data<int> d; // this does not work
    > cout << "Hello world!" << endl;
    > return 0;
    > }
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 29, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Graham
    Replies:
    0
    Views:
    577
    Matt Graham
    Jul 21, 2003
  2. christopher diggins
    Replies:
    16
    Views:
    784
    Pete Becker
    May 4, 2005
  3. Replies:
    1
    Views:
    2,147
    Gianni Mariani
    Jun 8, 2007
  4. nguillot
    Replies:
    5
    Views:
    559
  5. A L
    Replies:
    1
    Views:
    530
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page