How to create an Object from a Generic class in C++?

Go To StackoverFlow.com

0

I wrote a class which take two components, an random class type T and an integer, I implement it like following: In Test.h like:

    template <class A, int B>class Test { // two components, 
    private: 
        A first;
        int second;

    public:
        Test();
        Test (A,int);
    }

In Test.cpp I did:

    template <class T,int i> Test<T,i>::Test() {}
    template <class A,int i>Test<A,i>::Test(T a, int b):first(a) {second=b;}

But in Main function:

    Test<int, int >  T1; //It can not be passed
    Test<int, 4> T2; //It can not be passed 
    int x = 8;
    Test<int, x> T3 (3,4);// can not be passed

How I can declare an object instance from the above generic class?

2012-04-04 23:58
by ToBeGeek
Sigh. http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15 - Oliver Charlesworth 2012-04-05 00:02
The proper term is "class template". The word "generic" is a bit dangerous in this context, because it has a very different meaning in other languages - Kerrek SB 2012-04-05 00:02
How i can declare an object from the Test class ? This is the question. Dude, I need it.... Pleas - ToBeGeek 2012-04-05 00:19


0

You forgot the semicolon at the end of the class template definition.

2012-04-05 00:01
by Kerrek SB


0

template <class T,int i> Test<T,i>::Test() {}
template <class A,int i>Test<A,i>::Test(T a, int b):first(a) {second=b;}

You need to put these two template function definitions in the header rather than the .cpp - the actual code needs to be made available to all compilation units that call these functions, not just the declaration.

Test<int, int >  T1; //It can not be passed

This is invalid, the second int is a type, but the template expects an int value

Test<int, 4> T2; //It can not be passed 

There's nothing wrong with this

int x = 8;
Test<int, x> T3 (3,4);// can not be passed

You need to make the first of these lines static const x = 8 (i.e. make x a compile-time constant) to make it a usable as a template parameter

And there's also the missing semicolon at the end of the class definition.

2012-04-05 00:03
by je4d
Test T2; It is wrong. It can not be compiled.. - ToBeGeek 2012-04-05 00:34
@user1314029 with what error? it works for me after fixing the other error - je4d 2012-04-05 00:44
Undefined symbols for architecture x8664: "Test::Test()", referenced from: _main in main.o ld: symbol(s) not found for architecture x8664 clang: error: linker command failed with exit code 1 (use -v to see invocation - ToBeGeek 2012-04-05 00:46
@user1314029 ah, i missed the fact that you had your constructors in a .cpp file. Updated my answer - je4d 2012-04-05 00:49
To be pedantic.. Test<int, 4> T2; could be compiled, but it cased an error when linking - je4d 2012-04-05 00:54
Ok....How I can fix it... Please... - ToBeGeek 2012-04-05 00:56
@user1314029 see my updated answer - move the constructor code to the header fil - je4d 2012-04-05 00:56
I did, It gives me a new error, "No matching Constructor for intialization Test"... God.. - ToBeGeek 2012-04-05 01:10
@user1314029 Best update the question with current code... that'd only happen if you've somehow removed the no-arg constructo - je4d 2012-04-05 01:15
Ads