【问题标题】:Compilation errors related to template instantiation与模板实例化相关的编译错误
【发布时间】:2019-08-12 09:14:50
【问题描述】:

以下测试程序在更大程序的上下文中重现编译错误:

#include <algorithm>
#include <iostream>
#include <vector>
using std::for_each;
using std::vector;
using std::cout;
using std::endl;

template<typename T, typename C = vector<T>>
class display_container{
    public:
        display_container(const C& cr):this->cr(cr){this->();}
        ~display_container(){}
    private:
        constexpr void operator () (void){if(cr.empty()){cout << "NULL" << " ";} else{for_each(cr.begin(), cr.end(), [](const T& crt){cout << crt << " ";});}}
        const C& cr;
};

int main (void){

    int n = 5;
    vector<int> vec(n, 0);
    display_container d(vec);
    cout << endl;

    return 0;
}

以下是编译器错误的日志:

g++ -ggdb -std=c++17 -Wall -Werror=pedantic -Wextra  -c code.cpp
code.cpp: In constructor ‘display_container<T, C>::display_container(const C&)’:
code.cpp:12:40: error: expected identifier before ‘this’
         display_container(const C& cr):this->cr(cr){this->();}
                                        ^~~~
code.cpp:12:40: error: expected ‘{’ before ‘this’
code.cpp: In function ‘int main()’:
code.cpp:23:28: error: class template argument deduction failed:
     display_container d(vec);
                            ^
code.cpp:23:28: error: no matching function for call to ‘display_container(std::vector<int>&)’
code.cpp:12:9: note: candidate: template<class T, class C> display_container(const C&)-> display_container<T, C>
         display_container(const C& cr):this->cr(cr){this->();}
         ^~~~~~~~~~~~~~~~~
code.cpp:12:9: note:   template argument deduction/substitution failed:
code.cpp:23:28: note:   couldn't deduce template parameter ‘T’
     display_container d(vec);
                            ^
code.cpp:10:7: note: candidate: template<class T, class C> display_container(display_container<T, C>)-> display_container<T, C>
 class display_container{
       ^~~~~~~~~~~~~~~~~
code.cpp:10:7: note:   template argument deduction/substitution failed:
code.cpp:23:28: note:   ‘std::vector<int>’ is not derived from ‘display_container<T, C>’
     display_container d(vec);
                            ^
make: *** [makefile:20: code.o] Error 1

我认为剩余的错误是从与display_container 模板类的内联构造函数定义相关的第一个错误开始的。

对与内联构造函数定义相关的代码有什么问题有什么建议吗?

TIA

【问题讨论】:

    标签: c++ templates compiler-errors


    【解决方案1】:

    编译器还不能获取向量的模板类型:

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    using std::for_each;
    using std::vector;
    using std::cout;
    using std::endl;
    
    template<typename T, typename C = vector<T>>
    class display_container{
        public:
            display_container(const C& cr): cr(cr) { (*this)(); }
            ~display_container(){}
        private:
            constexpr void operator () (void){if(cr.empty()){cout << "NULL" << " ";} else{for_each(cr.begin(), cr.end(), [](const T& crt){cout << crt << " ";});}}
            const C& cr;
    };
    
    int main (void){
    
        int n = 5;
        vector<int> vec(n, 0);
        display_container<int> d(vec);
        cout << endl;
    
        return 0;
    }
    

    【讨论】:

      【解决方案2】:
      1. 您不能(也不需要)通过this 限定member initializer list 中的数据成员,它们应该是标识符。正确的语法应该是

        display_container(const C& cr):cr(cr){(*this)();}
        
      2. 您应该取消对this 的引用,然后在其上调用operator()(如上图所示),或者您可以像this-&gt;operator()(); 一样明确调用operator()(看起来很难看)。

      3. 您应该为display_container 指定模板参数。

        display_container<int> d(vec);
        

      LIVE

      【讨论】:

        猜你喜欢
        • 2016-04-15
        • 1970-01-01
        • 2013-09-25
        • 1970-01-01
        • 2021-08-21
        • 2016-07-23
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        相关资源
        最近更新 更多