[英]Expected nested-name-specifier before ‘namespace’
我有一個添加 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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.