C ++模板错误.请求帮忙!! [英] C++ templates error. Ask for help!!

查看:109
本文介绍了C ++模板错误.请求帮忙!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些有关模板实例化的编译错误,但找不到语法错误发生的位置.可以帮我吗?


I get some compiling errors about template instantiation , but I can''t find where the syntax errors happens. Can some help me?


template<typename Type> class SingleList;

template<typename Type> class SingleListNode{
private:
    friend class SingleList<Type>;

    SingleListNode() : next(NULL){}

public:
    friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln);                  //Error!!!

private:
    SingleListNode *next;
};

template<typename Type> ostream& operator<<(ostream& os,SingleListNode<Type>& out){
    os<<out.data;
    return os;
}

template<typename Type> class SingleList{
public:
    SingleList()
        {
            head =  new SingleListNode<Type> () ;                 //Error!!!
        }
    ~SingleList(){
        delete head;
    }

private:
    SingleListNode<Type> *head;
};



编译器错误消息:



Compiler error message:

|| g++ -g -I ./inc/ -c single_list_test.cpp  -o single_list_test.o
|| single_list.h: In instantiation of ‘SingleListNode<int>’:
single_list.h|25 col 13| instantiated from ‘SingleList<Type>::SingleList() [with Type = int]’
single_list_test.cpp|9 col 18| instantiated from here
single_list.h|10 col 18| error: template-id ‘operator<< <int>’ for ‘std::ostream& operator<<(std::ostream&, SingleListNode<int>&)’ does not match any template declaration
|| make:
*** [single_list_test.o] Error 1





推荐答案

似乎可以在Visual Studio中使用,因此我不能确定是什么问题.

但是,我发现涉及模板的朋友声明很难正确地声明. (过去在VS的较旧版本中几乎是不可能的)有时,我通过声明一个帮助程序类来解决这些问题:该帮助程序类将具有单个公共构造函数,该构造函数的参数列表与所讨论的函数相同,并且将其声明为原始模板化类的朋友.在您的情况下,这是某事.像这样:
Seems to work in Visual Studio, so I can''t say for sure what''s the problem.

However, I''ve found that friend declarations involving templates can be quite tricky to declare correctly. (used to be near impossible in older versions of VS) On occasion I fixed these problems by declaring a helper class instead: That helper class would have a single public constructor with the same parameter list as the function in question, and it would be declared friend to the original templated class. In your case it would be sth. like this:
template <typename Type> class SingleList;
template <typename Type> class SingleListNode;
template <typename Type> class SingleListNodeStreamHelper {
   ostream& os;
   SingleListNode<Type>& node;
public:
   SingleListNodeStreamHelper(ostream& s, SingleListNode& n)
    : os(s), node(n) {}
   ostream& operator()(); {
      // do some streaming ...
      return os;
   }
};

// SingleList declared here ...

template <typename Type> class SingleListNode {
private:
   friend SingleListNodeStreamHelper<Type>; // new!

   // rest of class declaration ...
};
// stream function now does not need to be friend!
ostream& operator<< <Type>(ostream& s, SingleListNode<Type> n) {
    SingleListNodeStreamHelper<Type> helper(s, n);
    return helper();
}



我还没有花时间来测试这一点,但我还是希望您能理解它的原理.



I haven''t taken the time to test this, but I hope you understand the principle anyway.


与模板问题有关的零件事...

抓住最新的编译器.

首先处理任何模板实例化问题...

摆脱模板.现在我知道这听起来很奇怪,但是如果您替换硬代码< t>编译器可能会在任何地方给您一个您正在做的任何事情的想法,听起来都有些麻烦.并确保您在最高警告级别上进行编译-这可能意味着您从模板中获得了更多莫名其妙的消息,但值得一试.

第二件事是将所有好友转发文档. FREX为什么列表节点需要知道它包含的内容,反之亦然?列表追加到当前告诉节点的末尾,那里有一个新节点,挂在该节点上.它可能不会告诉您实例化在哪里出错,但是它可能会使代码再次移动.

第三件事是尝试像斯蒂芬的建议. C ++ 98和''03充满了地方模板无法正常工作,正如您期望的那样,并且编译器具有不同程度的标准合规性.如果您觉得自己特别勇敢,可以在编译器的标准库中查看它的肮脏技巧,以使它正常工作.

干杯,




干杯,

Ash
The zeroth thing to do with template problems...

Grab the latest compiler you can.

The first thing to do with any template instantiation problems...

Get rid of the template. Now I know this sounds weird but if you replace hard-code <t> everywhere the compiler might give you an idea of anything you''re doing that sounds a bit fishy to it. And make sure you compile on the highest warning level possible - which might mean you get even more inexplicable messages from the template but it''s worth a go.

The second thing is to bin any friend and forward declarations. FREX why does a list node need to know about the thing it''s contained in and vice-versa? Lists append to the telling the node currently at the end that there''s a new node, hang onto it. It might not tell you where the instantiation''s going wrong but it might get the code moving again.

The third thing is to try something like Stefan''s suggested. C++98 and ''03 are full of places templates don''t work as you''d expect them AND compilers have varying degrees of standard compliance. If you''re feeling particularly brave have a look in you compiler''s standard library to see the dirty tricks it has to pull off to get something working.

Cheers,

Ash


Cheers,

Ash


尝试添加以下两行(延迟声明):

Try to add the following two lines (deferred declarations):

template<typename Type> class SingleListNode;
template<typename Type> ostream& operator<<(ostream& os,SingleListNode<Type>& out);

template<typename Type> class SingleListNode{


这篇关于C ++模板错误.请求帮忙!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆