By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

template overloading operator c++ 6 to vs2005

P: n/a
I am converting some C++ projects from 6 to vs2005

I turned off precompiled headers and common lanuage runtime
support.

template<int nSizeclass DBString
{
public:

// Indicates status of string, eg. length, NULL
long nIndicator;

// array of characters of appropriate length holds actual data
unsigned char strValue[((nSize/4)%4==0)? nSize : (nSize/4)*4];

// copy assignment different size oporator
template<int nInSizeoperator =(const DBString<nInSize>& strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValu e.strValue,nInSize);
}

// copy assignment (same size)
DBString<nSizeDBString<nSize>::operator =(const DBString<nSize>&
strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValu e.strValue,nSize);
nIndicator = strNewValue.nIndicator;
}

};
The syntax of the decaration causes me this error when
trying to compile.

\DBString.cpp(12) : error C4430: missing type specifier - int assumed. Note:
C++ does not support default-int
1.\DBString.cpp(24) : see reference to class template instantiation
'DBString<nSize>' being compiled

I had added DBString<nSize>
DBString<nSize>:: to the copy assignment same size oporator as this was
giving same error and this
seems to work, if i try the same thing with copy assignment different
sizes i get error C3856: 'DBString<nSize>::=': class is not a class
template and adding any type between template<int nInSize and
operator = without the DBString<nSize>:: gives me a c++ optimizer had
to close send error report to microsoft error.

obviously i have tried lots of variations on the syntax but i am still
flummoxed, there is the chance that what i am trying to do is not allowed in
VS2005 but i am not sure.

Oct 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
AlanJSmith wrote:
I am converting some C++ projects from 6 to vs2005

I turned off precompiled headers and common lanuage runtime
support.
[code snipped]
>
The syntax of the decaration causes me this error when
trying to compile.
VC6 was letting you getting away with some really loose code there. The
canonical form for copy-assignment operators is this:

// copy assignment different size oporator
template<int nInSize>
DBString& operator =(const DBString<nInSize>& strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValu e.strValue,nInSize);
return *this;
}

// copy assignment (same size)
DBString<nSize>& operator =(const DBString<nSize>& strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValu e.strValue,nSize);
nIndicator = strNewValue.nIndicator;
return *this;
}

which compiles just fine with VC8.

-cd
Oct 5 '06 #2

P: n/a

"Carl Daniel [VC++ MVP]" wrote :
AlanJSmith wrote:
>I am converting some C++ projects from 6 to vs2005

I turned off precompiled headers and common lanuage runtime
support.
[code snipped]
>>
The syntax of the decaration causes me this error when
trying to compile.

VC6 was letting you getting away with some really loose code there. The
canonical form for copy-assignment operators is this:

// copy assignment different size oporator
template<int nInSize>
DBString& operator =(const DBString<nInSize>& strNewValue)
{

_strncpy_s((char*)strValue,nSize,(char*)strNewValu e.strValue,nInSize);
return *this;
}

// copy assignment (same size)
DBString<nSize>& operator =(const DBString<nSize>& strNewValue)
{

_strncpy_s((char*)strValue,nSize,(char*)strNewValu e.strValue,nSize);
nIndicator = strNewValue.nIndicator;
return *this;
}

which compiles just fine with VC8.

-cd
Thank you Carl this is exactly what i was after the correct canonical
syntax. I am far from an expert in C++ but have been tasked with upgrading
the code written by someone else 11 years ago to a supported IDE.

If I could ask a favour, what book would you recomend as a reference ie: you
favourite C++ reference.

Regards,
Alan Smith
Oct 5 '06 #3

P: n/a
AlanJSmith wrote:
"Carl Daniel [VC++ MVP]" wrote :
Thank you Carl this is exactly what i was after the correct canonical
syntax. I am far from an expert in C++ but have been tasked with
upgrading the code written by someone else 11 years ago to a
supported IDE.
If I could ask a favour, what book would you recomend as a reference
ie: you favourite C++ reference.
First, go to www.accu.org and look through their book reviews. They have
100's of them on nearly every C and C++ book ever written.

To bring you up to speed, many people recommend:
"Accelerated C++" by Andrew Koenig and Barbara Moo.
"Thinking in C++" by Bruce Eckel. This book is available online for free:
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

For a more detailed language reference, the standard is "The C++ Programming
Language, 3rd ed." by Bjarne Stroustrup.

For the C++ standard library, the standard reference is "The C++ Standard
Library", by Nicolai Jusittus.

There are several books by Scott Meyers (Effective C++, More Effective C++,
Effective STL, More Effective STL, etc). These are all excellent and useful
books. Of all of these, "Effective C++" is a must-have for someone coming
up to speed on the subtleties of the language.

There are several books by Herb Sutter (Exceptional C++, More Exceptional
C++, etc). These are also excellent books.

Yeah, it's a lot of books - but then, C++ is a complex language used to do
complex things, so it takes a lot of books!

-cd
Oct 5 '06 #4

P: n/a

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:e4**************@TK2MSFTNGP05.phx.gbl...
AlanJSmith wrote:
>"Carl Daniel [VC++ MVP]" wrote :
Thank you Carl this is exactly what i was after the correct canonical
syntax. I am far from an expert in C++ but have been tasked with
upgrading the code written by someone else 11 years ago to a
supported IDE.
If I could ask a favour, what book would you recomend as a reference
ie: you favourite C++ reference.

First, go to www.accu.org and look through their book reviews. They have
100's of them on nearly every C and C++ book ever written.

To bring you up to speed, many people recommend:
"Accelerated C++" by Andrew Koenig and Barbara Moo.
"Thinking in C++" by Bruce Eckel. This book is available online for free:
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

For a more detailed language reference, the standard is "The C++
Programming Language, 3rd ed." by Bjarne Stroustrup.

For the C++ standard library, the standard reference is "The C++ Standard
Library", by Nicolai Jusittus.

There are several books by Scott Meyers (Effective C++, More Effective
C++, Effective STL, More Effective STL, etc). These are all excellent and
useful books. Of all of these, "Effective C++" is a must-have for someone
coming up to speed on the subtleties of the language.

There are several books by Herb Sutter (Exceptional C++, More Exceptional
C++, etc). These are also excellent books.

Yeah, it's a lot of books - but then, C++ is a complex language used to do
complex things, so it takes a lot of books!

-cd
Thank you Carl,

I have ordered, "The C++ Programming Language, 3rd ed." by Bjarne Stroustrup
and "The C++ Standard Library", by Nicolai Jusittus.

Thank you again for all your help.

Alan Smith
Oct 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.