2011-05-10 56 views
1

我有以下代码: file.h:C++/Ubuntu - 在模板错误

#ifndef __ANA_H__ 
#define __ANA_H__ 

template <class T> 
class ana { 

class ana1{//code 
      }*anna1; 

    public: 
    bool method(T& data,ana::ana1 &a); 
}; 

#endif 

file.cpp:

#include <ana.h> 
#include <iostream> 
using namespace std; 

template <class T> 
bool ana<T>::method(T& data,ana::ana1 &t) { 
    cout << "Data = " << data << endl; 
    if(data > 0) { 
    return true; 
    } 
    return false; 
} 

我有错误:ana::ana1 is not a type。如何解决这个错误?我在哪里错了?需要一些help.thx 我在ubuntu工作,我编译使用g ++的代码。我从我发布的.h和.cpp创建了一个.a。

+1

的名字,像'__ANA_H__',其中包含下划线,或以下划线开始在用户编写的代码是不允许的。将其更改为'ANA_H' – 2011-05-10 09:57:29

+0

@unapersson:包含下划线很好;包含*两个连续的*下划线是问题(以及从一个当然开始)。 – ildjarn 2011-05-10 10:03:57

+0

@ildjam确实 - 这就是我的意思,但显然没有打字。 – 2011-05-10 10:07:13

回答

2

作出这样的

// (declaration): 
bool method(T& data, ana1& t); 

// (definition): 
bool ana<T>::method(T& data, typename ana<T>::ana1& t) { 
+0

ana1是ana的一个子类。我可以;只是有ana1。我需要ana :: ana1,不是吗? – linuxx 2011-05-10 09:58:47

+0

可以请你重写正确的代码吗?我需要一些帮助:) – linuxx 2011-05-10 09:59:36

+0

@linuxx:它不是一个子类,它是一个嵌套类。 – 2011-05-10 09:59:53

2

有了模板,你应该把declaration and definition into the same file。这可能是这样的,你的情况:

#ifndef __ANA_H__ 
#define __ANA_H__ 

template <class T> 
class ana { 

class ana1{//code 
      }*anna1; 

    public: 
    bool method(T& data,ana1 &a) { 
     cout << "Data = " << data << endl; 
     if(data > 0) { 
      return true; 
     } 
     return false; 
    } 
}; 

#endif 
+0

正如他之前被告知的那样。 – 2011-05-10 10:40:03