| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
10 Aug 2006 08:52:01 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 2003 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: "Thomas Matthews" |
|
| Title: Re: Can't compile with call to function in global scope |
10 Aug 2006 09:10:22 PM |
|
|
wrote:
Can anyone explain why this file won't compile? The errors I'm getting
in VS.Net 2003 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;
}
When I compiled your example, verbatim, with g++ (GCC) 3.3.1 (cygming
special), I get the following errors:
$ g++ -o junk.exe junk.cpp
junk.cpp:1: error: syntax error before `&' token
junk.cpp:7: error: syntax error before `&' token
junk.cpp:8: error: syntax error before `.' token
junk.cpp:9: error: `str' was not declared in this scope
junk.cpp: In function `void foo()':
junk.cpp:13: error: `string' undeclared in namespace `std'
junk.cpp:13: error: `str' undeclared (first use this function)
junk.cpp:13: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
junk.cpp:13: error: `string_instance' undeclared (first use this function)
I added "#include <string>" and get the following error:
junk.cpp:10: error: syntax error before `.' token
Line 10 is the line with "str.c_str();" on it.
My understanding (and I will get corrected if I wrong), is that
the line "str.c_str();" is a statement and must be enclosed
within a function. In other words, this would be the equivalent
of:
#include <iostream>
std::cout << "This is not valid." << endl;
int main(void)
{
return 0;
}
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
.
|
|
|
|
| User: "" |
|
| Title: Re: Can't compile with call to function in global scope |
10 Aug 2006 09:06:06 PM |
|
|
std::string& string_instance()
{
static std::string msg("Hello");
return msg;
}
I think you return an reference of local variable is danger.
twelvetone@gmail.com =E5=86=99=E9=81=93=EF=BC=9A
Can anyone explain why this file won't compile? The errors I'm getting
in VS.Net 2003 are below.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
std::string& string_instance()
{
static std::string msg("Hello");
return msg;
}
std::string & str =3D string_instance();
str.c_str(); //--------->comment this line and the file will compile
const char * ptr =3D str.c_str();
void foo()
{
std::string & str =3D string_instance();
str.c_str();
const char * ptr =3D str.c_str();
}
=20
int main(int, char *)
{
return 0;
}
.
|
|
|
| User: "Default User" |
|
| Title: Re: Can't compile with call to function in global scope |
11 Aug 2006 11:31:06 AM |
|
|
wrote:
std::string& string_instance()
Please read the information below.
Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
.
|
|
|
|
| User: "blytkerchan" |
|
| Title: Re: Can't compile with call to function in global scope |
10 Aug 2006 10:30:56 PM |
|
|
wrote:
std::string& string_instance()
{
static std::string msg("Hello");
return msg;
}
I think you return an reference of local variable is danger.
Not if it's static: only a single instance of the variable will exist
and it will only exist as of the first time the function was called.
There's no real danger here unless your threading and your compiler
doesn't know about it (but that would be off-topic here anyway)
rlc
.
|
|
|
|
|

|
Related Articles |
|
|