Also: Why are you writing
DataType *Ptr = new DataType();
instead of simply writing
DataType Obj;
?
Also: Why are you writing
DataType *Ptr = new DataType();
instead of simply writing
DataType Obj;
?
What would you do with variable-length arrays that you can't do better with C++ vectors?
Remove the call to srand from the choose_number function.
As a general rule, you should call srand only once in your entire program.
The FBI, CIA, and the military comes to your house via dragon and breaths fire onto your soul sending you to hell for eternity to burn, simply because you deleted a pointer twice. So next time be aware.
And don't forget Basement Cat!
The results are undefined, which means that a program that does so is permitted to do anything at all.
By default C++ prints floating-point numbers in scientific notation if they are so large that doing so will provide more compact output than fixed-point notation. By sending "fixed" to the output stream, you are asking it to print in fixed-point notation (i.e. with a decimal point at a fixed place in each output number) regardless of value.
If you replace "fixed" by "scientific," it will print in scientific notation regardless.
What happens if you change
... << setprecision(2) << ...
to
... << fixed << setprecision(2) << ...
?
You say: "a smart compiler could optimise this by seeing that the next lines (below) of this function never use the derived part of d1 and d2 and that the base part has an identical content"
No it can't. The only way a compiler could do so would be if the program's behavior didn't change. But the moment you make your program depend in any way on d1 and d2 having different addresses, putting d1 and d2 in the same place would change the program's behavior. So that's not an acceptable optimization.
Different objects have different addresses.
You say "2 different objects share the same memory location if their content are identical."
The thing is: If they share the same memory location, they are not different objects.
IIRC, if a function that is declared to return a value falls off the end without doing so, the C89 standard permits this state of affairs so long as the caller does not try to use the value that the function failed to return. In other words:
int foo()
{
/* This function should return an int but doesn't */
}
int main()
{
foo(); /* Discards the invalid value, so the call is OK */
foo() + 0; /* Undefined behavior */
}
I do not remember for sure whether C99 tightened this rule.
When I try to run your code, it fails because "it" has the wrong time. When I remove the erroneous assignment to "it" in line 6, and the statement that tries to use "it" in line 8, the code prints 4 (correctly), not 6.
So if your code is printing 6, that means that what you're running is different from what you posted. I am not going to waste any more time by trying to read your mind.
If you want help with your code, you have to post the code that is having problems. You can't expect people to figure out what's wrong by looking at different code.
&*raiz means "give me the address of the contents of the thing the pointer raiz is pointing to"; '&' is an operator. It's a bit silly, as by definition raiz itself equals the address of the thing it is pointing to.
Actually, &*raiz by itself would make sense if raiz were, say, an iterator rather than a pointer. What doesn't make sense is to try to assign a value to &*raiz -- unless *raiz has a type with unary operator& defined. Such types are best avoided in any event.
How about explaining what you expect the statement to do? It looks like nonsense to me.
I suspect the solution is
std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_) const
The point is that at present, passengers_at_floor is permitted to modify its object, but you're passing it a const object, namely the key from a map.
So you need to add const to the definition of passengers_at_floor, as a promise that it will not modify its object. Of course you need to add it to the declaration inside the class as well, not just to the definition.
Please use code tags.
Are you sure that your user isn't picking k instead of K?
Also... Please use code tags.
In your search function, counter should be a reference, not a pointer. So you should delete the * on line 17, and in line 98, you should change "int * counter" to "int & counter".
rvalue-lvalue qualification is not the same as cv-qualification. You can have a const or non-const, lvalue or rvalue variable.
I think you're missing the point.
The C++ standard, clause 5.2.11, says that the result of const_cast<T>(e) is an lvalue if type T is a reference type; otherwise the result is an rvalue. So const_cast<myclass*>(anything) is an rvalue, because the type myclass* is not a reference type.
But, in theory, just reading or writing the value of the this pointer (not what it points to) is "valid" (if the compiler permits it), just like this code is valid, but useless:
void someFunction(int* some_ptr) { delete some_ptr; std::cout << "The pointer I just deleted had address: " << some_ptr << std::endl; some_ptr = NULL; // reading or writing some_ptr is valid and well-behaved after the delete. This also applies to the this pointer. };
I still believe you are mistaken. One of the interesting aspects of pointers is that it is undefined behavior even to copy the value of a pointer to memory that has been freed. Clause 3.7.3.2 of the C++ standard says that once memory has been deallocated, any pointer to that memory becomes invalid, and any use of such a pointer, even to pass it to a function, results in undefined behavior.
This behavior is inherited from C, and comes from some processor architectures that use different registers for addresses and other data. In such processors, the mere act of putting an address into an …
To give you an idea of how many worms are in this particular can, I'd like to isolate two lines from the OP's code:
delete this;
const_cast<myclass *>(this) = 0;
The program is already broken at this point, because once you have executed "delete this;" any subsequent use of this is invalid.
Come to think of it, the second statement is invalid anyway, even without "delete this", because this is not an lvalue. And even if it were, casting it to myclass* yields an rvalue, to which you can't assign.
If your compiler accepts this program, your compiler is broken.
You haven't shown the definitions of your classes. I presume your MyChild classes are derived from MyBase. In that case, I see nothing wrong with your code except that your MyBase class must have a virtual destructor for it to work correctly.
Your download function doesn't contain a return statement, which means that it doesn't return a value. So when you execute line 34, the variable "lines" is set to None. In line 35, when you try to select elements of Lines, the Python system tells you that you can't select elements of None.
If your goal is for download to return the value of the local variable "lines," which is completely distinct from the variable "lines" in line 34 of your program, then you need to put
return lines
at the end of your download function (appropriately indented, of course).
Line 15 uses hobby as the first argument of strcpy without ever having initialized it.
You haven't posted all of your code, so it's hard to know what all your problems are. However, the code that you posted for charStack::push ahd charStack::pop is broken. To see this, try creating a charStack object, pushing a few characters onto it, and then popping them one at a time and printing them. What you get should, of course, be the same as what you pushed, just in reverse order; but that's not what your code actually does.
And do you have a declaration of the string type in scope?
I see nothing obviously wrong with the code you've shown. However:
1) You should be aware that writing a parameter of the form string foo[]
means exactly the same as if you had written it as string *foo
-- that is, the parameter is just a pointer.
2) If you don't have a declaration of the string type in scope, the compiler won't know what it means.