Why can't I compile this program other than CLR in VC ++ 2008? How to do it?

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
+3
source share
3 answers

This is not a CLR C ++ program. In proper C ++, classes cannot be public (or private). Do you want to:

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 parameter.

+11
source

Delete public keyword. Link :

, , , . public private .

+4
namespace System
{
    public class Console // this line is causing the error
    .....

}

Classes in standard C ++ cannot be publicly available (either private or protected) inside / outside the namespace.

Change public class Consoletoclass Console

+1
source

Source: https://habr.com/ru/post/1758512/


All Articles