错误 "this declaration has no storage class or type specifier"

Error "this declaration has no storage class or type specifier"

我正在使用 Visual Studio 编写我的第一个应用程序,但我不明白它向我显示的错误。

有两个文件,Session 和 Login。 Login使用Session的set和get函数。正如您在下面看到的,Login 调用 "setCurrentLang",这是 Visual Studio 显示的消息:"this declaration has no storage class or type specifier" on Login.cpp。如果我编译,这就是错误:

"Error 26 error C2365: 'setCurrentLang' : redefinition; previous definition was 'function' (....)\GUI\Login.cpp".

这是 Session.cpp 文件:

#include "Session.h"
const char* CURRENT_LANG;
void setCurrentLang( char* lang){
    CURRENT_LANG = strdup(lang);
}
const char* getCurrentLang(){
    return CURRENT_LANG;
}

Session.h

#ifndef __SESSION_H__
#define __SESSION_H__

#include <cstring>
#include <stdio.h>

void setCurrentLang( char* lang);
const char* getCurrentLang();

#endif

Login.cpp

#include "Login.h"
#include "../data/Session.h"

setCurrentLang("English"); 

非常感谢您的帮助!

您在任何上下文之外调用该方法。这不可能。如果要在开始时设置语言,可以在 main 的开头调用它,或者使用在其构造函数中调用它的虚拟静态 class:

static class LanguageSetter
{
public:
    LanguageSetter()
    {
        setCurrentLang("English");
    }
} dummy;

或者直接在CURRENT_LANG的定义中设置默认值:

// std::string because this is C++, not C
std::string CURRENT_LANG = "English";