Can't compile with call to function in global scope

T

twelvetone

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;
}
 
B

blytkerchan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,061
Members
48,769
Latest member
Clifft

Latest Threads

Top