C++编程语言,定义与声明练习(简单,但需要乏味的帮助 XD)
我一直在阅读 Bjarne Stroustrup 的《C++ 编程语言》(第二版 - 我知道我真的应该买一本新的副本,但这是一本图书馆的书!),并且对他的一个更简单的问题有一些疑问。在第二章中,当谈到声明和常量时,他列出了一组声明,其中一些也是定义。在本章末尾的练习中,他要求读者返回并重新编写列表,这次将所有已定义的声明更改为仅声明,并将所有未定义的声明更改为有定义。
我已经完成了这项任务,希望大部分都是正确的,但有一些部分我遇到了困难。如果有人可以快速浏览我的列表,检查是否有任何我错误分配的原始列表,然后检查我的更改 - 包括最具体的如何声明但不定义 typedef< /code>,如果我的
enum
声明而不是定义是正确的。非常感谢任何提供帮助的人。我很抱歉,因为这严格来说不是一个直接的代码问题 - 即这里没有可编译的代码,它更多......我不知道。但它确实有代码,所以我希望没关系。
// 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.
*/
I've been working through Bjarne Stroustrup's "The C++ Programming Language" (2nd edition - I know I should really get a new copy but this is a library book!), and had a few questions about one of his simpler questions. In Chapter 2, when talking about Declarations and Constants, he lists a set of declarations, some of which are definitions as well. In an exercise at the end of the chapter, he challenges the reader to go back and re-write the list, this time changing all of the defined declarations to just declarations and changing all of the non-defined ones to have a definition.
I have completed this task, hopefully mostly correctly, but there were a few parts I got stuck on. I would appreciate it if anyone could take a quick look through my list, check if there are any of the original list I mis-allocated, and then check my changes - including most specifically how to declare but not define a typedef
, and if my enum
declaration-not-definition is right. Thanks very much to anyone who helps. I apologise since this isn't strictly a direct code question - i.e. there's no compilable code here, it's more... I dunno. But it does have code in it, so I hope that's okay.
// 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.
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
typedef complex point;
不是 C++ 中的定义,而只是 C 中的定义。据我所知,您也不能在不更改 pi 的情况下为
pi
提供非定义声明意思是,因为它显示的定义具有内部链接,但如果您输入extern const double pi;
您将为它提供外部链接(可能与其他翻译单元的pi
名称冲突) 。请注意,链接规则很复杂。您也不能只声明枚举而不定义它。
我相信您的其他解决方案是正确的。
typedef complex point;
is not a definition in C++, but only in C.You also cannot, from what I know, provide a non-defining declaration for
pi
without changing its meaning, because the definition it shows has internal linkage, but if you putextern const double pi;
you will give it external linkage (possibly conflicting with other translation units'spi
names). Note that linkage rules are complicatedYou also cannot only declare an enumeration without defining it.
I believe your other solutions are correct.