首页 > 解决方案 > “命名空间”之前的预期嵌套名称说明符

问题描述

我有一个添加 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++c++11

解决方案


这段代码在我的 C++ GNU 编译器上运行没有任何问题。所以你的编译器有问题。我建议您重置编译器的设置或使用其他编译器。您也可以使用在线 IDE。


推荐阅读