If NOT_WORKING is defined, the following program gives me linker errors:
/usr/bin/ld: Undefined symbols:
Definer<int, 1>::d
Definer<int, 2>::d
Definer<int, 3>::d
Definer<int, 4>::d
collect2: ld returned 1 exit status

But if NOT_WORKING is not defined it compiles and runs. I can't figure out: 1) Why those things are not getting instanciated. 2) Why the second method accesses things that the first method does not. 3) How to get the LIST thingy in this forum to work:lol:

#define NOT_WORKING

#include <iostream>

template <typename C, int cnt>
struct Definer
{
    static const C d = cnt - 1;
    Definer<C, cnt-1> f;

    C const& lookup(int index)
    {
	if (index == (cnt - 1))
	    return d;
	return f.lookup(index);
    }
};

template <typename C>
struct Definer <C, 0>
{
    static const C d = 0;

    C const& lookup(int index)
    {
	throw 18;
    }
};

int main()
{
    Definer<int, 4> g;

    int i = g.f.f.f.d;
    std::cout << i << std::endl;

    int j = g.f.f.d;
    std::cout << j << std::endl;

    int k = g.f.d;
    std::cout << k << std::endl;

    int l = g.d;
    std::cout << l << std::endl;

#ifdef NOT_WORKING

    i = g.lookup(0);
    std::cout << i << std::endl;

    j = g.lookup(1);
    std::cout << j << std::endl;

    k = g.lookup(2);
    std::cout << k << std::endl;

    l = g.lookup(3);
    std::cout << l << std::endl;

#endif

    return 0;
}

Are you sure? I can compile your code even when the #define NOT_WORKING is commented out or not. However I get an unreferenced g variable when it is commented out.

template <typename C, int cnt>
struct Definer
{
    static const C d = cnt - 1;
    Definer<C, cnt-1> f;

    C const& lookup(int index)
    {
    if (index == (cnt - 1))
        return d;
    return f.lookup(index);
    }
};

template <typename C>
struct Definer <C, 0>
{
    static const C d = 0;

    C const& lookup(int index)
    {
    throw 18;
    }
};

i

Dont you think the "typename" should be replaced with "class" ?

Are you sure? I can compile your code even when the #define NOT_WORKING is commented out or not. However I get an unreferenced g variable when it is commented out.

You may have misunderstood me. You seem to have it backwards.

Without doing anything to the program, it will not link.

If you comment out the #define NOT_WORKING, then the program links and runs.

I'm using g++ 4.0.1 on a Mac as well as xlC version 8 on AIX to test with. Both agree in both cases.

Thanks for helping.

Dont you think the "typename" should be replaced with "class" ?

Why is that?

Why is that?

Ah.. my bad, sorry I was thinking of somthing else.:confused:

You may have misunderstood me. You seem to have it backwards.

Without doing anything to the program, it will not link.

If you comment out the #define NOT_WORKING, then the program links and runs.

I'm using g++ 4.0.1 on a Mac as well as xlC version 8 on AIX to test with. Both agree in both cases.

Thanks for helping.

No. I am sure I have not misunderstood. It links and runs allright in both the cases. Check if you have posted the correct code.
These are the outputs.


#define commented out

0
1
2
3
Press any key to continue

#define not commented out

0
1
2
3
0
1
2
3
Press any key to continue

Yea, its the right code. I copied it back to a temp file and did a diff and it passed.

I'm using G++ 4.0.1 -- maybe its a compiler thing.

I got an answer via email. I need to add this:

template <typename C, int cnt>
const C Definer<C,cnt>::d;

Thanks again for helping out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.