how to use an external class

Mar 7, 2011 at 9:09am
Hi everybody,

a general question about C++ classes.

I have written a little simple class OpenCloseApp, and I would like to use it in another file .cpp. I am working with VS2005.
What is the most natural way to make it? Declare namespace, header file?
I am pretty confused.

Thank you

Nathaniel
Mar 7, 2011 at 9:29am
A precision,

for the moment I have defined the class in the same file, and try to declare an object from this class in an other class definition.
It gives me 2 errors:

1>.\SilverlightSample.cpp(18) : error C2065: 'OpenMyApp' : undeclared identifier
1>.\SilverlightSample.cpp(18) : error C2061: syntax error : identifier 'OpenCloseApp'

here a few part of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"
#include "pwinuser.h"
#include "xamlruntime.h"
#include "xrdelegate.h"
#include "xrptr.h"
#include "resource.h"
#include "shellapi.h"


class BtnEventHandler
{
public:

	HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
	{
		OpenMyApp= new OpenCloseApp;  // this is my object declaration, the class is // defined below
		

                BSTR pName;
		source->GetName(&pName);

		if (_tcscmp(L"But1", LPCWSTR(pName)) == 0)
		{
//			ps->szName = L"Solitare";
	//		ps->dwDelay = 1000;
		//	MessageBox(NULL,TEXT("GPS Application"),TEXT("Silverlight for Embedded test"),MB_OK);
		}
};

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR lpCmdLine,
                     int nCmdShow)
{
...
}

class OpenCloseApp
{

public:
	void OpenAppl (LPCWSTR pathToOpen);
};

void OpenCloseApp::OpenAppl(LPCWSTR pathToOpen)
{
	PROCESS_INFORMATION pi; 
	DWORD  dw;
	memset(&pi, 0, sizeof(pi)); 

	if(!CreateProcessW(pathToOpen,NULL,NULL,NULL,FALSE, CREATE_NEW_CONSOLE,NULL,NULL,NULL,&pi))
	{
		MessageBox(NULL,TEXT("Error!!"),TEXT("Windows CE message"),MB_OK);
	}
	
//	printf("dw is equal to: %u \n", dw);
	dw = GetLastError(); 
}



Thanks for help!

Nathaniel
Mar 7, 2011 at 9:57am
Nathaniel:

OpenMyApp= new OpenCloseApp;

This will never ever work (unless OpenMyApp is declared ad OpenCloseApp *OpenMyApp). :)

OpenMyApp is seen by the compiler as identifier - bluntly said some name. This name has no meaning and is not known to the compiler, since you have never told the it what it is supposed to mean!

A valid declaration would be:

OpenCloseApp *myApp;

Now, since you're working with dynamically allocated memory (i.e. new OpenCloseApp), you'll have to declare a pointer to OpenCloseApp and then define what it points to by calling:

OpenCloseApp *myApp = new OpenCloseApp; Remember, this is not Java! ;)

This should solve your problems.

Thomas

Last edited on Mar 7, 2011 at 9:58am
Mar 7, 2011 at 2:24pm
Hi thanks for replying.

This is indeed what I have written, but I cannot understand why it will compile only if I write the class definition and the method at the beginning of the program.

So what I have done after I have read this somewhere on the internet, it is to declare my class prototype
class OpenCloseApp;

and then I have written the entire class and its method at the end.

it indeed accept the instantiation
OpenCloseApp *myApp;

but not the use of the method,
OpenMyApp->OpenAppl(L"\\Storage Card\\solitare.exe");

and then I get some error:

1>.\SilverlightSample.cpp(30) : error C2227: left of '->OpenAppl' must point to class/struct/union/generic type

I don't know what to do.
Have I absolutely to write the class definition in a defined order.

More generally, isn't it a way to define some librairies, namespaces, in order to simply make a using/#include and being able to use the class we defined in other files??

Thank you for your help

Nathaniel
Mar 7, 2011 at 2:35pm
Forget about
OpenMyApp->OpenAppl(L"\\Storage Card\\solitare.exe");

You can't call what doesn't exist and OpenMyApp is still non-existent. You'll have to do the following:

1
2
OpenCloseApp *myApp = new OpenCloseApp;
myApp->OpenAppl(L"\\Storage Card\\solitare.exe");


Here's what you say with this:

a) You declare a pointer to an object of type OpenCloseApp call myApp.
b) You create an instance of type OpenCloseApp and assign the returned pointer to myApp.
c) You call a function of the object by using operator->()

You should really read up on the basics of C++.
Mar 8, 2011 at 9:22am
Hi

thanks for the advice, I have indeed read a lot about the basics of C++. Maybe you don't remember your own beginnings, but the things that seem natural are not always in the basics tutorial we have and even in the books.

For making it short, here is the only implementation that works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

// SilverlightSample.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "pwinuser.h"
#include "xamlruntime.h"
#include "xrdelegate.h"
#include "xrptr.h"
#include "resource.h"
#include "shellapi.h"
//#include "OpenCloseApp.h"
//using namespace OPEN;


class BtnEventHandler
{
public:
	OpenCloseApp *OpenMyApp; // first, 2nd and 3rd error

public:

	HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
	{
		BSTR pName;
		source->GetName(&pName);

		if (_tcscmp(L"But1", LPCWSTR(pName)) == 0)
		{
			OpenMyApp->OpenAppl(L"\\Storage Card\\solitare.exe"); // 4th and 5th error
		}
		else if (_tcscmp(L"But2", LPCWSTR(pName)) == 0)
		{
			MessageBox(NULL,TEXT("Radio!!"),TEXT("Silverlight for Embedded test"),MB_OK);
		}
		return S_OK;
	}
};

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR lpCmdLine,
                     int nCmdShow)
{

blah...blah

return 0;
}

class OpenCloseApp 
{

public:
	void OpenAppl (LPCWSTR pathToOpen);
};

void OpenCloseApp::OpenAppl(LPCWSTR pathToOpen)
{
	PROCESS_INFORMATION pi; 
	DWORD  dw;
	memset(&pi, 0, sizeof(pi)); 

	if(!CreateProcessW(pathToOpen,NULL,NULL,NULL,FALSE, CREATE_NEW_CONSOLE,NULL,NULL,NULL,&pi))
	{
		MessageBox(NULL,TEXT("Error!!"),TEXT("Windows CE message"),MB_OK);
	}
	
//	printf("dw is equal to: %u \n", dw);
	dw = GetLastError(); 
}




here are the errors:
1>.\SilverlightSample.cpp(18) : error C2143: syntax error : missing ';' before '*'
1>.\SilverlightSample.cpp(18) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\SilverlightSample.cpp(18) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\SilverlightSample.cpp(29) : error C2065: 'OpenMyApp' : undeclared identifier
1>.\SilverlightSample.cpp(29) : error C2227: left of '->OpenAppl' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>Build log was saved at "file://c:\Documents and Settings\nathaniel\My Documents\Visual Studio 2005\Projects\Copy of SilverlightSample\SilverlightSample\SilverlightSample_SDK (ARMV4I)\Debug\BuildLog.htm"
1>SilverlightSample - 5 error(s), 0 warning(s)

If I defined my class at the beginning of the file, I got no errors, and as I pass it at the end, I get all those, and pointer definition doesn't help.

And still, sorry to insist on this, but in fact all my question is how to define the class in another file.cpp that could be reused as library or something like this.

Thank you for your help. Please be indulgent.

Nathaniel


Mar 8, 2011 at 10:01am
You have #include "OpenCloseApp.h" commented out.
Mar 8, 2011 at 10:05am
I do remember my beginnings. :) Didn't want to come across so harsh. Sorry!

If I defined my class at the beginning of the file, I got no errors, and as I pass it at the end, I get all those, and pointer definition doesn't help.


Not noticing that was an obvious fault on my part. Sorry for that.

You'll have to at least declare a type in some way before declaring variables of that type. This means that in Order to be able to write

1
2
3
4
5
6
class BtnEventHandler
{
public:
	OpenCloseApp *OpenMyApp; 

   ...


you at least need a forward declaration before class BtnEventHandler - you already did that earlier IIRC:


1
2
3
4
5
6
7
8
class OpenCloseApp;

class BtnEventHandler
{
public:
	OpenCloseApp *OpenMyApp; 

   ...


The thing is that a forward declaration is only useful if you're only using references or pointers to objects of the forward declared type - that's why it is often possible to not include the header for a class within the header of another class.

As soon as you're using the class' interface you need to let the compiler know how the type is really built. This means you'll either have to declare/define the whole class within the file - what you did - or include the declaration of the class from a separate header file.

Thomas

Mar 9, 2011 at 8:21am
Thank you to both of you, really...

Someone knows how to define my class as libraries, from the DLL type.
I explain myself, we can have sometimes several programs that use the same #include .h, but when you modify one, that means you have to open again all your programs and compile them again.
I'd like to know how to make those headers that contain classes definitions, libraries for my programs, that are not compiled with it but used by it.

Thank you.

PS: this is intended for windows CE

thanks

Nathaniel
Topic archived. No new replies allowed.