Hello,

I'm not too experienced with C++ (I know C# pretty well), and I can't figure this out.
I've got two files, Form1.h and Utility.h.
Form1.h is initialized when the program starts, and when the user clicks a button, it should call some functions in Utility.h.

Now the problem I have is, I need the functions in Utility.h to send messages back to the instance of Form1 that has already been initialized, to display to the user.

Here's what I have... Form1.h:

#pragma once
#include <Utility.h>
#include "stdafx.h"

namespace PublishWizard {

	...

	public ref class Form1 : public System::Windows::Forms::Form
	{

Utility.h:

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

using namespace std;

bool ValidPath( string &filePath, Form1 &console )
{

and, my error:

...\Utility.h(6) : error C2061: syntax error : identifier 'Form1'

I have a public function in Form1, Write(string message) , that will show the user whatever message is sent.
So I was planning on passing the instance of Form1 to every function that I use in Utility.h and calling the Write function that way.

Does anyone know any way to fix this error, or any better suggestions?

Thanks

I'm not exactly sure what is going on, but it looks like Form1 is in the PublishWizard namespace and you have not given a PublishWizard:: prefix or a "using namespace PublishWizard" when you try to reference Form1 in Utility.h

Dave

Dave,

Thanks, I was trying that before but wasn't sure exactly if that's what I needed.
When I do either of those,

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

using namespace std;

bool ValidPath( string &filePath, PublishWizard::Form1 &console )
{

I get the same error plus this one:

error C2653: 'PublishWizard' : is not a class or namespace name

I have no idea why I'm getting this error. Because in the main file, "Publish Wizard.cpp", it does just that with no problems:

// Publish Wizard.cpp : main project file.

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

using namespace PublishWizard;

[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;
}

By the way I'm using MS Visual Studio 2008, with the multibyte character set (if that makes a difference)

Okay,

So I'm trying a different method but also getting an error.
Instead of trying to pass Form1 to every function that I call in Utility.h, instead I want to use it as a global variable.

So I changed Publish Wizard.cpp:

// Publish Wizard.cpp : main project file.

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

using namespace PublishWizard;

[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;
}

To this:

// Publish Wizard.cpp : main project file.

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

using namespace PublishWizard;

Form1 myForm;

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

	myForm = gcnew Form1();
	// Create the main window and run it
	Application::Run(myForm);
	return 0;
}

And here are my errors:

1>.\Publish Wizard.cpp(8) : error C3145: 'myForm' : global or static variable may not have managed type 'PublishWizard::Form1'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>.\Publish Wizard.cpp(8) : error C2039: '{dtor}' : is not a member of 'System::IDisposable'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::IDisposable'
1>.\Publish Wizard.cpp(16) : error C2582: 'operator =' function is unavailable in 'PublishWizard::Form1'
1>.\Publish Wizard.cpp(18) : error C2665: 'System::Windows::Forms::Application::Run' : none of the 3 overloads could convert all the argument types
1> c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll: could be 'void System::Windows::Forms::Application::Run(System::Windows::Forms::Form ^)'
1> c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll: or 'void System::Windows::Forms::Application::Run(System::Windows::Forms::ApplicationContext ^)'
1> while trying to match the argument list '(PublishWizard::Form1)'

As for the C2665 error on line 18, I tried casting myForm to type Form, Application::Run(static_cast<System::Windows::Forms::Form>(myForm)); But I got this error:

1>.\Publish Wizard.cpp(18) : error C2440: 'static_cast' : cannot convert from 'PublishWizard::Form1' to 'System::Windows::Forms::Form'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Pointers please?

Thanks,
-Joe

Yikes, please don't use global variables!!

Here is my suggestion - take a step back away from the code and explain to us in English what you are trying to do. Maybe someone can then suggest an appropriate way to go about it.

Dave

Okay.

I'm writing a program to act as an installer of sorts.
Basically it copies some files to the computer and runs them, but it also has to interface with some Canon software (a program called PosterArtist) so it's not as simple as just copying some files over - I also have to use the Windows API and manipulate the PosterArtist window to accept the files I'm sending to it.

I did all of that backend work, and it's done - as a console program.
But now I'm trying to make a GUI version of it.
Along with that, I'm going to add some more functionality later (adding new fonts to the system, adding some PDF documents).
So I need the form that's displaying to the user, Form1, to show a couple of things:
1. A progress bar
2. A "console" showing what actions are being taken (there are delays as PosterArtist accepts the files)

I have it working when the functions are part of the Form1 class, but the problem I'm running into is that when the program is waiting on PosterArtist to accept the files (14 total), it appears to become unresponsive. So I need to somehow initialize another thread to complete the functions in the background, while the main thread only controls the form and remains responsive.

That's why I'm trying to move all of the functions out of the Form1 class (i.e. to Utility.h), so another thread can handle them.
But I know that global variables don't play nicely with multiple threads so I'm trying to stay away from that.

-Joe

This sounds like a situation that calls for a backgroundworker object http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx. That being said, part of the problem you were having above is that you need to pass in a System::Windows::Forms::Form instead of a Form1.

I had to use it as a Form1 because it has a special function in it I created to update the "console" on my form.

I spent a few hours figuring out backgroundWorkers today and am happy to report that my multithreading issues have been solved.

I kind of gave up on moving functions over to a different file, I just put everything in Form1.h and used a few #pragma regions to make it look a little cleaner. It's a small program (< 1000 lines) and we'll never be modifying it in the future so it's not a big deal.

Thanks for the help everyone.

Great, I'm glad it's working. Let me leave you with some words of advice.

It's a small program (< 1000 lines) and we'll never be modifying it in the future so it's not a big deal.

EVERY time I have said something like that, I have later said "Dang, I should have done this better/figured this out the first time!". I try to build a database of examples out of every little project so I have organized code snippets to refer to when I come across the same problem or type of situation in the future.

Good luck,

Dave

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.