Problems declaring a map

E

Enric

I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;
}



When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s


What I'm doing wrong?
 
E

Enric

The error is that one (not complete in previous thread) :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > >' : is not a
class or namespace name
 
A

anon

Enric said:
I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;
}



When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s


What I'm doing wrong?

You do not know the prototype for the main function. Compiling your
code, I got:
/usr/lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

After fixing the main, the code compiled without problems.

The problem you are having has nothing to do with c++. Looks like you
are using windows - must be that you set
whatever_you_are_using_to_compile wrongly.
 
D

Dan Noland

I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;

}

When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s

What I'm doing wrong?

SystemC is not the same thing as C++. I don't know what their support
for the STL is like. Your code builds correctly under my version of g+
+.

YT,
Dan Noland
http://nolandda.org/
 
L

LR

anon said:
Enric said:
I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;
}



When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s


What I'm doing wrong?

You do not know the prototype for the main function.

Probably. But how do you know that. Maybe the name of that function is
sc_main. Would that be illegal?
> Compiling your
code, I got:
/usr/lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

After fixing the main, the code compiled without problems.

Are you sure that's not some linker error? And not a compilation error?
The problem you are having has nothing to do with c++. Looks like you
are using windows - must be that you set
whatever_you_are_using_to_compile wrongly.

I can't see why.

LR
 
J

Jim Langston

Enric said:
I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;
}



When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s


What I'm doing wrong?

Adding return 0; to sc_main and an int main() I can compile it without error
in MSVC++ .net 2003.

What compiler, platform and OS are you on? It sounds like it's something
platform specific (maybe compiler)
 
G

Guest

anon said:
Enric said:
I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;
}



When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s


What I'm doing wrong?

You do not know the prototype for the main function.

Probably. But how do you know that. Maybe the name of that function is
sc_main. Would that be illegal?

Having a function called sc_main is not illegal, but the code the OP
posted is illformed since it does not have a proper main function (a
function called main returning an int).
Are you sure that's not some linker error? And not a compilation error?

What he got was a linker error, yes. And after fixing that not only did
the code compile fine, it also linked correctly.
I can't see why.

Can not see what? That the OP is using Windows or that his/her compiler
is set up wrong? The latter is obvious from the fact that it does not
compile perfectly valid code.
 
O

Old Wolf

I've the following code:

#include <map>
#include <string>

using std::string;
using std::map;

int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;

}

When I try to compile it :

main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s

Your program does not have a line 55...

Can you show the actual code that you are trying to compile?
 
J

James Kanze

anon said:
Enric wrote:
I've the following code:
#include <map>
#include <string>
using std::string;
using std::map;
int sc_main(int ac, char *av[])
{
map<char,string> start;
map<char,string>::iterator si;
}
When I try to compile it :
main.cpp(55) : error C2653: 'map<char,class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >,struct std::less<char>,class s
What I'm doing wrong?
You do not know the prototype for the main function.
Probably. But how do you know that. Maybe the name of that
function is sc_main. Would that be illegal?
Having a function called sc_main is not illegal, but the code the OP
posted is illformed since it does not have a proper main function (a
function called main returning an int).

C++ supports separate compilation, and there's no requirement
that any one specific translation unit contain a main.

I compiled his code with three different compilers (g++, Sun CC
and VC++) and the only errors I got were that the function
didn't return a value. (A warning with g++; an error with both
Sun CC and VC++---although it shouldn't be an error according to
the standard.)

His code is legal, as long as the function is never called (in
which case, it has undefined behavior because it falls of the
end without returning a value). Either he's compiling it with
the wrong options or something, so the compiler isn't treating
it as C++, or he has a problem with his installation (so the
compiler reads a corrupt version of said:
Can not see what? That the OP is using Windows or that his/her
compiler is set up wrong? The latter is obvious from the fact
that it does not compile perfectly valid code.

I've never yet used a compiler which compiles all perfectly
valid code---even Comeau has some bugs hidden in it somewhere.
Two of the three compilers I normally have access to fail to
compile the code because of the missing return, for example.

But in this case, yes. His compiler is either installed
incorrectly or he's using it incorrectly. If I add the return,
I have no problems with any of my compilers.
 
J

Joel Yliluoma

I compiled his code with three different compilers (g++, Sun CC
and VC++) and the only errors I got were that the function
didn't return a value. (A warning with g++; an error with both
Sun CC and VC++---although it shouldn't be an error according to
the standard.)

His code is legal, as long as the function is never called (in
which case, it has undefined behavior because it falls of the
end without returning a value).

You said it shouldn't be an error according to the standard.

If the compiler can detect at compile-time that the code has
undefined behavior, does the standard require the compiler to
compile it regardless? Does "compiler outputs an error" not fall
into the domain of acceptable implementations of undefined behavior?
 
J

James Kanze

You said it shouldn't be an error according to the standard.
If the compiler can detect at compile-time that the code has
undefined behavior, does the standard require the compiler to
compile it regardless? Does "compiler outputs an error" not
fall into the domain of acceptable implementations of
undefined behavior?

Undefined behavior is, well, undefined. I'm pretty sure, in
fact, that it can have retroactive effects: data you wrote to
disk (and flushed) before the undefined behavior occurs may not
be there. And I'm pretty sure that the compiler is allowed to
refuse to compile the code. (It's always allowed to output an
error message, even for perfectly legal code.)

The problem here is that the undefined behavior only occurs *if*
the function is called. So the compiler must compile the code
unless it can prove that the code will in fact be executed, for
all possible input. When using separate compilation, it
generally means that a compiler error cannot occur until
linking, when the compiler has access to the entire program.

In the case of Sun CC, at least, it is very definitly an error
in the compiler; I have code something like:

MyClass
f( int i )
{
switch ( i ) {
case 0 :
// with a return at each case...

default :
programStatus.setError( ProgramStatus::fatal )
<< "Error message" ;
}
}

Sun CC fails to compile this (I think---I'm at home, and not on
my Sparc at the moment, but if it compiles this, then it fails
with something similar). In my framework, however, calling
ProgramStatus::setError with an error severity (the argument) of
fatal guarantees that you won't return. (In fact, the
destructor of the returned stream wrapper will ensure that you
don't go any further---either calling exit or abort, or raising
an exception.)

Now, given the complexities of the statement (dependent on the
argument and a user defined callback, code in a different
translation unit, etc.), I don't think it reasonable for the
compiler to detect that I can't possibly fall off the end of the
function. But if can't prove that I might; in fact, I can't.
So the compiler should accept the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top