SEARCH YOUR SOLUTION HERE  

Strange error with iterators

I've wrote this function which should add a comma for every 3 digits in a number (so that it looks something like 5,000). This is my function: std::string formatNumber(int number) { // Convert the int to a string. std::string ...

Posted On: Sunday 25th of November 2012 11:30:52 PM Total Views:  431
View Complete with Replies




Related Messages:

I got a strange error message when using fstream!   (204 Views)
#include #include #include using namespace std; class MyObj { public: int data; ifstream in; }; typedef std::vector MyObjVector; int main(int arc, char* argv[]) { MyObjVector myVector; MyObj obj; obj.data = 10; obj.in.open("test.txt"); myVector.push_back(obj); } In the code above, I want to use a vector of class MyObj, which have a ifstream object, but I got a message says that I can't access the private memeber of ios_base, both in MinGW and Visual C++, but if a commented the line myVector.push_back(obj); all thing is OK, can't any one help me! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
strange grammar about volatile and operator overload   (188 Views)
, The following code, Code: operator const Outer::Inner * volatile & (); 1. I think it means an operator &, which returns const Inner* type and takes no arguments, right 2. Adding volatile to return value means (I previously only used volatile to qualify variable) thanks in advance, George , On Jan 29, 10:47 pm, Jack Klein wrote: > On Tue, 29 Jan 2008 14:13:07 +0100, Hans Mull > wrote in comp.lang.c++: > > George2 schrieb: > > > The following code, > > > Code: > > > operator const Outer::Inner * volatile & (); > > > > > > 1. > > > I think it means an operator &, which returns const Inner* > > > type and takes no arguments, right That would be: const Outer::Inner * volatile operator & (); The above is an implicit conversion operator, converting the class in which it is defined to a Outer::Inter const* volatile&. > > > 2. > > > Adding volatile to return value means A top level volatile on a return value is ignored unless the type is a class type. Here 1) the type isn't a class type, but a reference, and 2) the volatile isn't top level, i.e. it isn't on the return type. What the volatile means here is that you limit enormously what the user can do with the pointer referred to. (As Alf pointed out, this is probably a case of exploiting the type system---what's important here isn't the semantics of volatile, but the fact that T* volatile isn't the same type as T* volatile.) > > > (I previously only used volatile to qualify variable) > > volaitle disables optimization > What a silly, incomplete, and absolutely wrong statement! It's incomplete, but that's all. Formally, except for a few special cases (e.g. with longjmp/setjmp), the semantics of volatile are implementation defined. Practically, however, most implementations define them as inhibiting some optimizations. > Consider this code: > extern volatile unsigned int vui; // defined and initialized elsewhere > unsigned int silly_func() > { > return vui * 2; > } > Are you really claiming that the compiler is forbidden from > making the strength reduction optimization of replacing the > multiply with a left shift by 1 No, but if the compiler inlines this function, and happens to find that it has vui in a register, as a result of a previous calculation, it will still reread it. (Or more correctly, it will still generate a machine instruction to load it from memory. On most modern hardware, this does not necessarily mean that it will actually reread it from global memory. In sum, the implementation defined semantics of volatile are, in most cases, totally useless for anything.) -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique oriente objet/ Beratung in objektorientierter Datenverarbeitung 9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
strange test question   (170 Views)
i got the following strange extra question in a written test today . since the test was abt c/c++ i guess this is on topic here --8
A strange problem of template   (247 Views)
Zheng Da wrote: > I want a use the class of map in a class with template, > and the type of elements in map is specified by the parameter of the > class's template. > For example, > template > class hashmap > { > map table; > public: > void find(const key &k){ > map::iterator it=table.find(k); } > }; > > int main() > { > hashmap hm; > } > > When I compile it, I get the error > test.cc:33: error: expected `;' before 'it' > I don't get it. Why I can use map in this class, but I can't define > its iterator Read up on 'typename' keyword and where it's supposed to be used. (Hint: there are FAQ entires that have it and there is more than one use of that keyword) V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask
Re: strange crash...or exit?   (214 Views)
Are you opening a file in that routine, and exiting that routine without closing it for Example.. void routine { int file file=fopen(xyz); .... .... ..I am done now. (But I forget to close the file) return. } You should close that file! Or at least put a flag check to see if the file is open. Dwayne
strange threading context   (204 Views)
This is my problem: I have two classes A and B: class A { void methodA(); } class B { void methodB(); struct S { int Size; }; S & getMyStruct(); S MyStruct; } methodA and methodB get executed in two different threads concurrently. They both access the variable Size in the structure MyStruct of type S. methodA gets a reference to this struct by calling getMyStruct which returns a reference to it. Here comes the strange part: if methodB alters the value of Size from 0 to 1, this change doesn't get reflected in methodA - methodA still reads a 0. The access to the struct MyStruct is sychronized by a mutex. Whats going on here Why are the changes of Size by thread executing methodB not visible to the thread executing methodA. I thought that struct myStruct lies in the context of both threads/methods.
strange crash - cast short* to int*   (191 Views)
the program below workes w/o problems on a GP2X and on the PC, but my PocketPC (using GCC 3.3.3) crashes. Very dissapointing, since I expect some speed boost from it. Thnak you for your help. int dx=..; // number of pixels width to draw // one pixel is one unsigned short // Get some pointer to the screen to draw a rect unsigned short* pScreen = getColorBuffer(x,y); int num_q = dx>>2; // number of 4 pixel pairs int num_r = (dx & 0x00000003); // remaining pixels -> loop unrolling // prepair a 2 pixel pair "long" unsigned long colcol = col | ((unsigned long)col
strange thing   (172 Views)
piece of code p_header->u16_headId=*((uint16_t *)&au8_buf[0]); p_header->u16_dataId=*((uint16_t *)&au8_buf[2]); p_header->u16_devClass=*((uint16_t *)&au8_buf[4]); p_header->u16_firmRev=*((uint16_t *)&au8_buf[6]); p_header->u32_devId=*((uint32_t *)&au8_buf[8]); printf("print 1\n"); p_header->u32_dataSize=*((uint32_t *)&au8_buf[12]); printf("print 2\n"); p_header->u32_dataCRC=*((uint32_t *)&au8_buf[16]); p_header->u32_headerCRC=*((uint32_t *)&au8_buf[60]); on the output i have only print 1 is any logic explanation why i does not have print 2 is this my ide problem or its something i shuld know Raegerd -k
strange use of this pointer   (162 Views)
Suppose Member is a member of a class called Class, and Function is a function belonging to that class. Then, is there any reason to write this->Member within Function Surely, just writing Member means the same thing and is clearer
c++ strange allocation memory behavior   (179 Views)
suppose I have simple class like this : /*++++++++++++++++++++++++++++++++*/ #include #include #include #include using namespace std; class Array { // klasa array pamieta 10^8 elemntow char oraz //tablice "tablica" wskaznikow do slow zbudowana z nich char memory [10000000]; char** tablica; string current; int n; int i; // bierzaca pozycja w tablicy memory int length; }; /*+++++++++++++++++++++++++++++++++++*/ now somwhere in main I declare : main(){ Array A; } when I run program it crushes. but when I do call like that : main(){ Array A = new A(); } everything works fine. I use gcc3.4 on windows xp. Is it something linked with the system memory allocation (in this two case at every time the piece of memory will be declared in diffrent memory segment : once at data and second time at stack, but have no clue why it behaves like that).
Bug in my C++ program seems really strange. (Update on debugging progress)   (198 Views)
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even crippled all the constructors. So now all the operations and constructors that were used do is just return BigFloats but no memory is actually accessed at any point, nor is any allocated. However, when I reenable those parts of the constructor that allocate memory, and _then_ enable the part of the _de_structor that _frees_ the memory, the bug appears. It only seems when I go to allocate the "digits" buffer with (in constructor) digits = new DIGIT32[length]; and free with (in destructor) delete digits; or: (allocate in constructor) digits = (DIGIT32 *)malloc(length*sizeof(DIGIT32)); (free in destructor) free(digits); When I don't call free or delete, though, the crash does not appear. But it does not seem to be something needing the memory and then accessing it as I tried it with none allocated in the first place and "digits" set to a dummy pointer, and there was no bug nor any attempt to access the memory as that would crash the program. It only happens when you allocate memory in the constructor and free it in the destructor. Freeing it immediately after allocation, in the constructor, does not result in the crash bug. What gives, anyway
Preprocessor treatment of stdafx.h is strange   (212 Views)
I have a CodeGear2007[all updates] project that refernces a .cpp file in a directory I use for Visual Studio 2005. It has an include "stdafx.h" in it as do all my borland local .cpp files used in the project. The ...
name lookup in class scope occurs ambiguity , it's very strange.   (175 Views)
#include using namespace std; struct A { int a; }; struct B1 ublic A { int a; B1(){a=1;A::a=11;} }; struct B2 ublic A { int a; B2(){a=2;A::a=22;} }; struct C ublic B1,public B2 { void foo() { cout<
Reference lets a function to return a variable . But strange things happen :(:(   (179 Views)
I wonder how the 'm' value is 10 in the program below . It should be 7. Further, it turns later to 11. Strange. How is it possible Why Need clarification. Any ideas using namespace std; ...
This is a wrong code.I just want to know how it affect the compiler,becausethe error message is quite strange.   (244 Views)
today I forgot to include some header,then I found the error message by the compiler is quite strange. so I want to know exactly the inner details of the compiler impletation,if possible. and I want to know what does the ...
strange problem opening ofstream   (138 Views)
Hi all, I am having a really odd problem. My code is spitting out a bunch of binary files through ofstream. Actually, I have a little wrapper for binary ofstream operations....where ofstream is a member of the wrapper (i.e. not ...
Bug in my C++ program seems really strange. (Update on debugging progress)   (285 Views)
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even crippled all the constructors. So now ...
strange problem of sorting   (136 Views)
I am reading an old book - Programming Pearls 2nd edition recently. It says, "Even though the general C++ program uses 50 times the memory and CPU time of the specialized C program, it requires just half the code and ...
strange overloaded operator problem   (135 Views)
This might be an IDE question, but we have a problem that is stumping us. We have a custom Complex_ class in a library that provides basic complex math functions. It includes some unary overloaded member operators. In the same ...
Some strange error   (144 Views)
I took old code and decided to modify it a bit, and I just noticed that it does not compile at all and before server one of severs (main) crashed in the system it was working fine (I am really ...