Can't compile with call to function in global scope



 DEVELOP > c-Plus-Plus > Can't compile with call to function in global scope

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
Can't compile with call to function in global scope
illegal call of non-static member compile error question
compile error trying to call STL sort
Call constructor on an already existing instance?
Re: call destructor (confused newbie)
Picking const vs. non-const member functions upon a call
Memory segment error in dlopen call
code portability and function call serialisation.
LASER Summer School on Software Engineering - call for participation
PN: Sample code to call another program/command from inside a C++ program
We need some help installing IP based CRM software.. IP based call center software
Aborting CALL to stored procedure
Override/redirect a function call from a dll
g++296: no matching function for call to istrstream::str()
Call for Papers - PhD Symposium on eXtreme Programming and AgileProcesses, June 18-23, 2005 Sheffield University, UK
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER