How to fix the error: "declaration of template parameter 'T' shadows template parameter" in C ++?

0

I'm trying to make a graphics program, but I'm having a problem ... My code is the one:

main.cpp:

#include "point_2d.h"
#include <iostream>

int main()
{
    cge::point_2d p(5, 5);
    p.x = 6;
    std::cout << p.x << " " << p.y << std::endl;
    std::cin.get();
    return 0;
}

point_2d.h:

#ifndef POINT_2D_H
#define POINT_2D_H

namespace cge
{

    template<typename T>
    struct basic_point_2d
    {

        typedef T value_type;

        constexpr basic_point_2d() noexcept = default;

        constexpr basic_point_2d(const basic_point_2d&) noexcept = default;

        constexpr basic_point_2d& operator=(const basic_point_2d&) noexcept = default;

        template<typename U>
        constexpr explicit basic_point_2d(const basic_point_2d<U>&) noexcept; //Conversions

        constexpr basic_point_2d(T, T) noexcept; //Constructors

        constexpr bool equal(const basic_point_2d&) const noexcept;

        template<typename T>
        friend constexpr bool operator==(const basic_point_2d<T>&, const basic_point_2d<T>&) noexcept; 

        template<typename T>
        friend constexpr bool operator!=(const basic_point_2d<T>&, const basic_point_2d<T>&) noexcept;


        T x = T();
        T y = T();

    }; //struct basic_point_2d

    typedef basic_point_2d<int> point_2d;
    typedef basic_point_2d<unsigned> upoint_2d;
} //namespace cge 

#endif //POINT_2D_H

point_2d.cpp:

#include "point_2d.h"

template<typename T>
constexpr cge::basic_point_2d<T>::basic_point_2d(T _X, T _Y) noexcept:
    x(_X), y(_Y)
{
}

template<typename T>
template<typename U>
constexpr cge::basic_point_2d<T>::basic_point_2d(const basic_point_2d<U>& _Right) noexcept:
    x(static_cast<U>(_Right.x)), y(static_cast<U>(_Right.y))
{
}

template<typename T>
constexpr bool cge::basic_point_2d<T>::equal(const basic_point_2d<T>& _Right) const noexcept
{
    return(this->x == _Right.x && this->y == _Right.y);
}

template<typename T>
constexpr bool operator==(const cge::basic_point_2d<T>& _Left, 
    const cge::basic_point_2d<T>& _Right) noexcept
{
    return(_Left.equal(_Right));
}

template<typename T>
constexpr bool operator!=(const cge::basic_point_2d<T>& _Left, 
    const cge::basic_point_2d<T>& _Right) noexcept
{
    return(!(_Left == _Right));
}

My makefile looks like this:

test: main.o point_2d.o
    g++ main.o point_2d.o -o test

main.o: main.cpp
    g++ -c main.cpp -std=c++1z 

point_2d.o: point_2d.cpp point_2d.h
    g++ -c point_2d.cpp -std=c++1z

And when I compile the program I have these surprises:

In file included from point_2d.cpp:1:
point_2d.h:26:18: error: declaration of template parameter 'T' shadows template
parameter
         template<typename T>
                  ^~~~~~~~
point_2d.h:7:14: note: template parameter 'T' declared here
     template<typename T>
              ^~~~~~~~
point_2d.h:29:18: error: declaration of template parameter 'T' shadows template
parameter
         template<typename T>
                  ^~~~~~~~
point_2d.h:7:14: note: template parameter 'T' declared here
     template<typename T>
              ^~~~~~~~
mingw32-make: *** [makefile:8: point_2d.o] Error 1

I searched a lot on the internet, but could not find any solution that was satisfactory to the problem.

    
asked by anonymous 11.10.2018 / 14:16

1 answer

1

The error says that the template parameter of its members has the same name as the class, thus preventing its use within the member.

Change the parameter name to get rid of the error:

template<typename U, typename V>
friend constexpr bool operator==(const basic_point_2d<U>&, const basic_point_2d<V>&) noexcept;

Or remove the template entirely in the members, which makes the most sense:

friend constexpr bool operator==(const basic_point_2d&, const basic_point_2d&) noexcept;
    
11.10.2018 / 14:29