| | |
Weird (?) problem using std::list ...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi!
I have a class called anItem which is defined something like this :
ok should be enough. Constructor/Destructor are empty.
At another place I use this class as a template for a std::list.
I can add and get items from that list without any problem, UNLESS I do the following.
If I add another Item to the anItem-class, it only works if it is not a CString type.
Examples :
above is OK!
Runtime Error ("Memory could not be read.").
Does any of you have an idea what the problem here is?
I am using MS Visual C++ 6.0; this is used in an MFC application.
Thanks!
I have a class called anItem which is defined something like this :
C++ Syntax (Toggle Plain Text)
class anItem { CString string1; CString string2; long bla1; long bla2; }
ok should be enough. Constructor/Destructor are empty.
At another place I use this class as a template for a std::list.
C++ Syntax (Toggle Plain Text)
std::list<anItem> list;
I can add and get items from that list without any problem, UNLESS I do the following.
If I add another Item to the anItem-class, it only works if it is not a CString type.
Examples :
C++ Syntax (Toggle Plain Text)
class anItem { CString string1; CString string2; char string3[10000]; //new char array long bla1; long bla2; }
above is OK!
C++ Syntax (Toggle Plain Text)
class anItem { CString string1; CString string2; CString string3; // new CString long bla1; long bla2; }
Runtime Error ("Memory could not be read.").
Does any of you have an idea what the problem here is?
I am using MS Visual C++ 6.0; this is used in an MFC application.
Thanks!
>I have a class called anItem which is defined something like this
I'll assume it's "something" like that rather than exactly like that because that class won't compile (no trailing semicolon) and even if it did would be unusable because all of the data members are private with no way to access them.
>Runtime Error ("Memory could not be read.").
Step through your code, and find out exactly when this happens and what you're doing at the time. Debugging remotely is difficult enough when the problem isn't vague.
I'll assume it's "something" like that rather than exactly like that because that class won't compile (no trailing semicolon) and even if it did would be unusable because all of the data members are private with no way to access them.

>Runtime Error ("Memory could not be read.").
Step through your code, and find out exactly when this happens and what you're doing at the time. Debugging remotely is difficult enough when the problem isn't vague.
I'm here to prove you wrong.
•
•
Join Date: Mar 2006
Posts: 4
Reputation:
Solved Threads: 0
youre right, sorry. here is the actual class :
so what happens is that the app crashes as soon as i compile with the comment removed and CString lspec is an attribute of the class.
Any Ideas anyone??
C++ Syntax (Toggle Plain Text)
class DataTreeItem { public: DataTreeItem(); virtual ~DataTreeItem(); CString projectpath; CString spec; CString name; char local[1024]; //CString lspec; int size; int type; long version; long checkedout; HTREEITEM treeitem; IVSSItem * vssitem; };
so what happens is that the app crashes as soon as i compile with the comment removed and CString lspec is an attribute of the class.
Any Ideas anyone??
>so what happens is that the app crashes
Define 'crash'.
>Any Ideas anyone??
No, because you're still not giving enough information. At this point, post a small program that exhibits the problem so that we can debug it personally rather than play twenty questions with you.
Define 'crash'.
>Any Ideas anyone??
No, because you're still not giving enough information. At this point, post a small program that exhibits the problem so that we can debug it personally rather than play twenty questions with you.
I'm here to prove you wrong.
So this doesn't run:
but this does:
It looks like a CString is equivalent to a char*. In that case, you need to allocate memory dynamically for it or use a char array.
Your best bet would be to use a string, from <string>. They automatically resize themselves.
C++ Syntax (Toggle Plain Text)
CString bad; bad[999] = 'a';
C++ Syntax (Toggle Plain Text)
char good[10000]; good[999] = 'a';
It looks like a CString is equivalent to a char*. In that case, you need to allocate memory dynamically for it or use a char array.
Your best bet would be to use a string, from <string>. They automatically resize themselves.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> int main(void) { string name; std::cout << "Enter your name: "; std::cin >> name; std::cout << "Hello, " << name << '!' << std::endl; return 0; }
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
![]() |
Similar Threads
- XSLT weird problem... [urgent] (RSS, Web Services and SOAP)
- HELP!!! Weird problem... (C++)
- Problem While Processing A String List (C++)
- weird problem : Internet Explorer cannot load all geocities pages (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Help with Map
- Next Thread: Compiling in Fedora Core 4
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






