using my template class

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

  1. template <class T, const int> class Data {
    private:
    T data;
    int id;
    public:
    Data();
    Data(T, const int);
    virtual ~Data();
    };
    template <class T, const int x>
    Data<T, x>::Data() {}

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

    template <class T, const int x>
    Data<T, x>::Data(const Data& other) {
    //copy ctor
    }

    int main(){
    jme::Data <std::string, const int> d("data", 1);
    std::cout << "Hello world!" << std::endl;
    return 0;
    }


    The class above compiles, but when trying to use it in the main() function, I get this error:

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

    Compiling: main.cpp
    C:\...\main.cpp: In function 'int main()':
    C:\...\main.cpp:8: error: type/value mismatch at argument 2 in template parameter list for 'template<class T, int <anonymous> > class jme::Data'
    C:\...\main.cpp:8: error: expected a constant of type 'int', got 'const int'
    C:\...\main.cpp:8: error: invalid type in declaration before '(' token
    C:\...\main.cpp:8: error: initializer expression list treated as compound expression
    C:\...\main.cpp:8: warning: left-hand operand of comma has no effect
    C:\...\main.cpp:8: warning: unused variable 'd'
    Process terminated with status 1 (0 minutes, 0 seconds)
    4 errors, 2 warnings

    I am bit confused as to how to use a class like that.
     
    Rene Ivon Shamberger, Sep 29, 2012
    #1
    1. Advertising

  2. Rene Ivon Shamberger

    Ian Collins Guest

    On 09/29/12 01:22 PM, Rene Ivon Shamberger wrote:
    > template<class T, const int> class Data {


    Why are you using "const int" here?

    > private:
    > T data;
    > int id;
    > public:
    > Data();
    > Data(T, const int);
    > virtual ~Data();
    > };
    > template<class T, const int x>
    > Data<T, x>::Data() {}
    >
    > template<class T, const int x>
    > Data<T, x>::Data(T _data, const int _id) {
    > this->data = _data;
    > this->id = _id;
    > }
    > template<class T, const int x>
    > Data<T, x>::~Data() {}
    >
    > template<class T, const int x>
    > Data<T, x>::Data(const Data& other) {
    > //copy ctor
    > }


    There isn't a declaration for this copy constructor in the class.

    > int main(){
    > jme::Data<std::string, const int> d("data", 1);


    Two problems I can see:

    Where does the namespace jme come from?

    The second template argument must be an integer value, not a type.
    Something like

    Data<std::string, 1> d("data", 1);

    > std::cout<< "Hello world!"<< std::endl;
    > return 0;
    > }
    >
    >
    > The class above compiles, but when trying to use it in the main() function, I get this error:


    It won't be compiled until you instantiate it!

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

  3. This is a snip of the original program, but this is the entire program:
    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;
    }
     
    Rene Ivon Shamberger, Sep 29, 2012
    #3
  4. On 9/29/2012 12:16 AM, Rene Ivon Shamberger wrote:
    > This is a snip of the original program, but this is the entire program:
    > 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() {}


    Members 'data' and 'id' have been left uninitialized here.

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


    See FAQ section 10 for recommendations on how to implement constructors.

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


    If the destructor doesn't do anything, it's probably better to avoid
    declaring and defining it at all.

    >
    > 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;


    When you try to instantiate the template, you must give it some real
    arguments, like

    jme::Data<int> d;

    > std::cout << "Hello world!" << std::endl;
    > return 0;
    > }
    >
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 29, 2012
    #4
  5. Victor Bazarov <> wrote:
    > On 9/29/2012 12:16 AM, Rene Ivon Shamberger wrote:
    >> template <class T> class Data {
    >> private:
    >> T data;
    >> int id;
    >> public:
    >> Data();
    >> Data(T, const int);
    >> virtual ~Data();

    [...]
    >> }
    >> template <class T>
    >> jme::Data<T>::~Data() {}

    >
    > If the destructor doesn't do anything, it's probably better to avoid
    > declaring and defining it at all.


    Not if it is virtual. If the class contains no other virtual methods this
    is not immediately necessary but it's certainly not wrong, especially if
    you plan to add virtual methods some time.
    If you forget to make the destructor virtual, the derived destructor may
    never be called and such bugs are hard to find.
    And yes, I know that there are compiler warnings for that...

    Tobi
     
    Tobias Müller, Sep 29, 2012
    #5
  6. On 9/29/2012 8:49 AM, Tobias Müller wrote:
    > Victor Bazarov <> wrote:
    >> On 9/29/2012 12:16 AM, Rene Ivon Shamberger wrote:
    >>> template <class T> class Data {
    >>> private:
    >>> T data;
    >>> int id;
    >>> public:
    >>> Data();
    >>> Data(T, const int);
    >>> virtual ~Data();

    > [...]
    >>> }
    >>> template <class T>
    >>> jme::Data<T>::~Data() {}

    >>
    >> If the destructor doesn't do anything, it's probably better to avoid
    >> declaring and defining it at all.

    >
    > Not if it is virtual.


    Of course. I missed that part.

    > If the class contains no other virtual methods this
    > is not immediately necessary but it's certainly not wrong, especially if
    > you plan to add virtual methods some time.
    > If you forget to make the destructor virtual, the derived destructor may
    > never be called and such bugs are hard to find.
    > And yes, I know that there are compiler warnings for that...
    >
    > Tobi
    >



    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 29, 2012
    #6
  7. On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias Müller wrote:
    The information I have presented here is intended to show a problem in the code, the code is, however, fully opperational. Please, try the code and ifyou are able to dupplicate the error, then you will be able to reply to myquestions.
    Thanks in advance.
     
    Rene Ivon Shamberger, Sep 29, 2012
    #7
  8. Rene Ivon Shamberger

    Ike Naar Guest

    On 2012-09-29, Rene Ivon Shamberger <> wrote:
    > On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias M?ller wrote:
    > The information I have presented here is intended to show a problem in
    > the code, the code is, however, fully opperational. Please, try the
    > code and if you are able to dupplicate the error, then you will be
    > able to reply to my questions.


    The code you've shown so far cannot be fully operational, because it
    doesn't even compile. If I try the code, the compiler complains
    as follows:
    a.cpp: In function 'int jme::main()':
    a.cpp:49: error: missing template arguments before 'd'
    a.cpp:49: error: expected `;' before 'd'
    a.cpp:50: error: 'cout' is not a member of 'std'
    a.cpp:50: error: 'endl' is not a member of 'std'
    a.cpp: At global scope:
    a.cpp:52: error: expected `}' at end of input
     
    Ike Naar, Sep 29, 2012
    #8
  9. Rene Ivon Shamberger <> wrote:
    > On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias Müller wrote:
    > The information I have presented here is intended to show a problem in
    > the code, the code is, however, fully opperational. Please, try the code
    > and if you are able to dupplicate the error, then you will be able to
    > reply to my questions.
    > Thanks in advance.


    I am pretty sure that I did _not_ write that.
    What did you want to cite and how does your "answer" relate to my post?

    Tobi
     
    Tobias Müller, Sep 29, 2012
    #9
  10. Ian Collins <> wrote:
    > On 09/29/12 01:22 PM, Rene Ivon Shamberger wrote:
    >> template<class T, const int> class Data {

    >
    > Why are you using "const int" here?


    The 'const' is indeed rather redundant given that only compile-time
    constants are allowed as template parameters (which is even a stricter
    requirement than a generic 'const' would be).
     
    Juha Nieminen, Oct 1, 2012
    #10
  11. Rene Ivon Shamberger

    Stuart Guest

    On 9/29/12 Tobias Müller wrote:
    [snip]
    >> On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias Müller wrote:
    >> The information I have presented here is intended to show a problem in
    >> the code, the code is, however, fully opperational. Please, try the code
    >> and if you are able to dupplicate the error, then you will be able to
    >> reply to my questions.
    >> Thanks in advance.

    >
    > I am pretty sure that I did _not_ write that.

    [snip]

    Is that so? Prove it.

    Next time you should add a private key encrypted MD5 sum to your post ;-)

    Um, forgot to do so myself ...

    Regards,
    Stuart
     
    Stuart, Oct 1, 2012
    #11
  12. Stuart <> wrote:
    > On 9/29/12 Tobias Müller wrote:

    [...]
    >> I am pretty sure that I did _not_ write that.

    > [snip]
    >
    > Is that so? Prove it.
    >
    > Next time you should add a private key encrypted MD5 sum to your post ;-)


    That would not help much in proving that I didn't write something. It could
    be a quote from a different post.

    > Um, forgot to do so myself ...


    My newsreader doesn't even support it.

    Tobi
     
    Tobias Müller, Oct 2, 2012
    #12
    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.

Share This Page