当我编写以下代码时,它会被编译并正确执行:
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first; //using derective
using second::y;
cout << x << endl;
cout << y << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我使用main函数之外的指令编写如下,
using namespace first; //using derective
using second::y;
int main () {
cout << x << endl;
cout << y << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了这个编译错误: …
我正在尝试编写自己的字符串类(用于理解目的).我写了如下,文件1 string.h
#include<iostream>
#include<string.h>
namespace MyString
{
class string
{
char* ch;
int len;
public:
string();
string(const char* ch);
char* getString();
int length();
};
}
Run Code Online (Sandbox Code Playgroud)
#include<iostream>
#include"string.h"
using namespace std;
using MyString::string; // use string from MyString namespace only.
string::string()
{
ch = NULL;
len = 0;
}
string::string(const char* ch)
{
this->ch = ch;
}
char* string::getString()
{
return ch;
}
int string::length()
{
return len;
}
int main()
{
string obj = "This is a …Run Code Online (Sandbox Code Playgroud)