Overloading template operators C++6 to VS2005

A

AlanJSmith

I am converting some C++ projects from 6 to vs2005 in one of the headers i
have the code that follows this passage in which I get an error saying int
default cant be assumed so i added DBString<nSize> DBString<nSize>:: to the
copy assignment same size oporator 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.
I have been searching for an answer for a couple of hours now can any one
help please.

template<int nSize> class DBString
{
public:
............
// copy assignment (different size)
template<int nInSize> operator =(const DBString<nInSize>& strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValue,nInSize);
nIndicator = strNewValue.nIndicator;
}

// copy assignment (same size)
DBString<nSize> DBString<nSize>::eek:perator =(const DBString<nSize>&
strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValue,nSize);
nIndicator = strNewValue.nIndicator;
}
 
V

Victor Bazarov

AlanJSmith said:
I am converting some C++ projects from 6 to vs2005 in one of the
headers i have the code that follows this passage in which I get an
error saying int default cant be assumed so i added DBString<nSize>
DBString<nSize>:: to the copy assignment same size oporator 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. I have been searching
for an answer for a couple of hours now can any one help please.

This seems to fall under FAQ 5.8. Please take time to read it before
your next post.
template<int nSize> class DBString
{
public:
...........
// copy assignment (different size)
template<int nInSize> operator =(const DBString<nInSize>& strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValue,nInSize);

There seems to be no check if 'nInSize' is greater than nSize, which
might present a problem, IME...
nIndicator = strNewValue.nIndicator;
}

// copy assignment (same size)
DBString<nSize> DBString<nSize>::eek:perator =(const DBString<nSize>&
strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValue,nSize);
nIndicator = strNewValue.nIndicator;
}

V
 
A

AlanJSmith

Victor Bazarov said:
This seems to fall under FAQ 5.8. Please take time to read it before
your next post.


There seems to be no check if 'nInSize' is greater than nSize, which
might present a problem, IME...


V

Ok you were right i didnt read the faq's so here it is as a complete
compilable unit, 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];

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>::eek:perator =(const DBString<nSize>&
strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValue.strValue,nSize);
nIndicator = strNewValue.nIndicator;
}

};


I have corrected the prob with nSize and nInSize as suggested but i knew i
would have to do that. the prob isnt with the content of the overloaded
operators but the syntax of the decaration which 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

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.
 
V

Victor Bazarov

AlanJSmith said:
[..]
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];

template<int nInSize> operator =(const DBString<nInSize>&

You wrote

template<int nInSize>
/***what return value type???***/
operator =(

That's what your compiler is asking about.
strNewValue) {

_strncpy_s((char*)strValue,nSize,(char*)strNewValue.strValue,nInSize);
}
// copy assignment (same size)
DBString<nSize> DBString<nSize>::eek:perator =(const DBString<nSize>&
strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValue.strValue,nSize);
nIndicator = strNewValue.nIndicator; }
};


I have corrected the prob with nSize and nInSize as suggested but i
knew i would have to do that. the prob isnt with the content of the
overloaded operators but the syntax of the decaration which 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

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.

Flummoxed? Really?

There are three essential syntaxes for a function declaration.

First is a constructor/destructor, which are special since they don't have
the return type *at all*.

Second is a conversion function in the form of

operator {sometype} ();

and the return value type is the {sometype} in its declaration.

The third, *and the most common* syntax is

{sometype} {functionname} ( [{arguments}] );

Those include overloaded operators whose name is somewhat special, which
has the form 'operator@' where the '@' is the actual operator symbol (can
have more than one character).

To make it any of those a template you just put "template< args >" in
front of them, right?

So, where did the return type go in your operator= declaration?

V
 
R

Roland Pibinger

so here it is as a complete
compilable unit, 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];

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>::eek:perator =(const DBString<nSize>&
strNewValue)
{
_strncpy_s((char*)strValue,nSize,(char*)strNewValue.strValue,nSize);
nIndicator = strNewValue.nIndicator;
}

};

What you (maybe) try to do is to define two operator= for your class
template DBString: one 'standard' operator= and one that accepts
DBStrings of different nSize, something like:

template<int nSize>
class DBString
{
public:
// copy assignment (same size)
DBString& operator =(const DBString& strNewValue) {
// your code
return *this;
}

// copy assignment (different size)
template<class U> DBString& operator= (const DBString<U>& r) {
// your code
return *this;
}
};

This doesn't work, because the template parameter is an integral type.


Best wishes,
Roland Pibinger
 
V

Victor Bazarov

Roland said:
[..]

// copy assignment (different size)
template<class U> DBString& operator= (const DBString<U>& r) {

You mean

template said:
// your code
return *this;
}
};

This doesn't work, because the template parameter is an integral type.

Huh?

V
 
R

Roland Pibinger

Roland said:
[..]

// copy assignment (different size)
template<class U> DBString& operator= (const DBString<U>& r) {

You mean

template<int U> DBString& operator= (const DBString<U>& r) {

Yes, that's it. It also works for integral types.

Best regards,
Roland Pibinger
 
A

AlanJSmith

Roland Pibinger said:
Roland said:
[..]

// copy assignment (different size)
template<class U> DBString& operator= (const DBString<U>& r) {

You mean

template<int U> DBString& operator= (const DBString<U>& r) {

Yes, that's it. It also works for integral types.

Best regards,
Roland Pibinger

Thank You Roland and Victor,

yes i have it working now with the problem was i kept using DBString<nSize>&
as the return type and this caused the compiler to break.
this was all written 11 years ago by someone else so i am not sure why they
didnt have a return type.

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;
}
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top