Error "this declaration has no storage class or type specifier"
You call the method outside of any context. This is not possible. If you want to set the language on start, you can either call it at the beginning of main
or use a dummy static class that calls it in its constructor:
static class LanguageSetter
{
public:
LanguageSetter()
{
setCurrentLang("English");
}
} dummy;
Or simply set the default value in the definition of CURRENT_LANG
:
// std::string because this is C++, not C
std::string CURRENT_LANG = "English";
Related videos on Youtube
Daniel 976034
Updated on June 04, 2022Comments
-
Daniel 976034 almost 2 years
I'm programing my first app with Visual Studio and I don't understand an Error that it shows me.
There're two files, Session and Login. Login uses the set and get functions of Session. As you can see bellow, Login calls "setCurrentLang" and this is the message Visual Studio shows: "this declaration has no storage class or type specifier" on Login.cpp. if I compile, this is the error then:
"Error 26 error C2365: 'setCurrentLang' : redefinition; previous definition was 'function' (....)\GUI\Login.cpp".
This is the Session.cpp file:
#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");
Thank you very much for your help!
-
Some programmer dude about 9 yearsPlease edit your question to include the complete and unedited error output. And also point out where you get the errors.
-
WhozCraig about 9 yearsPick a language.
-
Daniel 976034 about 9 yearsThanks @JoachimPileborg , I've edited the post.
-
Daniel 976034 about 9 years@WhozCraig I don't understand you. Can't I use C on C++?
-
StenSoft about 9 yearsYou can use C in C++ but C++ will kindly let you shoot your own leg on its own, and when you mix it with C, it will even give you a loaded shotgun with safety off. So expect very hard to debug problems, memory leaks and crashes because you would need to keep track of both your memory (because of C) and instances lifetime (because of C++ RAII) and make sure these two are in perfect sync.
-
-
Daniel 976034 about 9 yearsPutting the functionality aside, why can't I call the function on Login.cpp?? How could I call this function?
-
StenSoft about 9 yearsYou cannot call it because it has no context. To workaround it, read my answer.
-
Daniel 976034 about 9 yearsYes, I think I understood you. But I want to set a language on start and to use a way to manage the session from any file. For example, setting a new name, or a new email for the current user. Is this the best way to do it? How could I call a function of "dummy"? Thanks for your patience.
-
StenSoft about 9 yearsThe constructor is called automatically. Note that it is undefined in which order different files are initialized so this may not work as expected when used in multiple files. There are two ways to handle this, one is nifty counter and the other (and much preferred) is not to do this at all but to initialize all resources gradually as they are used.
-
youssef about 9 years@Daniel976034 the main aim of using
static class
, is that you can be sure that the object dummy is initialized for only one time which mean that yoursetCurrentLang("English")
well be called only one time even if you include its header in several files. -
StenSoft about 9 years@user3270926 If you would use it in a header file, it would have an instance in each cpp file it is included in and will be called that many times. If you want to have it in header file, you need to declare it
extern
and define it in one file. Usingstatic
here is to prevent linker error when you would use the namedummy
in some other file (static
is a modifier for the variabledummy
, not the class). -
Daniel 976034 about 9 yearsThanks @StenSoft So, the best practice would be to read again all the settings from file settings (I did it on this way), wouldn't it? I think I can pass the object by argument if I call a new frame.
-
StenSoft about 9 yearsFor you, the best way would be to have the default value for
CURRENT_LANG
specified in the definition and to usestd::string
when your code is C++:std::string CURRENT_LANG = "English";