Advertisement

C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by June 01, 2006 11:40 AM
182 comments, last by Dbproguy 16 years, 9 months ago
Quote:
Original post by kaydash
Error 1 error C3861: 'test': identifier not found \projects\ham\ham\test1.cpp 17
Error 2 error C2365: 'test' : redefinition; previous definition was 'formerly unknown identifier' \projects\ham\ham\test1.cpp 22


All symbols must be declared before (as the compiler parses the source) they are used. Note that a definition doubles as a declaration.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Now here's a small lesson on how to interpret error messages.

The compiler is telling you that, on line 17, it doesn't know what 'test' is, so it doesn't know what to do with it. Then, on line 22, it tells you that you are changing the meaning of 'test'.

To me, that reveals something about the internals of the compiler: on line 17, when you try to use something you haven't declared, the compiler emits a dummy definition for that symbol as an "unknown identifier". Then it takes advantage of the system that prohibits redefinitions to complain that what was before an "unknown identifier" is now being redefined as a function.

In C++, each and every symbol must have one and only one definition. That is called the "One Definition Rule".

Taken together, the two error messages tell you that you are using 'test' before it was declared, and point out where its definition is. What you have to do at that point is to either add a forward declaration that agrees with the definition, or move the definition to before the point where you used 'test'.

And no, the compiler does not backfill the previously unknown symbols with the new definition: such behaviour is not part of the language (unlike, say, C#).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Wow, that worked!Just cut and paste the method above the main method...

Here:
#include <iostream>

void test()
{
std::cout << "Dang!" << std::endl;

}

int main ()
{
using namespace std;

int x=5;
int y=7;

std::cout << "Sum Of X & Y: " << (x+y)<< std::endl << "Product Of X & Y: " <<(x*y)<< std::endl;
std::cout << "Works Like A Charm!" << std::endl;
std::cout << "Division Of X & Y: " << (float)x/y << std::endl;
std::cout << "Product Double: " <<(double)7000*7000 << std::endl;
test();
}
I also learned there's no need to define a prototype unlike C-language.
I Have Awoken.
Quote:
Original post by kaydash
Wow, that worked!Just cut and paste the method above the main method...

Here:
*snip*

I also learned there's no need to define a prototype unlike C-language.


You can still use a prototype and you will need them if you start getting several functions and you get some circular dependencies. The code looks like this:

#include <iostream>void test();int main () {   using namespace std;  int x=5;  int y=7;  std::cout << "Sum Of X & Y: " << (x+y)<< std::endl << "Product Of X & Y: " <<(x*y)<< std::endl;  std::cout << "Works Like A Charm!" << std::endl;  std::cout << "Division Of X & Y: " << (float)x/y << std::endl;  std::cout << "Product Double: " <<(double)7000*7000 << std::endl;  test(); }void test(){  std::cout << "Dang!" << std::endl;} 
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Hello guys,

I finally started reading, but don't worry, I'll catch up soon :)

I'm a bit confused by the linking proces. On page 6 it says: "The compiler then invokes a linker which combines the object file into an executable program"

So here's my question: With what is the object file combined, why does it have to be combined with this thing, what are the consequences and maybe (if it is not too complex for beginners) how is it done?

thanks,
TH
since all already going to the chapter 4, i'm still stuck with my IDE....:D

just wondering...

How i can start without Debug in my IDE?? currently i'm using DEV-C++
Advertisement
Have you tried running your .exe from the "command prompt"? (or how is it called in English?)


I have another question: in listing 2.2 page 29:

std::cout << (float) 5/8 << st::endl;

Is this "casting"?
Quote:
Original post by twoaterisn
Have you tried running your .exe from the "command prompt"? (or how is it called in English?)


Yes...and it's work...the DOS is stay...but is it i need to run it on command prompt each time i have compiled my program??
Quote:
Original post by twoaterisn
I have another question: in listing 2.2 page 29:

std::cout << (float) 5/8 << std::endl;

Is this "casting"?


I am not a tutor, but yes that is indeed casting. It is meant to convert the 5 into a float, because in c++ integer division will cut off the values. With 'real' values 5 / 8 is 0.625, but c++ would yield the result of 0. The only way to get the 'correct' answer is for one of the operands(the 5 or 8) to become a float and then c++ will do the proper arithmetic.

I put correct in quotes above because there are times when we desire the behavior of cutting the number off. Say you want to convert minutes into hours. Specifically say we have 267 minutes, how do we find the hours? In c++ we'd do it like so:

int minutes = 267;int hours = minutes / 60;


This would yield the correct value. That is, there are 4 hours in 267 minutes. Had you casted minutes to a float and hours was also a float, like this:

int minutes = 267;float hours = (float)minutes / 60;


Then hours would hold 4.45. Pretty much no one would ever say "it's only four point forty-five hours until the plane lands". So what if you wanted the minute portion remaining? Well, the key word is 'remain' and there is another operator whose result is the remainder, the % (mod) operator. Once again our code becomes
int minuts = 267;int hours = minutes / 60;int minutes_remaining = minutes % 60;

This yields the correct value of 4 for hours, and 27 for minutes. So integer division works like those word problems you may have been asked where the teacher says "You have eleven dollars and you want to buy as many candy bars as you can afford Each candy bar costs two dollars. How many candy bards can you buy?" And of course the answer is five. Then if the teacher asks "how much money is remaining?" that is like mod which gives you one dollar left.

There are others ways to cast. One related form of casting would be:

std::cout << float(5) / 8 << std::endl;


Which acts as a sort of 'function' casting. In c++ there are new, additional ways to cast that are meant to help clarify the intent of the casting and add safety so you don't 'shoot yourself in the foot' metaphorically speaking. For your information the c++ casts are:

dynamic_cast <new_type> (expression)reinterpret_cast <new_type> (expression)static_cast <new_type> (expression)const_cast <new_type> (expression)


reinterpret_cast and dynamic_cast would be very hard to explain until you encounter pointers and derived classes. Basically, they help the compiler tell you when you're doing something ill advised. const_cast is something that is only used in extreme cases. Personally I have never used it. static_cast, however, can be used like the C-style casts. The first example would become:

std::cout << static_cast<float>(5) / 8 << std::endl;


[advanced]These links assume you have knowledge of classes, derived classes, and pointers so if you haven't reached those parts of the book you should stay away from them until you do. Here and here.[/advanced]

I believe this is all factual information, but I'll keep checking back so if I'm wrong on anything let me know and I'll edit the offending section. 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!

Quote:
nobodynews
I believe this is all factual information, but I'll keep checking back so if I'm wrong on anything let me know and I'll edit the offending section. Hope this helps.


Nope. It all looks good to me. The book doesn't do a very good job of explaining casting. So further information provided by the tutors and helpful readers is appreciated. Thank you for not going into dynamic or reinterpet cast just yet. We'll get there in the weeks to come.

Thanks for your post!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement