|
I figured it out. I'm using the CStdioFile class and reading in a line at a time. Works great. Thanks all
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Is it possible to use a list to store templated classes in C++? The compiler chokes and gives me a C3203 error in Visual C++.
|
|
|
|
|
Try using typedef.
Kuphryn
|
|
|
|
|
|
Here is the code
namespace mrg
{
template<class _Ty> class mrgFwdPt
{
public:
mrgFwdPt()
{
}
mrgFwdPt(_Ty v, Date dt) : _mVal(v), _mFwdDt(dt)
{
}
~mrgFwdPt()
{
}
inline void SetValue(_Ty v) { _mVal = v; }
inline void SetDate(Date d) { _mFwdDt = d; }
inline _Ty GetValue(void) { return _mVal; }
inline Date GetFwdDt(void) { return _mFwdDt; }
// these operators work on the date objects to allow
// sorting of the forward points by date
bool operator>(const mrgFwdPt<_Ty>& right) const;
bool operator<(const mrgFwdPt<_Ty>& right) const;
bool operator>=(const mrgFwdPt<_Ty>& right) const;
bool operator<=(const mrgFwdPt<_Ty>& right) const;
bool operator==(const mrgFwdPt<_Ty>& right) const;
bool operator!=(const mrgFwdPt<_Ty>& right) const;
protected:
_Ty _mVal;
Date _mFwdDt;
};
template<class _Ty> bool mrgFwdPt<_Ty>::operator>(const mrgFwdPt<_Ty>& right) const
{
return (_mFwdDt > right._mFwdDt);
}
template<class _Ty> bool mrgFwdPt<_Ty>::operator<(const mrgFwdPt<_Ty>& right) const
{
return (_mFwdDt < right._mFwdDt);
}
template<class _Ty> bool mrgFwdPt<_Ty>::operator>=(const mrgFwdPt<_Ty>& right) const
{
return (_mFwdDt >= right._mFwdDt);
}
template<class _Ty> bool mrgFwdPt<_Ty>::operator<=(const mrgFwdPt<_Ty>& right) const
{
return (_mFwdDt <= right._mFwdDt);
}
template<class _Ty> bool mrgFwdPt<_Ty>::operator==(const mrgFwdPt<_Ty>& right) const
{
return (_mFwdDt == right._mFwdDt);
}
template<class _Ty> bool mrgFwdPt<_Ty>::operator!=(const mrgFwdPt<_Ty>& right) const
{
return (_mFwdDt != right._mFwdDt);
}
//=========================== end of point class ==========================================//
template<class _Ty> class mrgFwdCrv
{
public:
mrgFwdCrv()
{
}
~mrgFwdCrv()
{
}
bool push_back(mrgFwdPt<class _Ty> fp)
{
_mList.push_back(fp);
return true;
}
bool test_sort(void);
private:
list<mrgFwdPt<class _Ty>> _mList;
};
template<class _Ty> bool mrgFwdCrv<_Ty>::test_sort(void)
{
Date d;
list <mrgFwdPt<class _Ty>>::iterator i;
for (i = _mList.begin(); i != _mList.end(); i++)
{
d = (*i)->GetFwdDt();
cout << d.DateString() << endl;
}
_mList.sort(less<mrgFwdPt<class _Ty>>());
for (i = _mList.begin(); i != _mList.end(); i++)
{
d = (*i)->GetFwdDt();
cout << d.DateString() << endl;
}
}
}
Class mrgFwdPt is a templated class. It works exactly as expected. When the second class mrgFwdCrv is added, the compiler chokes and I get all kinds of errors. What I would like to do is have the second class (mrgFwdCrv) handle a list of the mrgFwdPt classes. I thought about the typedef approach but wouldn't the compiler just make the substitutions I have here?
Thx
|
|
|
|
|
I did some corrections in your code, and now the below compiles with no errors.
Some operator overloadings are omitted ...
-------------------------------------------------------
#include <list>
namespace mrg
{
typedef int Date; // Assumption for this demo only.
template<class _Ty>
class mrgFwdPt
{
public:
mrgFwdPt()
{ }
mrgFwdPt(_Ty v, Date dt)
: _mVal(v)
{ }
~mrgFwdPt()
{ }
protected:
_Ty _mVal;
};
//=========================== end of point class ==========================================//
template<class _Ty>
class mrgFwdCrv
{
public:
mrgFwdCrv()
{
}
~mrgFwdCrv()
{
}
bool push_back(mrgFwdPt<_Ty> fp) // this line.
{
_mList.push_back(fp);
return true;
}
bool test_sort(void);
private:
std::list<mrgFwdPt<_Ty> > _mList;
};
template<class _Ty>
bool mrgFwdCrv<_Ty>::test_sort(void)
{
Date d;
list <mrgFwdPt<_Ty> >::iterator i;
for (i = _mList.begin(); i != _mList.end(); i++)
{
d = (*i)->GetFwdDt();
cout << d.DateString() << endl;
}
_mList.sort(less<mrgFwdPt<_Ty> >());
for (i = _mList.begin(); i != _mList.end(); i++)
{
d = (*i)->GetFwdDt();
cout << d.DateString() << endl;
}
}
}
void main()
{
mrg::mrgFwdPt<int> aa;
mrg::mrgFwdCrv<long> bb;
}
------------------------------------------------
------ Build started: Project: TestDos, Configuration: Debug Win32 ------
Compiling...
main.cpp
Linking...
Build log was saved at "file://d:\Tmp\TestEnv\VC7\DOS\TestDos\Debug\BuildLog.htm"
TestDos - 0 error(s), 0 warning(s)
---------------------- Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
<b>Maxwell Chen</b>
|
|
|
|
|
Worked like a champ. Thanks very much.
|
|
|
|
|
I'd need help on converting a char* to a dword OR a byte[4]
thanks!
|
|
|
|
|
i don't know if this is what you are looking for
but my this will help
<br />
DWORD str_num = atoi("12345");<br />
Pain is a weakness living the body
|
|
|
|
|
Didn't work
Actually I have a string like " b7333109 " and I wanna convert it to a dword
|
|
|
|
|
|
I am trying to overload a comparison operator in a templated class and while the class files compile without complaint, the actual comparison statement prompts a C2678 compiler error:
error C2678: binary '>' : no operator found which takes a left-hand operand of type 'mrg::mrgFwdPt<_Ty>' (or there is no acceptable conversion)
with
[
_Ty=double
]
relevant code is
namespace mrg
{
template<class _Ty> class mrgFwdPt
{
public:
mrgFwdPt()
{
}
mrgFwdPt(_Ty v, Date dt) : _mVal(v), _mFwdDt(dt)
{
}
~mrgFwdPt()
{
}
inline void SetValue(_Ty v) { _mValue = v; }
inline void SetDate(Date d) { _mFwdDt = d; }
inline _Ty GetValue(void) { return _mValue; }
inline Date GetFwdDt(void) { return _mFwdDt; }
// these operators work on the date objects to allow
// sorting of the forward points by date
bool operator>(const _Ty& right) const;
private:
_Ty _mVal;
Date _mFwdDt;
};
template<class _Ty> bool mrgFwdPt<_Ty>::operator>(const _Ty& right) const
{
return (_mFwdDt > right._mFwdDt)
}
}
in the main program:
mrgFwdDt<double> one, two;
.
.
.
if (one > two) <- generates the compiler error
FWIW the date comparison operator works correctly and the date class has been used as is for a couple years.
Running MS Visual Studio .Net 2002 (C++ ver 7)
Thx
|
|
|
|
|
Never mind, The parameter passed in the operator declaration and definition were incorrect, should have been classes.
|
|
|
|
|
Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck!
1) Identify any errors in the following declarations.
a. int x[4] = { 8,7,6,4,3 };
b. int x[] = { 8,7,6,4 };
c. const int SIZE = 4;
int x[SIZE];
2) What is wrong with the following piece of code?
int sample_array[10];
for (int index=1; index<=10; index++)
sample_array[index] = 3*index;
3) Suppose we expect the elements of the array a to be ordered so that
a[0] <= a[1] <= a[2] <=…
However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it?
double a[10];
<some code="" to="" fill="" the="" array="" a="" goes="" here.="">
for (int index = 0; index <10; index++)
if(a[index] > a[index +1])
cout <<”Array elements”<
|
|
|
|
|
soer wrote:
1) Identify any errors in the following declarations.
a. int x[4] = { 8,7,6,4,3 };
b. int x[] = { 8,7,6,4 };
c. const int SIZE = 4;
int x[SIZE];
Yes, the array x is being defined 3 different times.
soer wrote:
2) What is wrong with the following piece of code?
int sample_array[10];
You should be using std::vector instead of a static array.
soer wrote:
3) Suppose we expect the elements of the array a to be ordered so that
a[0] <= a[1] <= a[2] <=…
for (int index = 0; index <10; index++)
if(a[index] > a[index +1])
cout <<”Array elements”<<index<<” and”="" <<="" (index="" +1)<<”are="" out="" of="" order.”;<="" i="">
Yes, there is no space between the words and the numbers being printed out.
soer wrote:
4) Consider the following function definition:
Void tripler(int& n)
{
n=3*n;
}
That should be:
n *= 3;
and "Void" should have a lower-case v.
soer wrote:
5) What (if anything) is wrong with the following code? The definition of tripler is given in #11.
int b[5]={1,2,3,4,5};
for (int i=1;i<=5;i++)
tripler(b[i]);
The line containing "tripler" is not indented properly.
soer wrote:
6) Which of the following are acceptable function calls?
too2(my_array, 29);
too2(my_array, 10);
The function name is incorrect. It should be "tutu".
So how did I do?
Sometimes I feel like I'm a USB printer in a parallel universe.
|
|
|
|
|
hehehe
Ant.
|
|
|
|
|
Too easy .
Pain is a weakness living the body
|
|
|
|
|
Syntaxx and code form is not a problem in any of the problems here. Spaces b/w words dont matter nor does indentation. Try again!
|
|
|
|
|
Maybe those problems are concerned with as folloing:
1.Overflow
2.Reference
...
and I can't understand the last problem.
|
|
|
|
|
Using CxImage, I'm loading a PNG from a dll. The png has some transparent parts. When I display the image on the screen, the transparency seems to be ignored. I've looked thru the code but just can't seem to find what I am looking for. Can someone provide a sample or point me in the right direction.
Snip-it of my code from my ::OnPaint() handler (error checking has been removed):
CxImage *image = new CxImage();
HRSRC hres;
hres = FindResource( dll->getModuleHandle(), "#5099"; , "PNG");
image->LoadResource( hres, CXIMAGE_FORMAT_PNG, dll->getModuleHandle());
image->Draw2( dc, 10, 10, 247, 52);
I get the image but the transparent parts are not transparent.
Thanks,
Randy
|
|
|
|
|
Hello,
I need to draw some rectangle which each has a dot in the centre.
The dot representing the x and y cordinates.
Can anyone help.
lvidot
|
|
|
|
|
Check MoveTo and LineTo , FrameRect and
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
How can I do something like TRACE if I'm not using MFC or ATL ?
I want my function that displays complex data structures to be able to show the result somewhere inside the IDE.
//Peter
|
|
|
|
|
VOID OutputDebugString(
LPCTSTR lpOutputString // pointer to string to be displayed
);
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Ah, so easy...
Almost embarrassing I didn’t find it myself.
Unfortunately my project is compiled without language extensions (also developing on other targets) and without them I cannot include windows.h .
Do you know the correct signature for this function ?
I have tried with
extern "C" void OutputDebugString( char const * );
extern "C" void _cdecl OutputDebugString( char const * );
but the linker wouldn't have any of that.
Or is there a pragma to enable language extensions for a small part of a source file?
Thanks a lot // Peter
|
|
|
|