How to fix the error: "declaration of template Parameter’T' Shadows template Parameter" in C++?

Asked

Viewed 174 times

0

I’m trying to do a Polygraph program, but I’m having a problem... My code and this:

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 and stuff:

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 satisfactory solution to the problem.

1 answer

1


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

Rename the parameter 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 completely remove the template in the members, which makes more sense:

friend constexpr bool operator==(const basic_point_2d&, const basic_point_2d&) noexcept;
  • When I removed the members template, it compiled, but I got 4 warnings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.