Thread: Help me

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    5

    Help me

    Code:
    Error: Reference not defined: classe1<int>::printsizeof()
    file1.cpp
    Code:
    #include <iostream>
    #include "classe.h"
        
    int main()
    {
        classe1 <int>c1;
        c1.printsizeof();
    }
    classe.h

    Code:
    template<class Type>
    class classe1{
        public:
            void printsizeof();    
    };
    classe.cpp
    Code:
    #include "classe.h"
    #include <iostream>
    
    
    void classe1<Type>::printsizeof(){
        cout << sizeof(Type) << endl;
    }
    I don't know what I do.
    Last edited by Julimar Melo; 08-25-2018 at 03:50 PM.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Your code looks incomplete. If possible, always post your code as written on your computer.

    Usually, you'd want to place the implementation of your class template inside the header file, i.e.

    classe.h
    Code:
    #include <iostream>
    
    template<typename Type> // I recommend 'typename' over 'class'
    class Classe1 // I recommend uppercase names for user defined classes
    {
    public:
        void printsizeof {
            std::cout << sizeof(Type) << std::endl;
        } 
    };
    
    // Or if you want to define it afterwards:
    template<typename Type>
    void Classe1<Type>::printsizeof()
    {
        std::cout << sizeof(Type) << std::endl;
    }
    With a header file like that, you then wouldn't have a classe1.cpp file at all.

    If you insist on defining your template inside a cpp file, you'll have to declare the instantiation within that file (translation unit):
    Code:
    #include <iostream>
    #include "classe.h"
    
    template<typename Type>
    void Classe1<Type>::printsizeof()
    {
        std::cout << sizeof(Type) << std::endl;
    }
    
    template class Classe1<int>; // You'd have to do this for any type...
    // ...you instantiate your class template with. e.g.
    // template class Classe1<double>;
    C++ is weird like that. I hope this helped.
    Last edited by -Adrian; 08-25-2018 at 04:36 PM.

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    5
    Quote Originally Posted by -Adrian View Post
    Your code looks incomplete. If possible, always post your code as written on your computer.

    Usually, you'd want to place the implementation of your class template inside the header file, i.e.

    classe.h
    Code:
    #include <iostream>
    
    template<typename Type> // I recommend 'typename' over 'class'
    class Classe1 // I recommend uppercase names for user defined classes
    {
    public:
        void printsizeof {
            std::cout << sizeof(Type) << std::endl;
        } 
    };
    
    // Or if you want to define it afterwards:
    template<typename Type>
    void Classe1<Type>::printsizeof()
    {
        std::cout << sizeof(Type) << std::endl;
    }
    With a header file like that, you then wouldn't have a classe1.cpp file at all.

    If you insist on defining your template inside a cpp file, you'll have to declare the instantiation within that file (translation unit):
    Code:
    #include <iostream>
    #include "classe.h"
    
    template<typename Type>
    void Classe1<Type>::printsizeof()
    {
        std::cout << sizeof(Type) << std::endl;
    }
    
    template class Classe1<int>; // You'd have to do this for any type...
    // ...you instantiate your class template with. e.g.
    // template class Classe1<double>;
    C++ is weird like that. I hope this helped.


    Sorry for my english errors.
    Thanks a lot for help me.
    Last edited by Julimar Melo; 08-25-2018 at 04:51 PM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread