Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Behavior of the local class

63 views
Skip to first unread message

somenath

unread,
Jan 2, 2014, 4:22:47 AM1/2/14
to
I am not able to understand why the following program does not compile.
#include<iostream>
using namespace std;

template <class T >
void Fun(T t)
{
static int i = t;

class Test {
public:
Test( int t1) {
cout<<"Value = "<<i<<endl;
}
};
Test tt(t);

}


int main(void)
{
Fun<int >(5);
return 0;

}
Whenever I try to compile the program I get the following error
g++ LocalClass.cpp
/tmp/ccfEKvyT.o:LocalClass.cpp:(.text+0x2b): undefined reference to `i'
collect2: ld returned 1 exit status


But I am aware that local class can access the static local variable of enclosing function. The following program proves that as well.

#include<iostream>
using namespace std;

void Fun(int t)
{
static int i=t;

class Test {
public:
Test( int t1) {
cout<<"Value = "<<i<<endl;
}
};
Test tt(t);

}

int main(void)
{
Fun(5);
return 0;

}
This code compile fine. Then what is going wrong with template version of the program?

Victor Bazarov

unread,
Jan 2, 2014, 8:48:16 AM1/2/14
to
On 1/2/2014 4:22 AM, somenath wrote:
> I am not able to understand why the following program does not compile.
> #include<iostream>
> using namespace std;
>
> template <class T >
> void Fun(T t)
> {
> static int i = t;
>
> class Test {
> public:
> Test( int t1) {
> cout<<"Value = "<<i<<endl;
> }
> };
> Test tt(t);
>
> }
>
>
> int main(void)
> {
> Fun<int >(5);
> return 0;
>
> }
> Whenever I try to compile the program I get the following error
> g++ LocalClass.cpp
> /tmp/ccfEKvyT.o:LocalClass.cpp:(.text+0x2b): undefined reference to `i'
> collect2: ld returned 1 exit status

You must be using a buggy version of the compiler/linker. It compiles
and links (and runs) just fine with VC++ 2012.

> [..]

V
--
I do not respond to top-posted replies, please don't ask

Alf P. Steinbach

unread,
Jan 2, 2014, 10:15:57 AM1/2/14
to
On 02.01.2014 10:22, somenath wrote:
> I am not able to understand why the following program does not compile.
> #include<iostream>
> using namespace std;
>
> template <class T >
> void Fun(T t)
> {
> static int i = t;
>
> class Test {
> public:
> Test( int t1) {
> cout<<"Value = "<<i<<endl;
> }
> };
> Test tt(t);
>
> }
>
>
> int main(void)
> {
> Fun<int >(5);
> return 0;
>
> }
> Whenever I try to compile the program I get the following error
> g++ LocalClass.cpp
> /tmp/ccfEKvyT.o:LocalClass.cpp:(.text+0x2b): undefined reference to `i'
> collect2: ld returned 1 exit status

This is known g++ bug, already reported at

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

As a workaround you can pass the referenced static variable as an
argument, or maybe move it inside the class, or ...


Cheers & hth.,

- Alf

K. Frank

unread,
Jan 4, 2014, 2:18:39 PM1/4/14
to
Hi somenath (and Alf and Victor)!

On Thursday, January 2, 2014 10:15:57 AM UTC-5, Alf P. Steinbach wrote:
> On 02.01.2014 10:22, somenath wrote:
> > I am not able to understand why the following program does not compile.
> > ...
> > Whenever I try to compile the program I get the following error
> > g++ LocalClass.cpp
> > /tmp/ccfEKvyT.o:LocalClass.cpp:(.text+0x2b): undefined reference to `i'
> > collect2: ld returned 1 exit status
>
> This is known g++ bug, already reported at
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688
> As a workaround you can pass the referenced static variable as an
> argument, or maybe move it inside the class, or ...
>
> Cheers & hth.,
> - Alf

I tried this with my copy of g++.

For what it's worth, your code compiles and runs fine
when I compile it with a mingw-w64 version of g++:

C:\>g++ --version
g++ (rubenvb-4.8-stdthread) 4.8.1 20130324 (prerelease)


Happy New Year Hacking!


K. Frank
0 new messages