Hi,

I am a little bit troubled.
I want to call a function in a separate file on a windows form.
Here is my header file:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {				 
				 String ^OriginalText;
				 String ^ConvertedText;

				 OriginalText = this->textBox1->Text;

				 ConvertedText = ParseBBCode(OriginalText);

				 this->textBox2->Text = ConvertedText;
				 }

This means that when button1 is clicked, two variables are declared. One of them is set to the value in textBox2. Then, I execute the function ParseBBCode(/*string parameter*/) and assigned the returned string to textBox2.

Here is my .cpp file:

String ^ParseBBCode(String ^OriginalText) {
String ^ParsedBBCode
//manipulate OriginalText and assign to ParsedBBcode
return ParsedBBCode;
}

I know I'm missing something, but I don't know what. What do I need to include the function into the header? I have #include <string> in the .cpp file. I can't use #include "xxx.cpp" because it generates about 80 errors. The once I'm stuck with right now is just this one:

error C3861: 'ParseBBCode': identifier not found

Thanks!

Create a new header file named "xxx.h". Put String ^ParseBBCode(String ^OriginalText); in the new header file. Include it in the file where you call ParseBBCode.

Create a new header file named "xxx.h". Put String ^ParseBBCode(String ^OriginalText); in the new header file. Include it in the file where you call ParseBBCode.

It doesn't seem to work... Which file do I include it in? The .h or the .cpp? Do I need to add an #include <string> at the beginning? Do I need to put that code within a function?

Put it in a file named something like "ParseBBCode.h". Include that header in your Form1.h file (or put it in stdafx.h, but that's not necessary). You do not need to include <string>, the String ^ in this case is a System::String^ from .NET.

You will either need to put a using namespace System; at the top of your header (not a good idea, as everything that includes it will now have that namespace) or qualify your String^s in the header as such:

System::String ^ ParseBBCode(System::String^ OriginalText);

Put it in a file named something like "ParseBBCode.h". Include that header in your Form1.h file (or put it in stdafx.h, but that's not necessary). You do not need to include <string>, the String ^ in this case is a System::String^ from .NET.

You will either need to put a using namespace System; at the top of your header (not a good idea, as everything that includes it will now have that namespace) or qualify your String^s in the header as such:

System::String ^ ParseBBCode(System::String^ OriginalText);

Should I move the function from my .cpp to the new .h file? I don't see how my function in the .cpp will be called.

From what I understand, my Form1.h is handling the button1 click. When it calls the function, it is in the included .h file... but what do I do with the actual function in the .cpp?

You're calling the function in Form1.h, correct? As long as your .cpp file is part of the project, everything should be fine. No, do not move the body of the function to the header.

It's confusing because in the Winforms programs, the Form1.h also contains a lot of definitions. If you have included the one line header in Form1.h, are calling the function in Form1.h, and have the associated .cpp file with the body of the function as a part of the same project, you've done all you need to do.

You're calling the function in Form1.h, correct? As long as your .cpp file is part of the project, everything should be fine. No, do not move the body of the function to the header.

It's confusing because in the Winforms programs, the Form1.h also contains a lot of definitions. If you have included the one line header in Form1.h, are calling the function in Form1.h, and have the associated .cpp file with the body of the function as a part of the same project, you've done all you need to do.

It's not working for me.

Here is my complete .cpp file.

// Form1.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include "xxx.h"

using namespace Form1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}
String ^ParseBBCode(String ^OriginalText) {
String ^ParsedBBCode
//manipulate OriginalText and assign to ParsedBBcode
return ParsedBBCode;
}

Here is xxx.h:

String ^ParseBBCode(String ^OriginalText)

Here is Form1.h, with the design bits excluded:

#include "xxx.h"
#pragma once

namespace Form1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form{/*FORM DESIGN CODE*/

#pragma endregion

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {				 
				 String ^OriginalText;
				 String ^ConvertedText;

				 OriginalText = this->textBox1->Text;

				 ConvertedText = ParseBBCode(OriginalText);

				 this->textBox2->Text = ConvertedText;
				 }
};
}

Is that correct? Why do I get these compiler errors:

/*filepath pointers*/xxx.h(1): error C2143: syntax error : missing ';' before '^'
/*filepath pointers*/xxx.h(1): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
/*filepath pointers*/xxx.h(1): error C2065: 'OriginalText' : undeclared identifier
/*filepath pointers*/xxx.h(1): error C3145: 'ParseBBCode' : global or static variable may not have managed type 'System::Int32 ^'
may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
/*filepath pointers*/xxx.h(1): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
/*filepath pointers*/xxx.h(1): fatal error C1903: unable to recover from previous error(s); stopping compilation

Why is String ^ not a variable type? Do I need #include <string> in xxx.h?

Delete line 5 of Form1.cpp and move lines 20-24 out of Form1.cpp and into a new file xxx.cpp. It shouldn't make that much of a difference, but it's good to keep Form1.cpp clean.

I had told you that in xxx.h, you need to qualify String^ as System::String^ as the compiler has no notion of it otherwise.

System::String ^ParseBBCode(System::String ^OriginalText)[B];[/B]

(you didn't have the semicolon after the declaration, but I'm not sure if that was a typo.

commented: you helped very much. thank you! +1

Thank you, that helped. However, I must say I made my own modifications after some debugging errors popped up. Here is the finished product, for the knowledge base.

/*-----xxx.cpp-----*/

#include "stdafx.h"
#include <string>
using namespace std;
using namespace System;

String ^ParseBBCode(String ^OriginalText) {
String ^ConvertedText;

//MANIPULATION OF TEXT

return ConvertedText;
}
/*-----xxx.h-----*/
System::String ^ParseBBCode(System::String ^OriginalText);
/*-----Form1.cpp-----*/
#include "stdafx.h"
#include "Form1.h"

using namespace Form1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}
/*-----Form1.h-----*/
#include "Function Linker.h"
#pragma once

namespace Form1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{/*Form components and designs*/
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {				 
				 String ^OriginalText;
				 String ^ConvertedText;

				 OriginalText = this->textBox1->Text;

				 ConvertedText = ParseBBCode(OriginalText);

				 this->textBox2->Text = ConvertedText;
				 }

That should be it! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.