1

I have a (simple) question about generic classes in c++. I have some knowledge of using them in C# like list but now I tried to implement one in c++ and i have a error and I don't know what i should do for the program to work. This is not a homework or something i need to but its research for myself.

Code:

#include <iostream>
using namespace std;

template<class A>class genericClass
{
    A ceva;
    char* clasa;
public: 
    void afisClasa(void);
    genericClass(A);
    ~genericClass(void);
};

template<class A>genericClass<A>::afisClasa()
{
    cout << clasa;
}

template<class A>genericClass<A>::genericClass(A myType)
{
    myType = ceva;

    if((int)ceva == ceva)
    {
        clasa = "INT";
        goto label;
    }       

    if((float)ceva == ceva)
    {
        clasa = "FLOAT";
        goto label;
    }   

    if((double)ceva == ceva)
    {
        clasa = "DOUBLE";
        goto label;
    }   

    label:
    //cout << clasa;
}

template<class A>genericClass<A>::~genericClass(void)
{
}


int main()
{
    int n;

    genericClass<float> A(6.2);

    cin >> n;
}

This program is supposed to take a generic number and to say what type it is, but when i implement the afisClass method i get an error:

C4430: Missing type specifier - int assumed. Note c++ does not support default-in
Dementor
  • 241
  • 1
  • 6
  • 12
  • 3
    Also FYI, in C++ those things are called "class templates", rather than "generic classes". There's lots of terminology surrounding templates that's worth getting used to if you want to be serious at C++. – Kerrek SB Jan 16 '12 at 18:54
  • 1
    As an aside: They're called *templates* in C++, not *generics.* – Maxpm Jan 16 '12 at 18:55
  • 1
    And it's not just that they have a different name, templates are a fundamentally different thing, even if they can be used to implement something superficially similar to what C# generics are used for. For example the way you're checking for the type using `if((int)ceva==ceva)` is not at all the way it should be done in templates. – bames53 Jan 16 '12 at 19:10
  • @bames53: Thanks! I was just trying to find a few references for that. Like [this](http://stackoverflow.com/questions/1208153/c-sharp-generics-compared-to-c-templates), [this](http://stackoverflow.com/questions/31693/what-are-the-differences-between-generics-in-c-sharp-and-java-and-templates-i), and [this](http://blogs.msdn.com/b/ericlippert/archive/2009/07/30/generics-are-not-templates.aspx), and maybe [this](http://www.andymcm.com/dotnetfaq.htm#14.3). – Kerrek SB Jan 16 '12 at 19:21

2 Answers2

6

You have to say:

template <class A> void genericClass<A>::afisClasa() { cout << clasa; }
//                 ^^^^

The function definition has to have the exact same form as the previous declaration.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

This is a case where you'd likely want to do template specialization. C++ allows you to customize the behavior of a template based off it's instantiated type. For example

template <typename T> 
void PrintMyType(T& arg) {
  cout << "Unknown type" << endl;
}

template <>
void PrintMyType<int>(int& arg) {
  cout << "int" << endl;
}

template <>
void PrintMyType<float>(float& arg) {
  cout << "float" << endl;
}

PrintMyType(42);  // prints "int"
PrintMyType("hello"); // prints "Unknown type"
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454