A L <> writes:
> On Jun 15, 5:11*pm, Paul Bibbings <paul.bibbi...@gmail.com> 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
|