Differences between version 4 and predecessor to the previous major change of C++Notes.
Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 4 | Last edited on Thursday, September 23, 2004 3:26:56 pm | by PerryLorier | Revert |
Older page: | version 2 | Last edited on Monday, May 31, 2004 5:44:06 pm | by PerryLorier | Revert |
@@ -8,4 +8,31 @@
''InetTCPSocket'' is an abstract base class used as in interface. The reason this error was generated was because one function was not declared as abstract, I had forgotten the '= 0' part of the definition.
Another symptom of this is the linker complaining "vtable undefined for class ''x''"
+----
+!!!std::map<>'s crashing
+
+I (PerryLorier) was having a problem after assigning something to a std::map<> the program would crash.
+My (abbreviated) code:
+
+ bool operator <(const foo_t &a, const foo_t &b)
+ {
+ if (a.foo < b.foo) return -1;
+ if (a.foo > b.foo) return 1;
+ return 0;
+ }
+
+ std::map<foo_t,int*> foo_map;
+
+...
+ foo_map[[foo]=new int;
+ *foo_map[[foo]=6;
+
+And this code crashed. The reason was that my operator < was wrong. it voilated the contact of a < operator. I changed the "bool operator <" to "int cmp" and then defined operator <:
+ bool operator <(const foo_t &a, const foo_t &b) { return cmp(a,b) < 0; }
+I also recommend adding some assert(3)'s to verify that your operators don't voilate the rules at runtime.
+
+
+
+----
+CategoryProgramming