-3

I'm having a little problem with trying to define a label outside of the header file. I've started to try to learn C++ only three days ago, so I may have done a pretty obvious mistake here that I've not noticed. So far I've not managed to get this to work.

I wanna define the text in MIDCSrc.cpp, whilst the declaration of the label is in MIDC.h. So far, what I've done in MIDC.h is:

*MIDC.h*
#include "MIDCSrc.cpp"

*VS Auto-generated code*

private: System::Windows::Forms::Label^  label1;

*VS Auto-generated code*

private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e)
{
    textfunc2(label1);
}

And in MIDCSrc.cpp is:

#include "MIDC.h"

namespace MIDC
{
    void textfunc2(System::Windows::Forms::Label^ aLabel)
    {
    aLabel->Text = "Wow!";
    }
}

(I'm using Visual Studio 2017 Community)

The problem is, I can't even test it to see it fail miserably because it gives me a "Error C2084 function 'void MIDC::textfunc2(System::Windows::Forms::Label ^)' already has a body"

As I said, I'm sorry if this is a obvious mistake, but I can't figure this lovely error. If there is something (and I know there is) wrong with the code aside the body error, if possible, tell me. Gotta fix this.

Thanks to whoever replies :-)

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 6
    I'm not a c++/CLI wiz, but `#including` a cpp file in a header file generally is a recipe for disaster. Is this normal? –  Jul 06 '17 at 00:31
  • 3
    The `.h` is including the `.cpp` which is including the `.h` which is bad. As Frank says, don't include `.cpp` files inside headers unless you're completely sure what you're doing. – tadman Jul 06 '17 at 00:34
  • 1
    Especially without a header guard of some sort. Infinite include recursion. – user4581301 Jul 06 '17 at 00:35
  • Hum.. didn't think about that. Now, how could I go about the program recognizing textfunc2, wich is on the .cpp, on the .h, so that it could use it? (without including the .cpp) – vini2003 Jul 06 '17 at 00:39
  • *DECLARE* `textfunc2` and `label1_Click()` in the `.h` file, and *DEFINE* their bodies in the `.cpp` file, not the `.h` file. If `textfunc2()` is only used in this one `.cpp` file, and not in other `.cpp` files, then you don't even need the declaration for it in the `.h` file – Remy Lebeau Jul 06 '17 at 00:43
  • 1
    It sounds like you need to go back to basics. A program like this might be a bit too much to start off with. – Steve Jul 06 '17 at 00:48
  • As others said it would be good to include .h only in the cpp. You can also use #pragma once for safety – Suraj S Jul 06 '17 at 01:03

1 Answers1

0

Try the suggestion user4581301 has mentioned: A "header guard". This prevents recursive inclusion of files. It's quite easy:

#ifndef _MIDC_H
#define _MIDC_H

// here go the contents of MIDC.h

#endif

Do the same in your CPP file:

#ifndef _MIDCSRC_CPP
#define _MIDCSRC_CPP

// here goe the contents of MIDCSrc.cpp

#endif

The choice of the #define'd symbols is arbitrary. The names I chose here are just common practice. The trick is that the contents of the file are included only if this symbol isn't defined - which is the default case. The first step in the #ifndef body is to #define it, so the #ifndef condition is met only once.

SBS
  • 806
  • 5
  • 13