template overloading operator c++ 6 to vs2005

A

AlanJSmith

I am converting some C++ projects from 6 to vs2005

I turned off precompiled headers and common lanuage runtime
support.

template<int nSize> class 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 nInSize> operator =(const DBString<nInSize>& strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValue.strValue,nInSize);
}

// copy assignment (same size)
DBString<nSize> DBString<nSize>::blush:perator =(const DBString<nSize>&
strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValue.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.
 
C

Carl Daniel [VC++ MVP]

AlanJSmith said:
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*)strNewValue.strValue,nInSize);
return *this;
}

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

which compiles just fine with VC8.

-cd
 
A

AlanJSmith

:
AlanJSmith said:
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*)strNewValue.strValue,nInSize);
return *this;
}

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

_strncpy_s((char*)strValue,nSize,(char*)strNewValue.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
 
C

Carl Daniel [VC++ MVP]

AlanJSmith said:
:
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/ThinkingInCPP2e.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
 
A

AlanJSmith

Carl Daniel said:
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/ThinkingInCPP2e.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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top