static std::vector<std::string> member and a static function

A

A L

Hi,

I am getting this linker error:

error LNK2019: unresolved external symbol "public: static class
std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > > &
__cdecl ......

I have a static std::vector<std::string> member in the private section
of my class. I am initializing/defining this static member in my cpp
file like this:

std::vector<std::string> MyClass::myStaticMember_ ;

then I have a function that returns a reference to the above vector:

std::vector<std::string> &myFunction()
{
return myStaticMember_;
}

I am using this function in my main code like this:

std::vector<std::string> &myStrings = MyClass::myFunction();

I am using Visual Studio 2008 Team System.

Any help?
 
A

Alf P. Steinbach

* A L, on 15.06.2010 11:08:
Hi,

I am getting this linker error:

error LNK2019: unresolved external symbol "public: static class
std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > > &
__cdecl ......

I have a static std::vector<std::string> member in the private section
of my class. I am initializing/defining this static member in my cpp
file like this:

std::vector<std::string> MyClass::myStaticMember_ ;

then I have a function that returns a reference to the above vector:

std::vector<std::string> &myFunction()
{
return myStaticMember_;
}

I am using this function in my main code like this:

std::vector<std::string> &myStrings = MyClass::myFunction();

I am using Visual Studio 2008 Team System.

Any help?

Most probably you forgot to add your cpp file to your Visual Studio project.

By the way are you sure that you want client code to be able to modify the vector?

Might be an idea to add a little 'const'.


Cheers & hth.,

- Alf
 
S

Saeed Amrollahi

Hi,

I am getting this linker error:

error LNK2019: unresolved external symbol "public: static class
std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > > &
__cdecl ......

I have a static std::vector<std::string> member in the private section
of my class. I am initializing/defining this static member in my cpp
file like this:

std::vector<std::string> MyClass::myStaticMember_ ;

then I have a function that returns a reference to the above vector:

std::vector<std::string> &myFunction()
{
   return myStaticMember_;

}

I am using this function in my main code like this:

std::vector<std::string> &myStrings = MyClass::myFunction();

I am using Visual Studio 2008 Team System.

Any help?

Hi

I ran your program and it's OK. I can't reproduce the Link error.
BTW, such (Linkage) errors occurred, when there is a declaration
for an entity like object, data member, member function and so on,
but there is no definition, so Linker issue error.

Good luck
-- Saeed Amrollahi
 
A

A L

Most probably you forgot to add your cpp file to your
Visual Studio project.

Might be an idea to add a little 'const'.

I have a simple cpp file in which I have declared a simple class also
- right at the top. I was using const before but to locate the error I
have removed all "const"s from my project and trying to nail it down -
don't understand why it is happening.
 
A

A L

I ran your program and it's OK. I can't reproduce the
Link error.
BTW, such (Linkage) errors occurred, when there is a
declaration
for an entity like object, data member, member function
and so on,
but there is no definition, so Linker issue error.

Saeed, as you can see in my original post, I am initializing/defining
the static member like this in the cpp file after the class
definition:

std::vector<std::string> MyClass::myStaticMember_ ;

Is there anything wrong with this?
 
A

A L

Saeed, I resolved this by returning a reference to a static vector of
strings which is defined inside a member function. Though it is
bizarre but VC++ gives me link errors when I define declare the static
member function inside the class and _define_ it outside the class
like this:
std::vector<std::string> &myFunction(void)
{
static std::vector<std::string> myVector;
//code;
return myVector;
}

However, it does compile fine if I define this member function
**inline*** INSIDE the class! Funny! but another BLATANT BUG in an M$
product!
 
P

Paul Bibbings

Paul Bibbings said:
Not a bug. Notice that the code given above defines a free function
and *not* the definition of your class member function, which would
be:

std::vector<std::string>& INeedMyClassNameHere::myFunction(void)
{
// ...
}

Also, while were at it, the use `static' in the definition of your
(non-)member function above does not reflect the same semantics as
would its use in declaring a static data member. Hence my question
about whether you were sure that the unresolved external related to what
you /thought/ it did in an earlier post.

I guess it's back to school, before you start throwing accusations at
your tools and the people that provide them for your dissatisfaction.

Regards

Paul Bibbings
 
A

A L

Hi Paul,

Sorry, it was a typing mistake. The function indeed *is* a member
function as you mentioned in your reply.

So, my point is really a genuine one - I am not throwing accusations.
I will download Service Pack1 today and try it again tomorrow - may
be, that is the reason for this behavior.
 
P

Paul Bibbings

A L said:
Hi Paul,


Sorry, it was a typing mistake. The function indeed *is* a member
function as you mentioned in your reply.


So, my point is really a genuine one - I am not throwing accusations.
I will download Service Pack1 today and try it again tomorrow - may
be, that is the reason for this behavior.

Are you wanting to say that something like the following doesn't work
for you with your version of VC 2008? The following certainly works for
me with VC++2008 Expression edition, without any service packs.

d:\CPPProjects\CLCPP>cat static_member.cpp
// file: static_member.cpp

#include <vector>
#include <string>

class MyClass {
public:
std::vector<std::string>& myFunction();
private:
static std::vector<std::string> myStaticMember_;
};

std::vector<std::string> MyClass::myStaticMember_;

std::vector<std::string>& MyClass::myFunction()
{
return myStaticMember_;
}

int main()
{
MyClass myClass;
std::vector<std::string>& vec_ref = myClass.myFunction();
}

d:\CPPProjects\CLCPP>cl /EHsc static_member.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

static_member.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:static_member.exe
static_member.obj

d:\CPPProjects\CLCPP>

Regards

Paul Bibbings
 
P

Paul Bibbings

Paul Bibbings said:
Are you wanting to say that something like the following doesn't work
for you with your version of VC 2008? The following certainly works for
me with VC++2008 Expression edition, without any service packs.

Lol. "Expression edition." Love it. That's when you /know/ you've had
your head buried in the FCD for too long!

PB
 
A

A L

Are you wanting to say that something like the following
doesn't work for you with your version of VC 2008?
   d:\CPPProjects\CLCPP>cat static_member.cpp
   // file: static_member.cpp

   #include <vector>
   #include <string>

   class MyClass {
   public:
      std::vector<std::string>& myFunction();
   private:
      static std::vector<std::string> myStaticMember_;
   };

   std::vector<std::string> MyClass::myStaticMember_;

   std::vector<std::string>& MyClass::myFunction()
   {
      return myStaticMember_;
   }

   int main()
   {
      MyClass myClass;
      std::vector<std::string>& vec_ref = myClass.myFunction();
   }


EXACTLY, Paul. It does not work even if I put the static vector of
strings in a static function and return a reference to it (instead of
putting it as a private static data member, that is). Do you have any
idea as to what this is all about? I am really drained on this - have
tried everything I could to solve it but there seems to be no
solution. I am surprised that your code works even if you put the
static vector of strings in the private section of your class and
access it through a static function! I know that is valid but
previously this was not working. It works only if I put the static
vector definition inside a static member function and return a
reference to it from the member function.
 
P

Paul Bibbings

A L said:
EXACTLY, Paul. It does not work even if I put the static vector of
strings in a static function and return a reference to it (instead of
putting it as a private static data member, that is). Do you have any
idea as to what this is all about? I am really drained on this - have
tried everything I could to solve it but there seems to be no
solution. I am surprised that your code works even if you put the
static vector of strings in the private section of your class and
access it through a static function! I know that is valid but
previously this was not working. It works only if I put the static
vector definition inside a static member function and return a
reference to it from the member function.

If you can say that you are able to *copy & paste* the above code into a
new file in a new project and then attempt to build it and it fails,
then I would suggest that you have a problem with your installation
(rather than a bug). Do you have the VC command prompt? If so, copy
and paste the above code into notepad, save it as test_static.cpp, and
then invoke:

cl /EHsc test_static.cpp

from the VC command prompt. Report back on what the error output is, if
any.

Also, and this is important to know, your "It works only if I put the
static vector definition inside a static member function..." is a
red-herring. Inside a function definition, static does not have the
same meaning. It defines a vector of string that is *local to* the
function and, even if it has the same name as that of your declared
class member, it does *not* define that member.

Regards

Paul Bibbings
 
A

A L

Hi Paul,

Also, and this is important to know, your "It works only if I put the
static vector definition inside a static member function..." is a
red-herring.


Paul, installing the Service Pack 1 has solved this problem. The code
works both ways now. By the way, Visual Studio Express edition
includes SP1 by default - you can check it out in its about box.
Thanks for your interest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top