473,563 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linker complains about symbol it can't possibly use

I have the following code. One one compiler it compiles OK,
on the other it gives the two warnings shown below, and then
the linker fails because Foo<int>::str is undefined.

Which is correct?

Second question: the compiler won't accept:
static const std::string str = std::string();

Is there any way to achieve that effect with alternative code?

#include <iostream>

template<typena me T>
struct Foo
{
static const bool i = false;
static const std::string str;
};

template<typena me T>
void foo()
{
std::cout << Foo<T>::str << std::endl;
}

template<typena me T>
void bar()
{
if (Foo<T>::i) // "Condition is always false."
foo<T>(); // "Unreachabl e code."
}

int main()
{
bar<int>();
}

Jul 23 '05 #1
4 1328
Old Wolf wrote:
I have the following code. One one compiler it compiles OK,
on the other it gives the two warnings shown below, and then
the linker fails because Foo<int>::str is undefined.

Which is correct?

Second question: the compiler won't accept:
static const std::string str = std::string();

Is there any way to achieve that effect with alternative code?

#include <iostream>

template<typena me T>
struct Foo
{
static const bool i = false;
static const std::string str;
};

template<typena me T>
void foo()
{
std::cout << Foo<T>::str << std::endl;
}

template<typena me T>
void bar()
{
if (Foo<T>::i) // "Condition is always false."
foo<T>(); // "Unreachabl e code."
}

int main()
{
bar<int>();
}


Well, 'i' is always 'false', and can never be anyting else.
You made 'i' 'static const' and initialized it to 'false',
so 'i' can never be changed (it will always be false).

Since 'Foo<T>::i' and 'Foo<T>::str' are both 'static const',
there will be only one 'i' and one 'str' regardless of how
many Foo<T> objects are created. I believe that the 'const'
qualifier means they can never be changed once they are created.

Someone with more knowledge about 'static' and 'const' members
in templates may be able to provide more info.

Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #2
Larry I Smith wrote:
Old Wolf wrote:
I have the following code. One one compiler it compiles OK,
on the other it gives the two warnings shown below, and then
the linker fails because Foo<int>::str is undefined.

Which is correct?
if (Foo<T>::i) // "Condition is always false."
foo<T>(); // "Unreachabl e code."


Well, 'i' is always 'false', and can never be anyting else.
You made 'i' 'static const' and initialized it to 'false',
so 'i' can never be changed (it will always be false).


I meant, the warnings are OK but is the link error correct or not?
The purpose of the warnings was to show that Foo<int>::str should
never be used.

Also, my code needs a #include <string> .

Jul 23 '05 #3
Old Wolf wrote:
Larry I Smith wrote:
Old Wolf wrote:
I have the following code. One one compiler it compiles OK,
on the other it gives the two warnings shown below, and then
the linker fails because Foo<int>::str is undefined.

Which is correct?
if (Foo<T>::i) // "Condition is always false."
foo<T>(); // "Unreachabl e code."

Well, 'i' is always 'false', and can never be anyting else.
You made 'i' 'static const' and initialized it to 'false',
so 'i' can never be changed (it will always be false).


I meant, the warnings are OK but is the link error correct or not?
The purpose of the warnings was to show that Foo<int>::str should
never be used.

Also, my code needs a #include <string> .


Try the following code. It creates the missing 'str'
and demos some other stuff...

#include <iostream>
#include <string>

template<typena me T>
struct Foo
{
// a static of basic type (int, char, etc) can
// be init'd in the template (or not)
static const bool i = false;
static const bool i2;

// static non-basic types must be init'd external
// to the template
static const std::string str;
};

template<typena me T>
void foo()
{
std::cout << "Foo<T>::st r = "
<< Foo<T>::str
<< ", Foo<T>::i = "
<< Foo<T>::i
<< ", Foo<T>::i2 = "
<< Foo<T>::i2
<< std::endl;
}

template<typena me T>
void bar()
{
if (Foo<T>::i) // "Condition is always false."
foo<T>(); // "Unreachabl e code."
}

// set the values for Foo<int>::str and Foo<int>::i2
const std::string Foo<int>::str(" hello");
const bool Foo<int>::i2 = true;

int main()
{
bar<int>();
foo<int>();
}

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #4
Static constant fundamental types (ints, chars, floats, booleans) are
OK to initialize the way you did. The reason is that the compiler can
substitue the values inline. I believe if you attempted to take the
address of "i", you'd get a similar warning message that it was not
defined either. Class types with constructors and destructors need to
be explicitly declared outside a templete. The reason is that the code
for the object can't be inlined and the compiler needs a place to
initialize the class' data. You did not provide this with "str", thus
the error. I believe non-fundamental PODs would also fail since
compilers have a difficult time inlining them, but I'm not 100% sure
here.

Anyway, as far as why one compiler compiles fine and the other
doesn't.... well, the compiler that produces the error is well within
it rights from the ISO standard to compile the code anyway -- even if
it is unreachable. That's why it produces the linker error. The
compiler that doesn't compile the unreachable code performed an
optimization and that's fine too -- it just means that the linker isn't
looking for the code symbol. So each compiler is within its right.
Personally, I'd like to receive the linker error. It's telling me
something about my code that may be important down the road -- namely
that I need to define the instances. Then in all likelyhood, I would
change where str was defined so I wouldn't have to keep defining new
instances for each possible template arguement.

Anyway, I hope this helps!

- Kevin

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8645
by: Georg | last post by:
Hello, I must be doing something wrong, but I don't get it: - compile gcc -c -O -Iinc src/hello.c -o obj/hello.o gcc -c -O -Iinc src/msg_1.c -o obj/msg_1.o gcc -c -O -Iinc src/msg_2.c -o obj/msg_2.o - make library
1
1319
by: Stefan Slapeta | last post by:
Hi, I've found some strange behaviour with some smart pointers. Consider this little sample: // ********************************************************** struct MyStruct {}; template <typename T> struct Wrapper
4
1811
by: Joel Whitehouse | last post by:
Hello All, I have a Visual C++ 2003 dll solution in a directory called "Antenna Test Range Control", and when I comile it, I get the error LNK1104: cannot open file "antenna.obj". When I remove teh spaces in my solution directory, I get the following errors: MastController.exp : error LNK2001: unresolved external symbol _Calibrate@8...
3
3276
by: ralphsieminsky | last post by:
A project compiles fine under VS 2005 RC without the /clr option. However, when /clr is turned on several errors appear: - A symbol exported from a DLL is not found by another DLL referencing it. The name of the symbol present in the DLL, as shown by depends.exe is ?Apply@ScreenContext@@SGPAV1@PAUHWND__@@@Z But the name of the symbol...
3
8048
by: Chucker | last post by:
Hi Folks, I got a Wrapper Dll around a native C++ static library. In .NET 1.1 this worked fine. When moving to .NET 2.0 I get a couple of unresolved externals / linker errors: Error 16 error LNK2028: unresolved token (0A000007) "extern "C" void __clrcall ___CxxCallUnwindDtor(void (__clrcall*)(void *),void *)"...
1
3276
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly, but throws a load of linker errors
1
10344
by: iiitsunny | last post by:
i have ported one project which was working in VC6 to VC7. Now the project is working fine in VC7 also. I have made some additions in VC7 environment only. But the problem is that the project is not working in the release mode. I mean it is shooting off linking errors. I am required to generate an exe file and am looking for some suggestions!!!...
3
1932
by: Vijay Bajwa | last post by:
I declared a static std::map<string, intin one.cpp at the module level. This is only used inside the file and I did it to avoid global namespace pollution The file one.o goes into the library archive mylib.a Then when I try to compile file two.cpp, which links to mylib.a, the linker spits out incomprehensible messages about undefined...
5
3296
by: VanKha | last post by:
Hi everyone , I've just read a book on Computer Systems.(Computer Systems:A programmer's perspective ) . On the part of "Linking" , the author talks about strong/weak symbol and as an example for the rule "Given a strong symbol and weak symbols , choose the strong one" , he gives the following C modules /* foo5.c */ #include<stdio.h> void...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.