# C++ Build Error: expected unqualified-id before "public"



## deleted122510

Hey,

Before I tell you my error I should probably give you some of my compiler information, etc :

Program: Code::Blocks
Compiler: GNU GCC Compiler (I believe this is the default one for Code::Blocks)

Also, I've NEVER made a program using any command-line language before, even C++ so this could be a simple fix, I dunno . . .

Here's my code:




Code:


namespace WMISample
{
        public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2\\Applications\\Games",
                    "SELECT * FROM Game");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Game instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("GameID: {0}", queryObj["GameID"]);
                    Console.WriteLine("GameInstallPath: {0}", queryObj["GameInstallPath"]);
                    Console.WriteLine("GDFBinaryPath: {0}", queryObj["GDFBinaryPath"]);
                    Console.WriteLine("InstallScope: {0}", queryObj["InstallScope"]);
                    Console.WriteLine("InstanceID: {0}", queryObj["InstanceID"]);
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                    Console.WriteLine("ResourceIDForGDFInfo: {0}", queryObj["ResourceIDForGDFInfo"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

Error:



Code:


C:\Users\Undocked Windy\Desktop\Untitled1.c|4|error: expected unqualified-id before "public"|
||=== Build finished: 1 errors, 0 warnings ===|

I clicked on FILE > NEW > EMPTY FILE to make the C++ program, is this right? Am I suppose to add some sort of CPP file or H file? I'm VERY unfamiliar with C++, VERRRRY!

Thank you.


----------



## deleted122510

Just for the sake of argument, here's a screengrab:

http://img91.imageshack.us/img91/5537/newbitmapimagepip.jpg


----------



## Angelfire777

I'm by no means a good programmer but .. the code you posted looks like C# code rather than a C++ one. :grin:


----------



## deleted122510

I got it from a website . . .

wait, why can't a c++ compiler compile C#?

How can I compile it . . . ?


----------



## Angelfire777

C++ and C# are two VERY different languages. 

C++ compilers can't compile C# code and C# compilers can't compile C++ code. C++ code compiles to machine code while C# compiles to byte code. 

In order to compile C# code, you would need something like this:

Visual C# Express Edition
http://www.microsoft.com/express/download/


----------



## deleted122510

Ahhh, OK. Thanks. Will I get the same error in that compiler?


----------



## Angelfire777

Nope, it should work fine in Visual C#. 

If I remember right, you will need to add a reference to System.Management in order for it to recognize some of the types there.


----------



## deleted122510

I converted this code to VB.NET and added that reference and still got the same here - so, I'll try Visual C#

THANKS.


----------



## deleted122510

oh also, will dev++ compile C?


----------



## Angelfire777

While VB.net and C# are similar in a lot of ways, that code shouldn't compile mainly because of the syntax. C# has similar syntax with C++.



> oh also, will dev++ compile C?


I use Visual C++ for my projects and it compiles C. I'm not sure if devc++ would but some C++ compilers do support C too.


----------



## shuuhen

Angelfire777 said:


> I'm not sure if devc++ would but some C++ compilers do support C too.


It should. C++ is _mostly_ a superset of C, so a very large amount of C code will compile as C++. There are some quirks, but I've met people that hardly seem to notice (although, there are others that know them quite well). Any C++ compiler should be able to compile a large amount of C++.

FYI, another free IDE for C# is SharpDevelop. I avoid .NET, so I haven't looked at it in years, but it seemed to be fairly good. As far as syntax, I'd actually say C# is closer to Java than C++ (although they do all have similarities).


----------



## deleted122510

SO, Visual C# < Sharpdevelop?


----------



## Kathy Lee

Dears,

I guess you are good at programming languages. 
Do you have any idea about *Microsoft Visual C++ Library*?
I came across this runtime error when using a desktop program.
It says the application has requested the Runtime to terminate it in an unusual way.What is it?


----------

