473,387 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloading template operators C++6 to VS2005

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<nSizeDBString<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 nSizeclass DBString
{
public:
............
// copy assignment (different size)
template<int nInSizeoperator =(const DBString<nInSize>& strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValu e,nInSize);
nIndicator = strNewValue.nIndicator;
}

// copy assignment (same size)
DBString<nSizeDBString<nSize>::operator =(const DBString<nSize>&
strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValu e,nSize);
nIndicator = strNewValue.nIndicator;
}
Oct 3 '06 #1
7 2070
AlanJSmith wrote:
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 nSizeclass DBString
{
public:
...........
// copy assignment (different size)
template<int nInSizeoperator =(const DBString<nInSize>& strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValu e,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<nSizeDBString<nSize>::operator =(const DBString<nSize>&
strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValu e,nSize);
nIndicator = strNewValue.nIndicator;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 4 '06 #2

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:D_******************************@comcast.com. ..
AlanJSmith wrote:
>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 nSizeclass DBString
{
public:
...........
// copy assignment (different size)
template<int nInSizeoperator =(const DBString<nInSize>& strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValu e,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<nSizeDBString<nSize>::operator =(const DBString<nSize>&
strNewValue)
{
strncpy((char*)strValue,(char*)strNewValue.strValu e,nSize);
nIndicator = strNewValue.nIndicator;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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

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

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

Oct 4 '06 #3
AlanJSmith wrote:
[..]
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];

template<int nInSizeoperator =(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*)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; }
};
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 5 '06 #4
On Thu, 5 Oct 2006 01:00:31 +0100, "AlanJSmith"
<Aa**********@REMOVEALLCAPSNnorthgate-is.Ccomwrote:
>so here it is as a complete
compilable unit, 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];

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

};
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 UDBString& 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
Oct 5 '06 #5
Roland Pibinger wrote:
[..]

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

template<int UDBString& operator= (const DBString<U>& r) {
// your code
return *this;
}
};

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 5 '06 #6
On Thu, 5 Oct 2006 14:02:50 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Roland Pibinger wrote:
>[..]

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

You mean

template<int UDBString& operator= (const DBString<U>& r) {
Yes, that's it. It also works for integral types.

Best regards,
Roland Pibinger
Oct 5 '06 #7

"Roland Pibinger" <rp*****@yahoo.comwrote in message
news:45**************@news.utanet.at...
On Thu, 5 Oct 2006 14:02:50 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>>Roland Pibinger wrote:
>>[..]

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

You mean

template<int UDBString& 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;
}
Oct 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcin Vorbrodt | last post by:
I see a lot of source code where some operators are defined as class members, and some as friends, or just outside the class. Can someone please explain what the general rule of thumb is? I...
3
by: Starx | last post by:
I was wondering if someone could help me get around this problem I'm having. I'm writing a fraction class and would like to overload the binary arithmetic operators so that when any numerical data...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
4
by: AlanJSmith | last post by:
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...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
2
by: jimzat | last post by:
I am trying to simulate the execution of some PLC ladder logic in python. I manually modified the rungs and executed this within python as a proof of concept, but I'd like to be able to skip the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.