| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
10 Aug 2006 08:50:43 PM |
| Object: |
Can't compile with call to function in global scope |
Can anyone explain why this file won't compile? The errors I'm getting
in VS.Net are below.
============================
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(24): error C3861: 'str':
identifier not found, even with argument-dependent lookup
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(23): error C2040: 'str' :
'int' differs in levels of indirection from 'std::string &'
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(23): error C2143: syntax error
: missing ';' before '.'
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(23): error C2501: 'str' :
missing storage-class or type specifiers
============================
std::string& string_instance()
{
static std::string msg("Hello");
return msg;
}
std::string & str = string_instance();
str.c_str(); //--------->comment this line and the file will compile
const char * ptr = str.c_str();
void foo()
{
std::string & str = string_instance();
str.c_str();
const char * ptr = str.c_str();
}
int main(int, char *)
{
return 0;
}
.
|
|
| User: "blytkerchan" |
|
| Title: Re: Can't compile with call to function in global scope |
10 Aug 2006 10:36:19 PM |
|
|
wrote:
Can anyone explain why this file won't compile? The errors I'm getting
in VS.Net are below.
============================
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(24): error C3861: 'str':
identifier not found, even with argument-dependent lookup
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(23): error C2040: 'str' :
'int' differs in levels of indirection from 'std::string &'
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(23): error C2143: syntax error
: missing ';' before '.'
c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(23): error C2501: 'str' :
missing storage-class or type specifiers
Your compiler is confused - ignore it if you want to find the problem
:)
============================
std::string& string_instance()
{
static std::string msg("Hello");
return msg;
}
std::string & str = string_instance();
str.c_str(); //--------->comment this line and the file will compile
If this code is verbatim, your problem is pretty simple: if you are
neither within the body of a function nor initializing a global
variable (neither of which would be true on this line if the code is
verbatim) you are calling a function in the middle of no-where. The
compiler is obviously confused about this - judging from the messages
it's shooting at you - but it doesn't make much sense to want to do
this anyway: the code would logically never be called as there is no
possible path from main() to the code in question (and you can
statically prove that). In the next line you do initialize a global
variable, so the code will be called with any other initialization
code, in undefined order but before main().
const char * ptr = str.c_str();
void foo()
{
std::string & str = string_instance();
str.c_str();
const char * ptr = str.c_str();
}
int main(int, char *)
{
return 0;
}
HTH,
rlc
.
|
|
|
|

|
Related Articles |
|
|