|
If I have a c++ template class declared in Point.hpp file as below
template <typename>
class Point
{
....
}
If I want a new class derived from the above class in Node.hpp file but the data type is specialized to be double:
class Node: public Point<double>
{
...
}
When I compile the codes, the compiler complains that in Node.hpp that the base class Point is undefined. I know that I have included Point.hpp in Node.hpp. Why that error? Or how can I derived a node class (which should be a non template class) from a template Point class with double data type?
|
|
|
|
|
After fixing your posted fake code to be real code it compiles fine for me
|
|
|
|
|
Why not let the function decided the size of the _Result parameter?
It'll be (_Last1-_First1+1) + (_Last2-_First2+1) firstly, then the function knows to keep it lean, also it can keep the extra elements at the end in memory if any, then size() <= capacity().
What is the desiners' idea?
template<class class="" outputiterator="">
OutputIterator set_union(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
InputIterator2 _Last2,
OutputIterator _Result
);</class>
|
|
|
|
|
Who says the thing the output iterator applies to has a size? Consider this code:
#include <iostream>
#include <algorithm>
#include <iterator>
int main(int, char**)
{
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 2, 4, 6, 8 };
std::set_union(a, a+5, b, b+4, std::ostream_iterator<int>(std::cout, ", "));
return 1;
}
It writes 1, 2, 3, 4, 5, 6, 8, to standard output. No size involved there...
The STL design of containers, iterators and algorithms is all about generality - iterators do not (repeat DO NOT) have to be associated with containers - they are a more general concept than that.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
All right.
But there is another question, about this code:
vector<int> va, vb, v;
set_union::(va.begin(), va.end(), vb.begin(), vb.end(), v.begin());</int>
It courses a runtime error, because I haven't make v large enough.
I don't know why it is designed this way, why it can't be done inside the function.
|
|
|
|
|
That would pose a serious limitation to how the function can be used.
For example, this would not be possible.
set_union(v1.begin(), v1.begin() + 3, v2.begin(), v2.end(), v1.begin() + 4);
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
followait wrote: I don't know why it is designed this way, why it can't be done inside the function.
In effect, it can be - use a back_inserter (which uses push_back to insert items) rather than a plain output iterator. To minimise allocations, use the reserve member of vector .
vector va, vb, v;
v.reserve(va.size() + vb.size());
set_union::(va.begin(), va.end(), vb.begin(), vb.end(), std::back_inserter(v));
That's one of those tricksy iterator adaptors I mentioned in an answer to one of your previous queries...
[edit]PS - I've told you why it's designed this way - to maximise generality.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
The current application i'm working on has a single vector having a list of structures with data as well as a flag set (true/false). If flag is true , i'll add the vector data to string A else string B.
Is there a performance difference of having 2 vectors rather than having a single vector or it doesn't matter?
Thanks in advance,
Mayur M
|
|
|
|
|
mayur8u wrote: Is there a performance difference of having 2 vectors rather than having a single vector or it doesn't matter?
I don't think there'll be any significant performance difference.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I have the following codes (partially copied for illustration) using template class:
template<typename T>
struct _BoundingBox
{
T x0, x1, y0, y1;
};
template<typename T>
class CMyObject
{
public:
typedef struct _BoundingBox<t> stBBOX;
CMyObject()
{
Init(); // do class initialization here
}
CMyObject(const CMyObject<t> &Obj) // copy constructor
{
// do some data copy here
....
_Box = Obj.GetBountingBox();
}
virtual ~CMyObject() {}
public:
stBBOX GetBoundingBox() { return _Box; }
protected:
stBBOX _Box;
};
int main(argc, char* argv[])
{
CMyObject<int> obj1
CMyObject<int> obj2(obj1);
}
After compile (using VC 2005, the compiler gave the following error message:
error C2662: 'CMyObject<t>::GetBoundingBox' : cannot convert 'this' pointer from 'const CMyObject<t>' to 'CMyObject<t> &'
with
[
T=int
]
Conversion loses qualifiers
However, if I change
_Box = Obj.GetBountingBox();
to
_Box = Obj._Box;
in the copy constructor, the error disappears.
Anyone knows what the problem is? Thanks. </t></t></t>
<div class="ForumMod">modified on Tuesday, February 24, 2009 5:18 PM</div>
|
|
|
|
|
You need to make GetBoundingBox a const method of CMyObject :
stBBOX GetBoundingBox() const { return _Box; }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks. I tried according to your suggestion and it worked.
But i still didn't get it why GetBoundingBox() should be a const function? what if the return value is a pointer?
|
|
|
|
|
The return value doesn't matter - it's where you use GetBoundingBox:
CMyObject(const CMyObject<t> &Obj)
{
....
_Box = Obj.GetBountingBox();
}
You are saying that Obj is const. Given that, you can only perform const methods on Obj. So, to use GetBoundingBox there, it has to be a const method.
A const method is making a promise that it doesn't alter the object on which it's called.
_Box = Obj._Box is implicitly 'const', as you are reading a member variable (it's an rvalue).
The error message gcc gives for your code is a bit better than VC++'s:
a.cpp: In copy constructor ‘CMyObject<T>::CMyObject(const CMyObject<T>&) [with T = int]’:
a.cpp:34: instantiated from here
a.cpp:20: error: passing ‘const CMyObject<int>’ as ‘this’ argument of ‘_BoundingBox<T> CMyObject<T>::GetBoundingBox() [with T = int]’ discards qualifiers
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I get an access violation when trying to add to a map in my release build. Since it works fine in debug, and if I turn off optimisations in release, I know its something I'm doing wrong. But I don't know what...
Interestingly (?), the code that fails is in a loop which works on the first iteration, and fails on the second, suggesting perhaps a problem with initialising (or reinitialising) something.
Any help or tips very much appreciated
The code that fails is (bold line):
ManInfo mi;
...
...
if (m_ManMap.count(lSubmainID) == 0)
{
mi.SubmainJunctionPt = m_pCADEngine->GetEntityPoint(pSubmainJnc->GetHandle(), 1);
mi.LatEndPt[0] = m_pCADEngine->GetEntityPoint(pFreeEnd->GetHandle(), 1);
mi.LatLen[0] = mi.SubmainJunctionPt.GetDistanceTo2D(mi.LatEndPt[0]);
mi.DistToRef = mi.SubmainJunctionPt.GetDistanceTo2D(m_ManRefPt);
m_ManMap[lSubmainID] = mi;
}
else
{
...
I have a structure thus:
struct ManInfo
{
Point3D SubmainJunctionPt;
Point3D LatEndPt[2];
double LatLen[2];
double DistToRef;
ManInfo()
{
SubmainJunctionPt.Construct(NO_ELEVATION, NO_ELEVATION, NO_ELEVATION);
LatEndPt[0].Construct(NO_ELEVATION, NO_ELEVATION, NO_ELEVATION);
LatEndPt[1].Construct(NO_ELEVATION, NO_ELEVATION, NO_ELEVATION);
LatLen[0] = -1.0L;
LatLen[1] = -1.0L;
DistToRef = -1.0L;
}
ManInfo& ManInfo::operator= (const ManInfo& second)
{
if (this != &second)
{
SubmainJunctionPt.Construct(second.SubmainJunctionPt);
LatEndPt[0].Construct(second.LatEndPt[0]);
LatEndPt[1].Construct(second.LatEndPt[1]);
LatLen[0] = second.LatLen[0];
LatLen[1] = second.LatLen[1];
DistToRef = second.DistToRef;
}
return *this;
}
};
And a map member variable thus:
typedef map<LONG, ManInfo> ManMap;
ManMap m_ManMap;
|
|
|
|
|
I don't know if it helps determine what the problem is, but I can fix it by disabling inline function expansion...
|
|
|
|
|
Given you've got an assignment operator, do you need a copy constructor as well? And a destructor?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Good suggestion Stuart, given that:
In C++, the implicitly declared assignment operator is a public and inline member function.
but it didn't help I'm afraid...
|
|
|
|
|
Well, reading around I see there are possible problems with static storage when using STL across DLLs and EXEs. My struct and map are defined and used in a DLL, but the class is dllexport ed to the EXE.
I've found that to make this work I have to turn off inlining in both the DLL and the EXE...so perhaps this is the issue??
|
|
|
|
|
That (very probably) explains it all if your C runtime is statically linked in the EXE or the DLL or both. If so, use a DLL runtime in DLL and EXE.
The reason this scenario ACCVIOs is that the DLL and EXE are using different heaps, whereas if they're using a DLL runtime, they use the same one.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks for looking at this Stuart, but still no joy. Both my EXE and DLL link the MT DLL runtime - unless its the multithreaded version that causes the problem... either way, since I need to make progress here, I switched out the std::map for a CMap and I'm golden
|
|
|
|
|
I don't have time to figure out why this doesn't work, so I just changed it to CMap instead!
|
|
|
|
|
Damn - I hate not being able to crush a bug
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I'm having the same problem. Are you inheriting from CObject and passing an instance to your View(s) via UpdateAllViews( , , instance )?
|
|
|
|
|
I have derived from the interface(IPimSrcContactNew),but the system did not invoke this API(IPimSrcContactNew::Create)?
Please help me. Thanks.
|
|
|
|
|
You need to do more than just inherit from it - it has to be a proper COM object.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|