Got the book today, time to get busy :)
Quick question about namespaces , what happens when there are 2 namespaces with the same name, compile error im guessing ?
C++ Workshop - Getting Started with C++ (Ch. 1 & 2)
yep. unless those two namespaces are in enclosed in two other namespaces with different names.
No.
The two namespaces 'merge'. You can reopen a namespace.
You can also open the same namespace in different headers. It all gets combined into the same namespace. This is why the standard library can all be in namespace std, but still be spread over many files.
The two namespaces 'merge'. You can reopen a namespace.
namespace Foo //open namespace Foo{ void a_function();} //close namespace Foonamespace Foo //open namespace Foo again. No problem!{ void another_function(); //now namespace Foo contains TWO functions.} //close namespace Foonamespace Foo //still fine{ void a_function(); //Uhoh! Foo::a_function was already declared!}
You can also open the same namespace in different headers. It all gets combined into the same namespace. This is why the standard library can all be in namespace std, but still be spread over many files.
I recently stumbled on this workshop and think it is a great way to get back into computer programming. Is this still being actively moderated? If so, I plan on catching up fast.
Just got a copy of the book and have a question about Exercise 3 in getting started. The code looks perfect to me and does not give me any errors when compiling but question 4 says to fix the error. Is this a mistake or am I missing something?
Also, on page 92:
The book says "If x is not equal to 5, then (++y == 3) is not evaluated...
Shouldn't this read "If x is equal to 5..." ?
[Edited by - Nuc12 on November 11, 2006 4:22:24 PM]
Just got a copy of the book and have a question about Exercise 3 in getting started. The code looks perfect to me and does not give me any errors when compiling but question 4 says to fix the error. Is this a mistake or am I missing something?
Also, on page 92:
if( (x==5) || (++y == 3) )
The book says "If x is not equal to 5, then (++y == 3) is not evaluated...
Shouldn't this read "If x is equal to 5..." ?
[Edited by - Nuc12 on November 11, 2006 4:22:24 PM]
I can't help with the first question because I do not have the book and therefore don't have a clue what Excersize 3 looks like. I do however understand your second question.
Short answer: the second bit of code in the if statement does Not run when x==5. This is known as 'short-circuit evaluation'
http://www.codepedia.com/wiki/printer.aspx?WikiID=1&p=ShortCircuitEval
Basically, because the if statement is guaranteed to be true why waste processor cycles to evaluate the rest of the statement? You shouldn't be relying on 'side effects' in code like that anyway. 'Side effects?' you say.
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=187&rl=1
Hope this helps.
Short answer: the second bit of code in the if statement does Not run when x==5. This is known as 'short-circuit evaluation'
http://www.codepedia.com/wiki/printer.aspx?WikiID=1&p=ShortCircuitEval
Basically, because the if statement is guaranteed to be true why waste processor cycles to evaluate the rest of the statement? You shouldn't be relying on 'side effects' in code like that anyway. 'Side effects?' you say.
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=187&rl=1
Hope this helps.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!
THanks for the reply
But since this is an || (or) wouldn't it evaluate the second argument if the first one was false?
But since this is an || (or) wouldn't it evaluate the second argument if the first one was false?
Quote:
Original post by Nuc12
THanks for the reply
But since this is an || (or) wouldn't it evaluate the second argument if the first one was false?
I believe you are correct, for things like this the best thing to do would be to write small programs to test whether you are right or wrong. Something like the following should suffice:
int x = 4;int y = 2;if((x == 5) || (++y == 3)) cout << "(++y == 3) evaluated\n";else cout << "(++y == 3) not evaluated\n";
Looks like it is an error in the book.
How about them apples?
Hello I'm having a problem with one of the examples in the book Listing 2.7 pg 37 FUNC.cpp
originally in the book it looked like this.
and got these errors
functions.cpp(4) : error C2001: newline in constant
functions.cpp(5) : error C2017: illegal escape sequence
functions.cpp(5) : error C2001: newline in constant
functions.cpp(5) : error C2146: syntax error : missing ';' before identifier 'n'
functions.cpp(5) : error C2065: 'n' : undeclared identifier
functions.cpp(5) : error C2143: syntax error : missing ';' before 'string'
functions.cpp(6) : error C2143: syntax error : missing ';' before 'return'
functions.cpp(15) : error C2001: newline in constant
functions.cpp(16) : error C2144: syntax error : 'int' should be preceded by ';'
functions.cpp(21) : error C3861: 'Add': identifier not found
then edited it to this
Edit: refined the code got less errors now and getting these errors
functions.cpp(22) : error C2143: syntax error : missing ';' before ':'
functions.cpp(22) : error C2143: syntax error : missing ';' before ':'
Also I'm using Visual C++ EE
Thanks too the people here for this nice C++ workshop.
[Edited by - Super Pancake on December 27, 2006 4:27:42 PM]
originally in the book it looked like this.
#include <iostream>int ADD (int first, int second){ std::cout << "In Add(), received " << first << " and " << second << "\n"; return (first + second);}int main(){ using std::cout; using std::cin; cout << "I'm in main()!\n; int a, b, c; cout << "enter two number: "; cin >> a; cin >> b; cout << "\nCalling Add()\n"; c=Add(a,b); cout << "\nBack in main().\n"; cout << "c was set to " << c; cout << "\nExiting...\n\n"; return 0;}
and got these errors
functions.cpp(4) : error C2001: newline in constant
functions.cpp(5) : error C2017: illegal escape sequence
functions.cpp(5) : error C2001: newline in constant
functions.cpp(5) : error C2146: syntax error : missing ';' before identifier 'n'
functions.cpp(5) : error C2065: 'n' : undeclared identifier
functions.cpp(5) : error C2143: syntax error : missing ';' before 'string'
functions.cpp(6) : error C2143: syntax error : missing ';' before 'return'
functions.cpp(15) : error C2001: newline in constant
functions.cpp(16) : error C2144: syntax error : 'int' should be preceded by ';'
functions.cpp(21) : error C3861: 'Add': identifier not found
then edited it to this
#include <iostream>int Add (int first, int second){ std::cout << "In Add(), received " << first << " and " << second << "\n"; return (first + second);}int main(){ using std::cout; using std::cin; cout << "Im in main()!\n"; int a, b, c; cout << "Enter two numbers: "; cin >> a; cin >> b; cout << "\nCalling Add()\n"; c=Add(a,b); cout << "\nBack in main().\n"; cout << "c was set to " << c: cout << "\nExiting...\n\n"; return 0;}
Edit: refined the code got less errors now and getting these errors
functions.cpp(22) : error C2143: syntax error : missing ';' before ':'
functions.cpp(22) : error C2143: syntax error : missing ';' before ':'
Also I'm using Visual C++ EE
Thanks too the people here for this nice C++ workshop.
[Edited by - Super Pancake on December 27, 2006 4:27:42 PM]
on this line:
Look at the very last character very carefully. Give up? That's a colon(:). You need a semi-colon(;). A dot with a comma, not two dots.
cout << "c was set to " << c:
Look at the very last character very carefully. Give up? That's a colon(:). You need a semi-colon(;). A dot with a comma, not two dots.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement