473,385 Members | 1,356 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,385 software developers and data experts.

ISO C++ forbids declaration of "tst" with no type

I'm new to C++, and cannot figure out why this won't compile:

std::map<std::string, int> tst;
tst["a"] = 1;

int main() { /*...*/ }

It gives me:
error: ISO C++ forbids declaration of `tst' with no type

However, if I do:

int main() {
std::map<std::string, int> tst;
tst["a"] = 1;
}

It works. Why is that? I cannot find the (reasonable, I suspect)
explanation in Stroustup's TC++PL.

--
Henrik S. Hansen
Jul 22 '05 #1
2 2580
Henrik S. Hansen <hs*@freecode.dk> wrote:
I'm new to C++, and cannot figure out why this won't compile:

std::map<std::string, int> tst;
tst["a"] = 1;

int main() { /*...*/ }


You can't have an assignment outside of a function.
This would produce a similar error:

int foo;
foo = 1;

int main() { /*...*/ }

The compiler thinks that it's a declaration, with an initial value:

int foo = 1;

but the type (int, in this case) is missing.

--
Jack
Jul 22 '05 #2
* hs*@freecode.dk (Henrik S. Hansen) schriebt:
I'm new to C++, and cannot figure out why this won't compile:

std::map<std::string, int> tst;
tst["a"] = 1;

int main() { /*...*/ }

It gives me:
error: ISO C++ forbids declaration of `tst' with no type

However, if I do:

int main() {
std::map<std::string, int> tst;
tst["a"] = 1;
}

It works. Why is that? I cannot find the (reasonable, I suspect)
explanation in Stroustup's TC++PL.


I don't know whether it's reasonable or not -- at first sight it seems
very reasonable, then as one thinks about it it seems less reasonable.

The basics: in C++ you cannot put executable statements directly at
namespace scope, that is, outside a function body.

So you have to put them in e.g. 'main'.

That seems reasonable; after all, in what order should statements at
namespace scope be executed in a multi-file program?

But then, you can achieve nearly the same effect simply by declaring dummy
variables and initialize them by function calls, e.g.
int dummy001 = myDummyFunction001( argument1, argument2, argument3 );
int dummy002 = myDummyFunction002( argument1, argument2, argument3 );
int dummy003 = myDummyFunction003( argument1, argument2, argument3 );
Now in what order should those functions be called?

Within a compilation unit this is well-defined: it's the order of declaration.

Across compilation units it's messy, messy, messy. You're not even guaranteed
that they are actually called, unless some other part of the program calls
some function in the relevant compilation unit. And if one of those functions
throws an exception you're not even guaranteed that 'main' will be called.

At that point in the reasoning it begins to seem reasonable again to have all
calls, all that actually is done, originating in 'main'.

But then one can reason further and think about e.g. initialization of the
standard i/o-streams.

And then one discovers that hey, here's something lacking in the language:
those streams are initialized (at namespace scope) by some mechanism that _is
not_ available to you, the C++ "end user".

So there's something not quite solved here. It's much like reasoning about
relativity theory. As a child you find it obviously wrong, then as a teenager
obviously correct, then maturing a bit you find some less than well-defined
parts, then those parts, after a while, again seem reasonable, and so on, but
the upshot is that there is a little gray terra incognita where there is still
something to be discovered, and all the difficulties emanate from that area.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

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

Similar topics

3
by: CHRISTOF WARLICH | last post by:
Hi, the following few lines of code are showing a quite strange and unexpected behaviour of namespaces that makes me worry wheater I should rely on namespaces in the future at all. The...
4
by: Otis Hunter | last post by:
I have been fighting with this for days and your expert help is needed! Below is the code I am executing which results with "Object doesn't support this property or method". The error is occuring...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
0
by: Matt | last post by:
I have a problem when I select node elements from an xml file and validata each node againts the schema. I use XmlValidatingReader and it complains about elements not being declared. I have...
1
by: Tim Marshall | last post by:
By "lite" I mean some of the things that are factory installed on PCs, in my case a Dell laptop - they include such things as MS Works (which includes some kind of database software - forget what...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
4
by: sidesteal | last post by:
PLEASE forgive me! I have been a web designer for a few years now, and want to move on and learn a programming language. Im currently building a website, and i have a js file that contains some...
2
by: ChrisLA | last post by:
Hi; I've seen lots of discussion & disagreement on this issue, so any good explanation would be appreciated. Some people seem to think that "document.GetElementByID("MyName").submit(); should...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.