Hamzsé
Hamzsé

Reputation: 29

Is it possible to use a class template in its member functions?

My class is size parameterized. In one of its methods I have to create a temporary array, but I don't know how to pass the class's size template to the member function. This is how I tried:

#include <array>

template<unsigned int N>
class MyClass{
    std::array<int,N> m_data;
  public:    
    void myFunc(){
        std::array<int,N> tempArray;
    }
};


int main(){
    MyClass<5> obj;
    obj.myFunc();
}

Edit: Build log:

C:\Windows\system32\cmd.exe /C ""C:/Program Files/mingw-w64/x86_64-6.3.0-win32-seh-rt_v5-rev1/mingw64/bin/mingw32-make.exe" -j6 SHELL=cmd.exe -e -f  Makefile"
"----------Building project:[ hatizsak_konyv - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv'
"C:/Program Files/mingw-w64/x86_64-6.3.0-win32-seh-rt_v5-rev1/mingw64/bin/g++.exe"  -c  "E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp" -g -O0 -Wall  -o ./Debug/main.cpp.o -I. -I.
E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp: In instantiation of 'void MyClass<N>::myFunc() [with unsigned int N = 5u]':
E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp:15:16:   required from here
E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp:8:27: warning: unused variable 'tempArray' [-Wunused-variable]
         std::array<int,N> tempArray;
                           ^~~~~~~~~
"C:/Program Files/mingw-w64/x86_64-6.3.0-win32-seh-rt_v5-rev1/mingw64/bin/g++.exe" -o ./Debug/hatizsak_konyv @"hatizsak_konyv.txt" -L.
mingw32-make.exe[1]: Leaving directory 'E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv'
====1 errors, 1 warnings====

Upvotes: 1

Views: 75

Answers (1)

Matteo Italia
Matteo Italia

Reputation: 126787

Template parameters are visible inside methods of the template class; the code is correct.

There's no error at all, nor in the provided code example, nor in the build log. The message in the build log is just a warning (with the lines before providing context for it), which correctly warns you about the fact that that variable isn't used, as per the -Wall option provided on the command line. Other than that, the code compiles fine, both on ideone and on my machine (where it gives you exact same warning, not error).

[matteo@teolapkubuntu /tmp]$ g++ -Wall -Wextra -std=c++11 stuff.cpp 
stuff.cpp: In instantiation of ‘void MyClass<N>::myFunc() [with unsigned int N = 5u]’:
stuff.cpp:15:16:   required from here
stuff.cpp:8:27: warning: unused variable ‘tempArray’ [-Wunused-variable]
         std::array<int,N> tempArray;
                           ^~~~~~~~~

The "1 error" message at the end of the build log is just CodeLite misinterpreting the compiler output; there is an open bug about it, with conditions similar to yours.

Upvotes: 3

Related Questions