1

I have the following code: file.h:

#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;
}

I have error: ana::ana1 is not a type. How to resolve this error? where am i wrong? need some help.thx i am working in ubuntu and i compile the code using g++. i create an .a from the .h and .cpp i've posted.

linuxx
  • 1,487
  • 4
  • 16
  • 17
  • 1
    Names like `__ANA_H__`, which contain underscores, or start with an underscore are not allowed in user-written code. Change it to `ANA_H` –  May 10 '11 at 09:57
  • @unapersson : Containing underscores is fine; containing *two consecutive* underscores is the problem (as well as starting with one of course). – ildjarn May 10 '11 at 10:03
  • @ildjam Indeed - that's what I meant, but obviously didn't type. –  May 10 '11 at 10:07

2 Answers2

2

Make that

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

and

// (definition):
bool ana<T>::method(T& data, typename ana<T>::ana1& t) {
ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • ana1 is a subclass from ana. I can;t just have ana1. i need ana::ana1,no? – linuxx May 10 '11 at 09:58
  • @linuxx: It's not a subclass, it is a nested class. – Björn Pollex May 10 '11 at 09:59
  • @linuxx : Just replace the relevant lines *you* pasted with the lines *I* pasted. Why should I need to re-paste the entirety of your code? – ildjarn May 10 '11 at 10:00
  • you are right.thx! i keep have the ana::ana1 is not a type:(. please help! – linuxx May 10 '11 at 10:01
  • @linuxx : If your code is using `ana::ana1` anywhere, then you didn't make the changes I suggested. – ildjarn May 10 '11 at 10:02
  • i've replaced ana::ana1& t with ana1& t but i still have the same error: ana::ana1&t is not a type. – linuxx May 10 '11 at 10:02
  • i just want the definition of the method in the .h file! – linuxx May 10 '11 at 10:03
  • 2
    @linuxx : If you can't copy and paste the lines I posted over the ones you have, then I simply don't know how to help you. – ildjarn May 10 '11 at 10:04
  • My code is much more complicated. It contains a lot of nested classes. I did replace my code with what you suggested but i still have the same error. do you know why? It's true i do use ana::ana1 in other methods too. In the method i've posted i just need a GENERIC OBJECT! As i've seen this is the only solution! PLEASE HELP! I need to call a generic object – linuxx May 10 '11 at 10:06
  • I need to have only the definition of the method in the .h file. The code that the method containes is in the .cpp code. – linuxx May 10 '11 at 10:07
2

With templates you should put declaration and definition into the same file. This could look like this in your case:

#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
Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283