c++ - “命名空间”之前的预期嵌套名称说明符
问题描述
我有一个添加 2 个二进制字符串的基本程序。给定两个二进制字符串 a 和 b,将它们相加并返回结果字符串。我正在用 C++ 编译器(G++ v8.4.0)编译它
#include <iostream>
#include <string>
using namespace std;
std::string addBinaryStrings(std:: string a, std:: string b)
{
std::string result = "";
int s = 0;
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
s += ((i >= 0)? a[i] - '0': 0);
s += ((j >= 0)? b[j] - '0': 0);
result = char(s % 2 + '0') + result;
s /= 2;
i--; j--;
}
return result;
}
int main()
{
string a,b;
getline(cin, a);
getline(cin, b);
cout << addBinaryStrings(a, b) << endl;
return 0;
}
当我执行此操作时,出现以下错误:
main.cpp online 2:7: error: expected nested-name-specifier before ‘namespace’
using namespace std;
^~~~~~~~~
我哪里错了?
解决方案
这段代码在我的 C++ GNU 编译器上运行没有任何问题。所以你的编译器有问题。我建议您重置编译器的设置或使用其他编译器。您也可以使用在线 IDE。
推荐阅读
- python - 有没有办法将此shell代码移植到python?
- javascript - 如何从键分配为“*”的javascript对象中提取数据?
- java - Java:多线程安全问题:使用线程扩展和锁定
- javascript - 具有 Excel 功能的 HTML 表格到 Excel 电子表格
- javascript - WebRTC 获取 IP 不一致
- javascript - 等待 `insertAdjacentElement` 更新 DOM
- r - 在 r 中进一步设置子集值
- php - PHP 向上取整值
- python - 如何在 Django REST Framework 中验证完整的模型数据
- javascript - 如何将 reactjs 中的常量和 useEffect 从函数更改为渲染组件