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

Discussion in 'C++' started by A L, Jun 15, 2010.

  1. A L

    A L Guest

    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 L, Jun 15, 2010
    #1
    1. Advertising

  2. * 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>
     
    Alf P. Steinbach, Jun 15, 2010
    #2
    1. Advertising

  3. On Jun 15, 12:08 pm, A L <> 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
     
    Saeed Amrollahi, Jun 15, 2010
    #3
  4. A L

    A L Guest

    On Jun 15, 2:21 pm, "Alf P. Steinbach" <> 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.
     
    A L, Jun 15, 2010
    #4
  5. A L

    A L Guest

    On Jun 15, 2:39 pm, Saeed Amrollahi <> 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?
     
    A L, Jun 15, 2010
    #5
  6. A L

    A L Guest

    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 <> wrote:
    > On Jun 15, 2:39 pm, Saeed Amrollahi <> 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?
     
    A L, Jun 15, 2010
    #6
  7. Paul Bibbings <> 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
     
    Paul Bibbings, Jun 15, 2010
    #7
  8. A L

    A L Guest

    Hi Paul,
    On Jun 15, 4:52 pm, Paul Bibbings <> wrote:
    > Paul Bibbings <> 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.
     
    A L, Jun 15, 2010
    #8
  9. A L <> writes:

    > Hi Paul,
    > On Jun 15, 4:52 pm, Paul Bibbings <> wrote:
    >> Paul Bibbings <> 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
     
    Paul Bibbings, Jun 15, 2010
    #9
  10. Paul Bibbings <> 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
     
    Paul Bibbings, Jun 15, 2010
    #10
  11. A L

    A L Guest

    On Jun 15, 5:11 pm, Paul Bibbings <> wrote:
    > 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.
     
    A L, Jun 15, 2010
    #11
  12. A L <> writes:

    > On Jun 15, 5:11 pm, Paul Bibbings <> wrote:
    >> 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.


    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
     
    Paul Bibbings, Jun 15, 2010
    #12
  13. A L

    A L Guest

    Hi Paul,

    On Jun 15, 6:04 pm, Paul Bibbings <> wrote:
    > 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.
     
    A L, Jun 16, 2010
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    8
    Views:
    864
    Dhruv
    Jul 2, 2003
  2. 0to60
    Replies:
    4
    Views:
    530
    jeffc
    Nov 21, 2003
  3. Jason Heyes
    Replies:
    8
    Views:
    737
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,967
    Csaba
    Feb 18, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,362
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page