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 ...

I got a strange error message when using fstream!
&nbsp(133 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
&nbsp(126 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
&nbsp(112 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
&nbsp(174 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?
&nbsp(159 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
&nbsp(137 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 use of this pointer
&nbsp(116 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
&nbsp(127 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)
&nbsp(134 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
&nbsp(116 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 ...

strange problem opening ofstream
&nbsp(93 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 ...

strange problem of sorting
&nbsp(96 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
&nbsp(86 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
&nbsp(100 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 ...