English 中文(简体)
C++ 模版,使用中继器
原标题:C++ templates,errors where used std iterators

在我试图汇编模板类别时,我发现以下错误:

C.cpp: In member function  void PeriodContainerAdvanced<T>::add() :
C.cpp:133: error: type/value mismatch at argument 1 in template parameter list for  template<class _T1, class _T2> struct std::pair 
C.cpp:133: error:   expected a type, got  std::map<int,T,std::less<int>,std::allocator<std::pair<const int, T> > >::iterator 
C.cpp:133: error: invalid type in declaration before  ;  token

C.h档案中的班级:(简化)

template <class T>
class PeriodContainerAdvanced 
{
[skip]
    void add (); 
[skip]
}

C.cpp(简化):

template <class T>
void PeriodContainerAdvanced<T>::add()
{
[skip]
    std::pair<std::map< time_t, T >::iterator, bool> ret; // line 133 !
[skip]
}

And the similar error at other function when trying to define

std::map< time_t, T >::iterator it, it_start, it_end; // line 153

之后,编辑们说:

C.cpp:153: error: expected `;  before  it 
C.cpp:166: error:  it_start  was not declared in this scope

How to fix it? thanks

最佳回答

这名受扶养者的姓名需要宣布为:

std::pair<typename std::map< time_t, T >::iterator, bool> ret;

此外,为了避免日后的链接错误,你应将模板执行移至所有使用该模板的翻译单位的档案中,就像你界定你的模板类别时的负责人一样。

问题回答

首先,需要将<代码>add(>>的实施工作列入标题档案或列入标题的档案。 汇编者需要看到该守则,以便立即公布一个<代码>的模板。 T。

Second, the source of the error you quote, you need to add a typename to tell the compiler you are talking about a type. std::map< time_t, T >::iterator could be interpreted as a value.

template <class T>
class PeriodContainerAdvanced {
  void add () {
    std::pair<typename std::map< time_t, T >::iterator, bool> ret;
    ....         ^
  }
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签