简体   繁体   中英

Class as library of global not const variables in cli c++

How should defined class where are only global variables? I did something like that:

public ref class Klient
{
public:
    Klient(){}
    // zmienne
    static array<DWORD,2>^ klienty = gcnew array<DWORD,2>(40,2);
    static int i = 0;
    static DWORD pid;
    static HANDLE handle;
    static String^ nick;
    //funkcje
};

But if i include it more than 1 time it won't compile and showing redefinition of class error.

Did you guard your header? In Visual Studio, you should place this directive at the top of all header files:

#pragma once

This is equivalent to the classic C++ header guard:

#ifndef HEADER_SYMBOL_X
#define HEADER_SYMBOL_X

 // class declarations go here

#endif // HEADER_SYMBOL_X

If you don't guard your header, C++/CLI will indeed try to redefine your class on each include.

You'll have to be a little more clear, and paste the error you get. Also if you have a "ref" class the compiler generates a default constructor for you, so you don't need to write one.

This code worked for me, I was able to fetch the static int value into my WPF application:

#pragma once

#include "windows.h"

using namespace System;

namespace cppcli 
{
    public ref class Klient
    {
        public:
            static array<DWORD,2>^ klienty = gcnew array<DWORD,2>(40,2);
            static int i = 22;
            static DWORD pid;
            static HANDLE handle;
            static String^ nick;
    };
}

Update:
Noticed your comment, yes you need #pragma once in there. I assumed it was there since it's generated automatically by Visual Studio, well good to know that it works :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM