954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Visual Studio 6: Initializing static class member

The following works on solaris, but doesn't compile on windows visual studio 6. is there a way to do this differently so it compiles in both places?

In ComandLineArg.h
-------------------------
class CommandLineArg
{
private:
..etc..
static int longestDescr;
..etc..
};

In CommandLineArg.cpp
-------------------------------

CommandLineArg::longestDescr=0;

Error on visual studio:
error C2501: 'longestDescr' : missing storage-class or type specifiers

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

You forgot the type:

int CommandLineArg::longestDescr=0;
Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

I didn't realize I needed to. Why do I need to specify the datatype? (The syntax looks to me as if it is supposed to return an int rather than that it IS an int..)


(Also, why do I not need to on Solaris Workshop compiler?)

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

>> Why do I need to specify the datatype?
It's a definition. Any valid definition is also a valid declaration, and standard C++ disallows implicit int declarations.

>> The syntax looks to me as if it is supposed to return an int rather than that it IS an int..
How does an object return anything? It might evaluate to something, but only after its definition during use.

>> Also, why do I not need to on Solaris Workshop compiler?
It's either pre-standard implicit int, where if a type is needed, int is assumed, or it's a compiler extension. I would guess the former.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

I added the int on it and it worked both on Sun and Visual Studio. Thanks.


(I now have to try and deal with all of MS VStudio's other really complicated STL related warnings and errors - of course all of the rest of the code works fine on SUN as well...)

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

Here's another good one for you. This also compiles (and runs fine on Solaris)

Properties.h

template <class K, class V> class Properties : public Printable
{
 ...etc...
 protected:
 
 map<K,V> myMap;
void addMap( map<K,V> mapToAdd);
 
...etc..
};


Properties.hpp

template <class K, class V> void Properties<K,V>::addMap( map<K,V> mapToAdd)
{
 myMap.insert( mapToAdd.begin(), mapToAdd.end() ); 
}


And the wonderfully succint error:
c:\development\danlibs\src\properties.hpp(129) : error C2664: 'class std::_Tree,class std::allocator >,struct std::pair,class
std::allocator > const ,class std::basic_string,class std::allocator > >,struct std::map,class std::allocator >,class std::basic_string<
char,struct std::char_traits,class std::allocator >,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::_Kfn,struct std::less,class std::allocator > >,class std::allocator,class std::allocator<
char> > > >::iterator __thiscall std::map,class std::allocator >,class std::basic_string,class std::allocator >,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::insert(class std::_Tree,class std::allocator >,struct std::pair,class std::allocator > const ,class std::basic_string,class std::allocator > >,struct std::map,class std::allocator >,class std::basic_string,class std::allocator >,struct std::less,class std::allocator > >,
class std::allocator,class std::allocator > > >::_Kfn,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::iterator,const struct std::pair,class std::allocator > const ,class std::basic_string,class std::allocator > > &)' : cannot convert parameter 2 from 'class std::_Tree,class std::allocator >,struct std::pair,class std::allocator > const ,class std::basic_string,class std::allocator > >,struct std::map,class std::allocat
or >,class std::basic_string,class std::allocator >,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::_Kfn,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::iterator' to 'const struct std::pair,class std::allocator > const ,class std::basic_string,class std:
:allocator > > &'
Reason: cannot convert from 'class std::_Tree,class std::allocator >,struct std::pair,class std::allocator > co
nst ,class std::basic_string,class std::allocator > >,struct std::map,class std::allocator >,class std::basic_string,class std::allocator >,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::_Kfn,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >::iterator' to
'const struct std::pair,class std::allocator > const ,class std::basic_string,class std::allocator > >'
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\program files\microsoft visual studio\vc98\include\xmemory(70) : while compiling class-template member function 'void __thiscall Properties,class std::allocator >,class std:
:basic_string,class std::allocator > >::addMap(class std::map,class std::allocator >,class std::basic_string
,class std::allocator >,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > > >
)'

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
And the wonderfully succint error:

http://www.bdsoft.com/tools/stlfilt.html

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Dave,

(It looks like I have to install perl for this to work, is that correct?)

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

I was meaning to try to mess with it, but I never got around to it -- but I believe so.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Are you trying to define that function outside of the header? That ussually can cause problems.

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

Yes, but it's in an .hpp, and all files that require the class include the .hpp file (rather than the .h). in addition, it works perfectly fine on Solaris, only using Visual Studio does it not compile...

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

I just wrote this up real quick does this compile on your VC++. It works for me on Dev-C++(MinGW(GCC)) :)

#ifndef tester_h
#define tester_h

#include <map>

template<class K, class V> class tester
{
    public:
    std::map<K,V> myMap;
    void addMap(std::map<K,V> mapToAdd);
};

#endif
#ifndef tester_hpp
#define tester_hpp
#include <map>
#include "tester.h"

template<class K, class V> void tester<K,V>::addMap(std::map<K,V> mapToAdd)
{
    myMap.insert( mapToAdd.begin(), mapToAdd.end() ); 
}

#endif
prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

Note that the code I posted before compiles fine in visual studio .net 2003, so it's gotta be something specific with visual studio 6...

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You