sepp2k 378 Practically a Master Poster

You must use format specifiers at the places in the format string where you want your arguments to appear.

sepp2k 378 Practically a Master Poster

The error means that the readUser function expects an argument of type char*, but you're giving it a char.

sepp2k 378 Practically a Master Poster

Your printf format strings don't include any specifiers (like %s, %d, %f etc.). So printf does not expect any arguments after the format string, but you do give arguments. These arguments will be ignored and the compiler warns you about this because that's most probably not what you want.

sepp2k 378 Practically a Master Poster

'F' and 'T' are constants. Something like if('F' && 'T') will always be true. You could just as well write if(true). As I said, what you want to do is to check the truth values that you read, not some constants.

Also note that after you read the second truth value, you no longer have access to the first because you reuse the same variable. This makes it impossible to properly check both truth values, so you should change that.

sepp2k 378 Practically a Master Poster

When you say "functor" are you using the term in the C++ sense (i.e. a callable object) or something else?

And would a good example be the most simple one that shows you how to create one or something that shows you where they'd be useful?

sepp2k 378 Practically a Master Poster

Your variables letter1 and letter2 will always contain the values 'P' and 'Q' respectively (using the given input file, I mean). So doing things like if(letter1) doesn't make sense.

Inside the loop you should be reading the two truth values on each line into one variable each and then check those instead of letter1 and letter2. Keep in mind that they will be 'T' or 'F' rather than true or false.

You should never just output the truth value you just read. Instead you should output T when the given condition is true and F otherwise.

sepp2k 378 Practically a Master Poster

But, why can't we write all the functions directly in .h file and include it in my project files?

Because then linking would fail with a "multiple definitions" error if you include the same header into more than one compilation unit (i.e. if you include it from more than one .c file in the same project).

In header file, we have header guard which prevent to have the definitions again and again.

That will prevent the header being included multiple times into the same compilation unit. It will not prevent different compilation units from including the same header (after all different compilation units need to be able to include the same header).

nitin1 commented: nice. +4
sepp2k 378 Practically a Master Poster

<string.h> (also available as <cstring>) is a C header that declares functions that work on C strings (i.e. 0-terminated char*s). The std::string class is defined in <string>. You'll also need to refer to it as std::string as it's located in that namespace.

sepp2k 378 Practically a Master Poster

When you say 'member' are you refering to the, what would be an object if the structure were a class or something else?

No, he's talking about what comes after the . in foo.bar (foo is the object, bar is a member of that object) or in other terms: members are the things you define inside the class/struct/union body.

sepp2k 378 Practically a Master Poster

A standard compliant compiler must produce the same code in both cases. This is not even a matter of optimization, really, it's a matter of standard compliance.

That's not true. The standard makes absolutely no guarantees about what the generated instructions look like - it only makes guarantees about the behavior of the code. So as long as two pieces of code act the same, it does not matter (as far as standard compliance is concerned) whether they're doing so using the exact same instructions.

There are two operations involved here: allocating the space on the stack and setting the memory to the value of 10.

Yes, but (depending on the architecture of course) there may be one instruction that can perform both operations at once: push. It's theoretically conceivable that a simple compiler could translate int a = 10; to pushl $10 and int a; a = 10; to subl $4, %esp, followed by movl $10, (%esp) (completely ignoring calling conventions, %ebp etc. and that any sane compiler is going to increment the stack pointer all at once when the function is called - not once per variable).

Since those two pieces of code are completely equivalent as far as their observable behavior is concerned, there is nothing in the standard that forbids a compiler from doing this.

Or for a (somewhat) more realistic example: Let's say the compiler initializes all uninitialized variables to something like 0xDEADBEEF for debugging purposes. Now let's say that same …

sepp2k 378 Practically a Master Poster

Edit: Please ignore me.

sepp2k 378 Practically a Master Poster

I assume it only looks at the last 5 bits of the right operand to << and ignores anything else (presumably that's how it's implemented in the CPU). So 32 is the same as 0, 33 as 1 etc. The reason it's allowed to do that is that, as I said, it's undefined behavior and thus any behavior is allowed.

sepp2k 378 Practically a Master Poster

If that's all your code, you never give a value to any of your lower-case variables.

sepp2k 378 Practically a Master Poster

Shifting an n-bit integer by m bits when m > n invokes undefined behavior in C and C++.

If you change your code like this, it will give you 0 from count 32 onwards as you'd expect:

unsigned int t = 1;
for (unsigned int p = 0; p < 40; p++)
{
    cout << "Count: " << p << " - "<< t << endl;
    t = t << 1;
}

Here I made two changes to your code:

  1. Instead of shifting t by p, I shift t by one and store the result in t. This will still lead to t having been shifted p times, but since I only ever shift by 1 bit at a time, I don't invoke undefined behavior.

  2. I made t into an unsigned integer. The reason for that is that signed integer overflow invokes undefined behavior as well.

sepp2k 378 Practically a Master Poster

I haven't read How to Design Programs, so I don't know how good it is, but if you're having success so far, I'd say just stick with it. In my opinion Racket/Scheme is a much easier language to learn than C++, so I wouldn't recommend starting with C++ (though you certainly can).

I don't know what Dr.Racket is but looks like some form of haskell.

Racket is a dialect of Scheme, which is a dialect of Lisp. It's not related to Haskell. Dr Racket is an IDE for Racket.

chriswelborn commented: +1 for clearing up the origin of Racket. +4
sepp2k 378 Practically a Master Poster

A destructor is called (automatically) when an object is destroyed. It does not itself destroy the object. An object is destroyed when it goes out of scope or when delete is used on it (in case of objects created using new).

You should never call a destructor manually unless you're doing advanced manual memory management (like defining your own allocator using placement new).

sepp2k 378 Practically a Master Poster

Can you show us the other files (or rather a minimal version of them that still produces the same error message) in the project and how you build the project?

sepp2k 378 Practically a Master Poster

Use a while loop.

sepp2k 378 Practically a Master Poster

So I just started using the Goto command

Perfect time to stop.

But when I type in follow or Follow it gives me the text that I intend ( You get up and go to the door) but it still gets to the first goto it finds and sends me back to the beggining

That's because the goto isn't part of the else-block. It's outside of the if-else and thus gets executed either way (same as the print statement before it other than the first one).

To clarify this, here's what your code looks like indented correctly:

if ((sof=="follow")||(sof=="Follow"))
{
    cout<<"*You get up and go to the door*"<<endl;
    cout<<"-Well thats it for now xD"<<endl;
}
else
    cout<<"*5 hours later*"<<endl;

cout<<"-So, not getting anywhere, huh?"<<endl;
cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
cout<<"*You feel a strange buzz*"<<endl;
cout<<"So here we are at your first choice."<<endl;
cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
goto firsttp;

As you see, everything other than "5 hours later" happens regardless of whether the if- or the else-block was hit. To add the other statements to the else-block, you'll need to add braces around them.

And stop using goto.

ddanbe commented: Wise advice! +15
sepp2k 378 Practically a Master Poster

You can't call constructors in the class body. You have to do it in the initializer list of your class's constructor. Like so:

class Activity : public Event
{
  private:
    std::vector<std::string> activities;
    std::string newA;

  public:

    Activity(std::string act, std::string type, int id, int m, int d, int y):
      Event(type, id, m, d, y),
      activities(1),
      newA(act)
    {}

    // ...
};

PS: You need to qualify standard library classes with the std namespaces unless you have using statements, which you shouldn't use in header files anyway.

sepp2k 378 Practically a Master Poster

Can you show a complete code sample that causes that error? That line looks fine on its own.

sepp2k 378 Practically a Master Poster

You don't link individual functions - you link files. If all your files are added to the same project, the compiler (or rather the linker) will link all your files together.

If you get error messages about missing definitions, that means that the file with the missing definition is not part of your project or for some other reason (like a broken makefile or project file) does not get linked.

If you get error messages about missing declarations, that has nothing to do with linking. It simply means that you did not properly declare the function in your header file or did not include the header file in the file where the function is called. It may also mean that you have circular includes.

sepp2k 378 Practically a Master Poster

So how do I do the do while NOT thing

while(!thing)

Perhaps a switch -- case statement will do the trick here.

You can't switch on strings. And even if you could that would not help with looping.

sepp2k 378 Practically a Master Poster

Why are you trying to cast a string to an int pointer on that line? That is indeed suspicious.

sepp2k 378 Practically a Master Poster

You mean, you wrote a program that's asking for a password and using the backspace and delete keys does not work in that program (or at least doesn't work when it's prompting for the password)?

So what does happen when you press backspace? Could you show us the code of the program then? Which functions/library are you using to prompt for the password?

sepp2k 378 Practically a Master Poster

If the running time is equal to n^2 u for some unit u, then you can easily solve for u in 10^2 u = 1 second and then insert the solution in 25^2 u.

If the running time is merely in Theta(n^2) (which is usually what we mean when we say that the running time of something is quadratic), then the question is not answerable.

sepp2k 378 Practically a Master Poster

According to this list Visual Studio did not support initializer lists until version 2013. So you just won't be able to use this feature with your version of Visual Studio.

sepp2k 378 Practically a Master Poster

sizeof(states) gives you the number of bytes in the array. The number of bytes in an array equals the number of elements times the number of bytes per element. The elements in states are char pointers and apparently pointers have 4 bytes on your platform (so you're running a 32-bit system or at least a 32-bit compiler), so we get 4 * 4 = 16 bytes total.

sepp2k 378 Practically a Master Poster

I don't know why it gave me that portion of code if it's unusable

fn is unused, not unusable. You could replace line 19 with fn(rNum, ptr) and thus actually use fn.

sepp2k 378 Practically a Master Poster

That's not really true. The compiler will know whether you're using java.awt.Color regardless of whether or not you use an import. All the import does is to allow you to write Color instead of spelling out java.awt.Color. It's just a convenience feature.

JamesCherrill commented: Yes, Iknow. I was deliberately "over-simplifying" to try to get a point across to someone who was really struggling. JC +15
sepp2k 378 Practically a Master Poster

What's the relation between a class that has a main method and the String class? The relation is that there's a method that takes an instance of that class as an argument - that's all.

sepp2k 378 Practically a Master Poster

Are you asking why a method defined in Component can take a Color as its argument? Why wouldn't it be able to? It is perfectly common for methods to take objects of different in classes as arguments.

Even the standard main method takes an array of Strings as its argument even though it is not defined in the String class.

sepp2k 378 Practically a Master Poster

If you hadn't included functions.h in Main.cpp, you'd have gotten a compilation error telling you that print_hello and factorial were undeclared.

sepp2k 378 Practically a Master Poster

JApplet extends Component.

sepp2k 378 Practically a Master Poster

The purpose of the header files is to provide declarations for the functions, so that the compiler knows what their types are. This is necessary for the compiler to properly type check the code and generate the right code for function calls (it needs to know the types to generate the proper implicit conversions for example and it needs to know which functions are variadic because then the steps to call the function are likely different). It is unrelated to linking.

The contents of each .o file will simply be the code for the functions defined in the corresponding .cpp file with the caveat that function calls contain place holder addresses. For example factorial.o will contain the code for factorial, hello.o the code for print_hello and Main.o the code for main.

Then when the .o files are linked together, an executable is created that contains all of the code of the .o files together and the placeholders in the function calls are replaced with the actual addresses that the functions have in the executable file.

sepp2k 378 Practically a Master Poster

The current object is the object that the method is being called on, that is the object referred to by this.

we are not creating any object of any class

The Java plug-in will create an instance of your applet class and then call the appropriate methods on that instance.

sepp2k 378 Practically a Master Poster

int& length = 0; does not work because 0 is not an l-value. As I said an l-value is something like a variable or an array access - something that can be assigned to. You can't assign to 0.

Think of it this way: defining length as a reference means "whenever I change length, I want the target of the reference to also change". That is when you write int& length = x; (where x is an int variable) and then length = 42;, the value of x will now also be 42. If you write int& length = 0; and then length = 42;, that would mean that now the value of 0 is now also 42. Of course that doesn't make any sense - the value of 0 can't be 42; the value of 0 is 0. So you're not allowed to write int& length = 0;.

I don't use the & in the declaration only when passing it?

You use & if you want length to be a reference to something, that is if you want something else to change whenever length changes. If that's not what you want, you don't use &.

sepp2k 378 Practically a Master Poster

my question is that when we write applet then we write only methods defination not method calling so i am confuse about this that who call the methods of applets.

Think of it this way: When you write a normal Java application you write a main method, but you never write any code that calls the main method. So who calls the main method? The Java VM does when your application is run.

Likewise when your applet is loaded in a browser, the browser's Java plug-in will load your applet class and call methods on it. It just doesn't call the main method, but rather calls the apropriate listeners whenever an event occurs.

these methods call without anything so how should be calling methods with this syntax.

They're called on the current object. It's exactly the same as writing this.setForeground(Color.cyan);. When calling a method on the current object, the this. is simply optional in Java (this is not specific to applets).

sepp2k 378 Practically a Master Poster

As the message says int& length is a reference (that's what the & means). You can not define a reference without initializing it. That is you need to do int& length = foo; where foo is an l-value (like a variable, an array access, a member access or a function call that returns a reference).

Just writing int& length; is not allowed because then the compiler does not know what length is a reference to.

sepp2k 378 Practically a Master Poster

It still has a value

That's not necessarily true:

An uninitialized local variable may contain a trap representation for its type (and thus not a valid value), in which case reading it will invoke undefined behavior (possibly a crash).

Furthermore reading an uninitialized local variable whose address is never taken invokes undefined behavior regardless of whether or not the given type has a trap representation on your implementation.

sepp2k 378 Practically a Master Poster

Your first piece of code is illegal.

In C89 it's illegal because array sizes need to be compile-time constants, so variables are not allowed. A conforming compile should give an error message or at the very least a warning when compiling in C89 mode.

In C99 it's allowed to use variables as array sizes, but it's still illegal because size is uninitialized and thus using it invokes undefined behavior.

As a(n irrelevant) side note: it is defined, just not initialized. To declare a variable without defining it, you'd use the extern keyword.

---

Your second piece of code defines a one-dimensional array of size 5 because 2+3 is 5. A multi-dimensional array would be defined by using multiple pairs of brackets (like int siy[2][3]).

sepp2k 378 Practically a Master Poster

Yes, because secs is the third argument and hrs is the first. You can use either MyTime(0, 0, totalsec) or MyTime(secs = totalsec).

sepp2k 378 Practically a Master Poster

If you do print(t1), you'll use the __str__ method that you defined. The reason that it's not being called when you do print(t1.increment(300)) is that increment returns an integer, not a MyTime, so print prints it using the normal rules for printing integers. To fix this you should probably make increment return a MyTime rather than an integer.

sepp2k 378 Practically a Master Poster

Wait, do you want to create your own command line program that you start from the command line and into which you can then enter the command "display bla.txt" (which is what I though you meant) or do you want to create a display command so that you can enter "display bla.txt" into cmd?

sepp2k 378 Practically a Master Poster

As the error message says, tuples don't have a between method, so you can't do t.between(...) where t is a tuple.

You can call between on MyTime objects because you defined a between method for the MyTime class. So if you create an object of that class (by using MyTime(...)), you can call between on that object.

sepp2k 378 Practically a Master Poster

Assuming that the intended syntax is "'display', followed by one or more spaces, followed by the name of the file", you read a line of input, check whether the line starts with "display" followed by one or more spaces. If it doesn't, you display a "no such command" error. If it does, you iterate up to the first non-space character after "display", which is where the file name starts. Then you open the file with that name, read each line of the file until you hit the end of the file and print each line as you read it.

Then you put the whole thing into a loop, so it asks for commands until the window is closed or the user hits Ctrl-z or Ctrl-c.

sepp2k 378 Practically a Master Poster

You're creating two references to two different myHuman objects. If you had two references to the same object (as you would if you did myHuman person2 = person1 instead of myHuman person2 = new myHuman()), the fields would always have the same values, no matter which reference you access them through.

Example:

myHuman ref1 = new myHuman();
ref1.name "John Key";

ref2 = ref1;
Console.WriteLine(ref1.name);
Console.WriteLine(ref2.name); // Both will print "John Key"

ref2.name = "Keil Carpenter";
Console.WriteLine(ref1.name);
Console.WriteLine(ref2.name); // Now both will print "Keil Carpenter"

Basically every time you use new, you get a new object. And if you just assign one variable to another, you get two references to the same object.

sepp2k 378 Practically a Master Poster

I only ask this because because references have their own individual definitions of the objects fields that are not shared between other references.

What exactly do you mean by that?

sepp2k 378 Practically a Master Poster

pipelining and branch prediction at the CPU level maybe???

Branch prediction seems like a pretty good candidate. Note that the if condition data[c] >= 128 will randomly switch between being true and false when the array is unsorted, but when it is sorted it will be false consistently until the first element > 128 appears and after that it will be true all the time. So it will act much more predictably for a sorted array.

sepp2k 378 Practically a Master Poster

Abstract classes are sort of the C++ flavor of what is traditionally called "Interfaces" in the Java/C# OOP terminology.

I'd say they're the C++ flavor of what Java and C# call abstract classes. A C++ abstract class wouldn't translate to a Java/C# interface unless all the members are purely virtual.

if am rewriting this function in c# , should i need to write them necessarily as virtual

If you declare the method as abstract, it's automatically virtual, so you don't need to spell that out explicitly.