473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Aug 11 '06 #1
4 4565
std::string& string_instance()
{
static std::string msg("Hello");
return msg;
}
I think you return an reference of local variable is danger.

tw********@gmail.com 写道:
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;
}
Aug 11 '06 #2
tw********@gmail.com 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

Aug 11 '06 #3
Is********@gmail.com 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

Aug 11 '06 #4
Is********@gmail.com 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>
Aug 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
1
by: JH Programmer | last post by:
Hi all, I've got a problem when I want to create a global map //file operator.cpp #include <map> map<string, Staff*> Symbol_definition; Symbol_definition = NULL; Symbol_definition = NULL;...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
1
by: twelvetone | last post by:
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':...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
8
by: Sullivan WxPyQtKinter | last post by:
I am confused by the following program: def f(): print x x=12345 f() result is: 12345
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.