例如,以下函数用于检查工作簿是否已打开:
Function BookOpen(Bk As String) As Boolean
Dim T As Excel.Workbook
Err.Clear
On Error Resume Next
Set T = Application.Workbooks(Bk)
BookOpen = Not T Is Nothing
Err.Clear
On Error GoTo 0
End Function
Run Code Online (Sandbox Code Playgroud)
这两个Err.Clear陈述是否必要?
Lippman的Essential C++第4.7节就是这样做的.但我不知道为什么这段代码不能编译:
#include <iostream>
using namespace std;
class A
{
void f();
//other members...
};
class B
{
//other members...
friend void A::f();
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在A类中的void f()之前放置"public:"进行编译.那么李普曼错了吗?
ps Lippman的代码是这样的:
//...
class Triangular_iterator
{
//...
private:
void check_integrity() const;
//...
};
//...
class Triangular
{
//...
friend void Triangular_iterator::check_integrity();
//...
};
//...
Run Code Online (Sandbox Code Playgroud) 在file1.cc我写
int i = 0;
Run Code Online (Sandbox Code Playgroud)
而在file2.cc我写
#include <iostream>
int i = 1;
int main()
{
std::cout<< i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在MacOS中,编译器报告
duplicate symbol _i in:
/var/folders/wn/q9648wb507j9l504vp2d_dwm0000gn/T/file1-bb8eca.o
/var/folders/wn/q9648wb507j9l504vp2d_dwm0000gn/T/file2-b5e667.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
但是不是不同的文件有不同的范围,所以我们可以在file2中定义一个与file1同名的全局变量吗?此外,如果不同的文件在同一范围内,那么为什么将file2.cc转换为以下内容是非法的:
#include <iostream>
int main()
{
std::cout<< i <<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 此代码无法编译,错误信息是" 对A :: a'的未定义引用 ":
代码1:
#include <iostream>
using namespace std;
class A
{
public:
static const int a=0;
};
int main()
{
cout<<&A::a<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但对于非const静态成员,它编译:
代码2:
#include <iostream>
using namespace std;
class A
{
public:
static int a;
};
int A::a=0;
int main()
{
cout<<&A::a<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否无法访问类的静态const成员的地址?如果有,怎么样?为什么代码1不能编译?