473,569 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::s tring, 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::s tring, 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 2593
Henrik S. Hansen <hs*@freecode.d k> wrote:
I'm new to C++, and cannot figure out why this won't compile:

std::map<std::s tring, 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::s tring, 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::s tring, 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 = myDummyFunction 001( argument1, argument2, argument3 );
int dummy002 = myDummyFunction 002( argument1, argument2, argument3 );
int dummy003 = myDummyFunction 003( 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
1246
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 example below compiles if OK is defined, but gives the following error otherwise:
4
14131
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 on the "With Me.OLEObject" line (By the way, I haven't found documentation which explains what the "With" clause is suppose to do?). I am trying...
388
21494
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in...
0
1406
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 defined a schema for details of a particular service request. Below is a schema similar to the one that I defined: <?xml version="1.0"...
1
1564
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 it is - I'm at work, laptoop is at home), MS Money (or something), MS Word 2002, a Powerpoint "reader" and possibly some other things. I vaguely...
9
5491
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 FrameWork.cpp I define some of the methods while I naturally leave the abstract methods undefined. Now I wrote a class FrameWork_GUI.h which inherits...
1
2798
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 incorporate the same logic within my simulation, the class that implements the functor logic has problems compiling. I get the following errors: --...
4
1360
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 info relating to a directory of taxis in the UK. Copy pasting is getting boring now, so i wanted to automate and simplify the procedure so i can ask...
2
3109
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 and does work. I and others have experienced that it should & doesn't work. I'll give you a little file that I tested with IE 6, FF 3, Opera &...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.