看,我把我的命名空间和函数放在了main这之前并成功编译:
#include <iostream>
#include <exception>
#include <string>
using namespace std;
//==================================================
namespace Bushman{
void to_lower(char* s)
// Replace all chars to lower case.
{
const int delta = 'a' - 'A';
while(*s){
if(*s >= 'a' && *s <= 'z') *s -= delta;
++s;
}
}
}
//==================================================
int main()
// entry point
try{
namespace B = Bushman; // namespace alias
void B::to_lower(char* s);
char* str = "HELLO, WORLD!";
B::to_lower(str);
printf("%s\n",str);
}
catch(exception& e){
cerr << e.what() << endl;
return 1;
}
catch(...){
cerr << "Unknown exception." << endl;
return 2;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我把我的命名空间和函数放在之后main,那我就无法编译:
#include <iostream>
#include <exception>
#include <string>
using namespace std;
//==================================================
int main()
// entry point
try{
namespace B = Bushman; // namespace alias
void B::to_lower(char* s);
char* str = "HELLO, WORLD!";
B::to_lower(str);
printf("%s\n",str);
}
catch(exception& e){
cerr << e.what() << endl;
return 1;
}
catch(...){
cerr << "Unknown exception." << endl;
return 2;
}
//==================================================
namespace Bushman{
void to_lower(char* s)
// Replace all chars to lower case.
{
const int delta = 'a' - 'A';
while(*s){
if(*s >= 'a' && *s <= 'z') *s -= delta;
++s;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何正确指定我的命名空间的声明和第二种情况下的函数?
您可以在其命名空间内转发声明函数,然后执行以下操作main:
namespace Bushman
{
void to_lower(char* s);
}
int main()
{
// as before
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |