Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static std::vector<std::string> member and a static function

Reply
Thread Tools

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

 
 
A L
Guest
Posts: n/a
 
      06-15-2010
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?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-15-2010
* 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

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      06-15-2010
On Jun 15, 12:08*pm, A L <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
A L
Guest
Posts: n/a
 
      06-15-2010
On Jun 15, 2:21*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
A L
Guest
Posts: n/a
 
      06-15-2010
On Jun 15, 2:39*pm, Saeed Amrollahi <(E-Mail Removed)> wrote:
> 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?
 
Reply With Quote
 
A L
Guest
Posts: n/a
 
      06-15-2010
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!


On Jun 15, 2:42*pm, A L <(E-Mail Removed)> wrote:
> On Jun 15, 2:39*pm, Saeed Amrollahi <(E-Mail Removed)> wrote:
>
> > 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?


 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      06-15-2010
Paul Bibbings <(E-Mail Removed)> writes:

> A L wrote:
>> 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!

>
> 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
 
Reply With Quote
 
A L
Guest
Posts: n/a
 
      06-15-2010
Hi Paul,
On Jun 15, 4:52*pm, Paul Bibbings <(E-Mail Removed)> wrote:
> Paul Bibbings <(E-Mail Removed)> writes:
> > Not a bug. *Notice that the code given above defines a
>> free function ...


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

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

>


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.
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      06-15-2010
A L <(E-Mail Removed)> writes:

> Hi Paul,
> On Jun 15, 4:52*pm, Paul Bibbings <(E-Mail Removed)> wrote:
>> Paul Bibbings <(E-Mail Removed)> writes:
>> > Not a bug. *Notice that the code given above defines a
>>> free function ...

>
> Sorry, it was a typing mistake. The function indeed *is* a member
> function as you mentioned in your reply.
>
>> > * *std::vector<std::string>& INeedMyClassNameHere::myFunction(void)
>> > * *{
>> > * * * // ...
>> > * *}

>>

>
> 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
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      06-15-2010
Paul Bibbings <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
How do you call a regular member function from a static member function? aling C++ 6 10-30-2005 04:38 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments