0

The code basically checks the given array is symmetrical or not. When i send parameter std::array i get this error :

ccCPLdU9.o:main.cpp:(.text+0x5da): undefined reference to bool isItSymmetrical<5u>(std::array<int, >5u> const&)' AppData\Local\Temp\ccCPLdU9.o:main.cpp:(.text+0x60b): undefined reference to >bool isItSymmetrical<2u>(std::array const&)'

std::array implementation should be work with unknown size, i couldn't figure out why it turned out. the error

Other implementations works well by the way.

My question5.hpp file

#ifndef question5_hpp
#define question5_hpp

#include <iostream>
#include <array>
#include <vector>

bool isItSymmetrical(int const arr[], int size);
template<std::size_t SIZE>
bool isItSymmetrical(const std::array<int, SIZE> &arr);
bool isItSymmetrical(const std::vector<int> v);


#endif /* question5_hpp */

Related question5.cpp file

template<std::size_t SIZE>
bool isItSymmetrical(const std::array<int, SIZE> &arr)
{
    for (int i = 0; i < (int)(arr.size()/2); i++)
    {
        if (arr[i] != arr[arr.size()-i-1])
        {
            return false;
        }
    }
    return true;
}

And main.cpp

std::array<int,5> ar1  {2, 4, 6, 8, 10};
std::array<int,5> ar2  {2,4,5,4,2};
std::array<int,2> ar3  {1,2};
std::cout << isItSymmetrical(ar1) << "\n";
std::cout << isItSymmetrical(ar2) << "\n";
std::cout << isItSymmetrical(ar3) << "\n";
abra
  • 134
  • 9
  • What is the command you're compiling your code with? – Shan S May 04 '20 at 14:30
  • g++ main.cpp GeoBox/*.cpp Questions/*.cpp This function only in Questions folder unique function in my project – abra May 04 '20 at 14:33
  • It doesn't really matter. You have answer at the top of your question - templates must be defined in header files. – Yksisarvinen May 04 '20 at 14:40
  • Yes i defined template in my hpp file. The reason i wrote template in my cpp too i get an error: error: request for member 'size' in 'arr', which is of non-class type 'const int' disappears only when i add template in cpp – abra May 04 '20 at 14:51

0 Answers0