【问题标题】:error: expected ', ' or ' ;' before 'namespace'错误:预期 '、' 或 ' ;'在“命名空间”之前
【发布时间】:2016-02-28 08:34:18
【问题描述】:

我对 C++ 很陌生,我不知道为什么,但是每当我尝试运行我的程序时,我都会收到以下错误:

error: expected ', ' or ' ;' before 'namespace'

我很确定下面文件中的 using namespace std; 行会导致问题,但我不知道如何解决它。

Main.cpp

#include <iostream>
#include <windows.h>
#include "Helper.h"
#include "KeyConstants.h"
#include "Base64.h"

using namespace std;

int main()
{
    MSG Msg;

    while(GetMessage (&Msg, NULL, 0, 0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return 0;
}

它还会打开一个名为stl_construct.h 的文件,并告诉我错误来自该文件中的namespace std _GLIBCXX_VISIBILITY(default)。我很确定这不是问题,但以防万一我会添加一些。

摘自stl_construct.h

#ifndef _STL_CONSTRUCT_H
#define _STL_CONSTRUCT_H 1

#include <new>
#include <bits/move.h>
#include <ext/alloc_traits.h>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

/**
* Constructs an object in existing memory by invoking an allocated
* object's constructor with an initializer.
*/
#if __cplusplus >= 201103L
  template<typename _T1, typename... _Args>
    inline void
    _Construct(_T1* __p, _Args&&... __args)
    { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
#else
  template<typename _T1, typename _T2>
    inline void
    _Construct(_T1* __p, const _T2& __value)
    {
      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 402. wrong new expression in [some_]allocator::construct
      ::new(static_cast<void*>(__p)) _T1(__value);
    }
#endif

Base64.h

#ifndef BASE64_H
#define BASE64_H

#include <vector>
#include <string>

namespace Base64
{
    std::string base64_encode(const std::string &);

    const std::string &SALT1 = "LM::&&jARLZ_E5?u ;f9+,4AZwA8MF3t(t+T+   {o!g,Ze22Pu&6$GbROk-* LzIxb?d'";
    const std::string &SALT2 = "'hytO|-h0,Rb@6Z{iH=H=+Q:E{+Y:&<rzP!^;oIC!.OGk5o6)^S^1-o,UcLt(`kQx'?";
    const std::string &SALT3 = "FU%^L,RHo({KD~[iZ/7Y%EehTkaE6^jwYQXwR#5Qh|c?)m?CGC(j-&oG~laZclg?Q'!";


    std::string EncryptB64(std::string s)
    {
        s = SALT1 + s + SALT3 + SALT2;
        s = base64_encode(s);
        s.insert(15, SALT1);
        s += SALT3;
        s = base64_encode(s);
        s = SALT3 + SALT1 + SALT2;
        s = base64_encode(s);
        s.insert(7, "t");
        s.insert(1, SALT3);
        return s;
    }

    const std::string &BASE64_CODES = "ABCDEGFHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    std::string base64_encode(const std::string &s)
    {
        std::string ret;
        int val = 0;
        int bits = -6;
        const unsigned int b63 = 0x3F;

    for(const auto &c : s)
    {
        val = (val << 8) + c;
        bits += 8;
        while(bits >= 0)
        {
            ret.push_back(BASE64_CODES[(val >> bits) & b63]);
            bits -= 6;
        }
    }

    if(bits > -6)
        ret.push_back(BASE64_CODES[((val << 8) >> (bits + 8)) & b63]);

    while(ret.size()%4)
        ret.push_back('=');

    return ret;
    }
}

#endif // BASE_64

【问题讨论】:

  • 其他文件(Helper.hKeyConstants.hBase64.h)的内容是什么?
  • 错误可能在头文件中
  • 我预测Base64.h 的末尾有问题 - 可能缺少分号。
  • 尝试自己编译头文件。即用 .cpp 扩展名重命名它们并编译它们,
  • 没关系,我忘了在 KeyConstants.h 的末尾添加分号

标签: c++ syntax compiler-errors syntax-error


【解决方案1】:

检查“KeyConstants.h”文件。不要忘记“;”在地图的尽头。 (具有键列表的那个)

【讨论】:

    【解决方案2】:

    你少了一个分号
    可能在Base64.hext/alloc_traits.h 或其他.h 文件之一(如KeyConstants.h)的末尾

    【讨论】:

    • 很好的答案说明了显而易见的
    • 我添加了“Base64.h”以便您查看
    • 你们为什么投反对票,伙计们?这样的问题,这样的答案。
    • 应该是评论而不是答案
    • 两者都不应该。解决后应删除此类简单问题。该错误只是语法,因此这些不太可能在将来帮助某人,这是删除的原因。即便如此,简单的答案也不属于 cmets,因为 cmets 不是提供答案的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多