Search code examples
c++winformsc++-cli

Communicating C++ header file and source files with Windows Form


I am building a windows form app where I need this youtube header and source file (for context) to communicate with a button in a windows form, I am fairly new to this as I just started this a few weeks ago, I already have this cpp file for my button;

//i made this separate file because the windows form header file is flooded with system generated codes already
ButtonHandler.cpp

#include "pch.h" 
#include "Main.h"

void MovieRentalSystem::Main::btn_clientAddReg_Click(System::Object^ sender, System::EventArgs^ e) {
     //I need to call the linked list into here
     DataList::DataList() { //(1) This is the first error I got
           f_Data = NULL;
           currentData = NULL;
           temp = NULL;
     }
}

So I've been following the tutorials mentioned, this is the header file I made

LinkedList.h
#pragma once

ref class DataList {
private:
    typedef struct fork { //(2) this is the second error I got
        int data;
        fork* nextData;
    }* dataPointer;
    dataPointer f_Data;
    dataPointer currentData;
    dataPointer temp;
public:
    DataList();
    void addFork(int addFork);
    void delFork(int delFork);
    void p_list();
};

Errors:

(1) DataList::DataList() { is expecting a ;

(2) struct is throwing "a standard class cannot be nested in a managed class" just by adding a ref because of Tomashu's answer here.

Maybe, I'm just being stoopid for trying them to communicate or is there a better way in creating a LinkedList or List using System::Collections::Generic;?


Solution

  • First include the header file properly and you are missing a semicolon:

    #include "LinkedList.h"
    
    void MovieRentalSystem::Main::btn_clientAddReg_Click(System::Object^ sender, System::EventArgs^ e) {
         //I need to call the linked list into here
         // the call is also wrong, you want to create a variable here I guess
         auto datalist = gcnew DataList() {
               f_Data = NULL;
               currentData = NULL;
               temp = NULL;
         }; // <-- is missing
    }
    

    The second answer is straight-forward as it tells you that a standard class cannot be part of your managed class. You only can hold pointers (and native types) in managed classes:

    LinkedList.h
    #pragma once
    
    
    typedef struct fork { //(2) this is the second error I got
        int data;
        fork* nextData;
    } dataFork;
    
    ref class DataList {
    private:
        dataFork* f_Data;
        dataFork* currentData;
        dataFork* temp;
    public:
        DataList();
        void addFork(int addFork);
        void delFork(int delFork);
        void p_list();
    };