小编Amr*_*rit的帖子

为什么using指令在全局范围和本地范围内表现不同?

当我编写以下代码时,它会被编译并正确执行:

#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)

它给出了这个编译错误: …

c++ namespaces

11
推荐指数
2
解决办法
449
查看次数

编写我自己的字符串类,但获取"引用'字符串'是不明确的"编译错误

我正在尝试编写自己的字符串类(用于理解目的).我写了如下,文件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)

file 2 string.cpp

#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)

c++ string namespaces

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×2

namespaces ×2

string ×1