tags:

views:

51

answers:

3

Why can't I compile/run this non-CLR program in VC++ 2008?

How to do it?

MyProgram.cpp

#include <iostream>

namespace System
{
    public class Console
    {
    public:
        static void WriteLine(char str[])
        {
            std::cout<<str;
        }
    };
}

int main()
{    
    System::Console::WriteLine("This a non-CLR program!");
}

Error

Error   1   error C3381: 
'System::Console' : assembly access specifiers are only 
available in code compiled with a /clr option   
e:\...\MyProgram.cpp    6
+4  A: 

That isn't a non-CLR C++ program. In proper C++, classes cannot be public (or private). You want:

namespace System
{
    class Console
    {
    public:
        static void WriteLine(char str[])
        {
            std::cout<<str;
        }
    };
}

Also in C++ character literals are const, so your function should be:

static void WriteLine( const char * str)

if you want to call it with one as a parameter.

anon
+5  A: 
namespace System
{
    public class Console // this line is causing the error
    .....

}

Classes in Standard C++ cannot be public(or private or protected) inside/outside a namespace.

Change public class Console to class Console

Prasoon Saurav
+4  A: 

Remove the public keyword. Link:

When applied to a managed type, such as class or struct, the public and private keywords indicate whether the class will be exposed via assembly metadata. public and private cannot be applied to unmanaged classes.

John