I work through Bjarne Stroustrup's "C ++ Programming Language" (second edition - I know I really need to get a new copy, but this is a library!), And you had a few questions about one of its simple questions, In Chapter 2, when it comes to declarations and constants, it lists a set of declarations, some of which are also definitions. In the exercise at the end of the chapter, he forces the reader to go back and rewrite the list, this time changing all the defined ads to just ads and changing all the undefined to have a definition.
I accomplished this task, hopefully mostly correctly, but there were a few parts that I was stuck with. I would appreciate if anyone could quickly look through my list, check if there is any of the original list that I did not distribute correctly, and then check my changes, including most specifically how to declare but not determine typedef, and if my definition is enumreporting is correct. Many thanks to everyone who helps. I apologize, as this is not a strictly direct code issue, i.e. There is no compiled code, it is more ... I do not know. But there is code in it, so I hope everything is in order.
// Original:
/*
char ch; // Definition.
int count = 1; // Definition.
char* name = "Njal"; // Definition.
struct complex { float re, im; }; // Definition.
complex cvar; // Definition.
extern complex sqrt(complex); // Declaration, NOT definition.
extern int error_number; // Declaration, NOT definition.
typedef complex point; // Definition.
float real(complex* p) { return p->re; } // Definition.
const double pi = 3.1415926535897932385; // Definition.
struct user; // Declaration, NOT definition.
template<class T> abs(T a) { return a < 0 ? -a : a; } // Definition.
enum beer { Carlsberg, Tuborg, Thor }; // Definition.
*/
// Definitions/Declarations switched:
/*
extern char ch;
extern int count;
extern char* name;
struct complex;
extern complex cvar;
complex sqrt(complex in) { // Yes, my maths may be wrong here. Doing the actual maths from memory.
complex out;
out.re = (in.re * in.re) - (in.im * in.im);
out.im = (in.re * in.im)*2;
return out;
}
int error_number;
// No idea how to declare but not define a typedef!
float real(complex* p);
extern const double pi;
struct user { string name; int age; char gender; }; // Lets assume we include <string>, and yes, using int for age *might* be a bit wasteful, but meh.
template<class T> abs(T a);
extern enum beer; // Not sure if this is right.
*/
source
share