global access to objects / instances
-
I'm quite new to programming C++ and I ran into a problem. I'm working on a program in which some objects are being used by different functions, in separate files. I created the objects in a header file and can access them from a source file. However, I would like to have an instance of the object available for access from all of my functions. How can I create such objects / instances? Any help is highly appreciated.
-
I'm quite new to programming C++ and I ran into a problem. I'm working on a program in which some objects are being used by different functions, in separate files. I created the objects in a header file and can access them from a source file. However, I would like to have an instance of the object available for access from all of my functions. How can I create such objects / instances? Any help is highly appreciated.
I'm not sure I fully understand your question, but the usual way of doing this is to pass a reference to the object to each function that requires it. Making objects globally visible is generally less safe. For example:
int main()
{
CType* object = new CType();
// do other stuff// call a function, passng it reference to the object BOOL result = MyFunc(object); // etc
}
BOOL MyFunc(CType* anObject)
{
// do other work
// call next function passing the object pointer
int rc = OtherStuff(anObject);// etc
}
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
I'm not sure I fully understand your question, but the usual way of doing this is to pass a reference to the object to each function that requires it. Making objects globally visible is generally less safe. For example:
int main()
{
CType* object = new CType();
// do other stuff// call a function, passng it reference to the object BOOL result = MyFunc(object); // etc
}
BOOL MyFunc(CType* anObject)
{
// do other work
// call next function passing the object pointer
int rc = OtherStuff(anObject);// etc
}
txtspeak is the realm of 9 year old children, not developers. Christian Graus
Thanks for the quick answer Richard! I tried to do this, but the problem I ran into is the following: I have a sourcefile "program.cpp" with header "program.h" in which my classes are defined. I want to pass an object from "program.cpp" to "function1.cpp", by:
double^ function1result = function1(CType^ object)
In function1 I should now write the function as:double^ function1(CType^ object){}
When I do that, I get an "undeclared identifyer" error. When I include the "program.h" header where the class is defined I get a "type redefinition" error. So my question is how to pass these objects in a way that such errors are avoided. -
Thanks for the quick answer Richard! I tried to do this, but the problem I ran into is the following: I have a sourcefile "program.cpp" with header "program.h" in which my classes are defined. I want to pass an object from "program.cpp" to "function1.cpp", by:
double^ function1result = function1(CType^ object)
In function1 I should now write the function as:double^ function1(CType^ object){}
When I do that, I get an "undeclared identifyer" error. When I include the "program.h" header where the class is defined I get a "type redefinition" error. So my question is how to pass these objects in a way that such errors are avoided.Arjen Tjallema wrote:
When I include the "program.h" header where the class is defined I get a "type redefinition" error.
This means you are defining the type (presumably
CType
) in more than one place in your source code. Without seeing more of your header and source file it's difficult to be more explicit.txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
Arjen Tjallema wrote:
When I include the "program.h" header where the class is defined I get a "type redefinition" error.
This means you are defining the type (presumably
CType
) in more than one place in your source code. Without seeing more of your header and source file it's difficult to be more explicit.txtspeak is the realm of 9 year old children, not developers. Christian Graus
Thanks again! I'll put some bits of my code here to clarify my question. The "program.h" header contains a class clGlobals:
public ref class clGlobals
{
public:
double^ airDensity;
double^ waterDensity;
double^ gravitation;
};In my "program.cpp" source I create an instance, put some values in it and (try to) call function1:
clGlobals^ globals = gcnew clGlobals();
globals->airDensity = Convert::ToDouble(1.025);
globals->waterDensity = Convert::ToDouble(1025);
globals->gravitation = Convert::ToDouble(9.81);double^ load = function1();
The "function1.h" header contains a declaration of the function:
double^ load(clGlobals^ global);
And the "function1.cpp" source contains the actual function:
double^ load(clGlobals^ global)
{
// function stuff...
return load;
}I hope this clarifies my question.
-
Thanks again! I'll put some bits of my code here to clarify my question. The "program.h" header contains a class clGlobals:
public ref class clGlobals
{
public:
double^ airDensity;
double^ waterDensity;
double^ gravitation;
};In my "program.cpp" source I create an instance, put some values in it and (try to) call function1:
clGlobals^ globals = gcnew clGlobals();
globals->airDensity = Convert::ToDouble(1.025);
globals->waterDensity = Convert::ToDouble(1025);
globals->gravitation = Convert::ToDouble(9.81);double^ load = function1();
The "function1.h" header contains a declaration of the function:
double^ load(clGlobals^ global);
And the "function1.cpp" source contains the actual function:
double^ load(clGlobals^ global)
{
// function stuff...
return load;
}I hope this clarifies my question.
-
I don't quite get some of this syntax. What does
function1()
do and where is it defined? Why does functionload()
not return a double, or does it?txtspeak is the realm of 9 year old children, not developers. Christian Graus
O, excuse me, I made a mistake in my previous post. The "function1.h" header code should be:
double^ function1(clGlobals^ global);
And the "function1.cpp" source code should be:
double^ function1(clGlobals^ global)
{
double^ load;
// function stuff...
return load;
}I hope it is understandable now.
-
O, excuse me, I made a mistake in my previous post. The "function1.h" header code should be:
double^ function1(clGlobals^ global);
And the "function1.cpp" source code should be:
double^ function1(clGlobals^ global)
{
double^ load;
// function stuff...
return load;
}I hope it is understandable now.
Arjen Tjallema wrote:
I hope it is understandable now.
Yes, and after correcting this line from:
double^ load = function1();
to:
double^ load = function1(globals);
it compiles and runs fine. If you still have a problem then it is not within this code.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
Arjen Tjallema wrote:
I hope it is understandable now.
Yes, and after correcting this line from:
double^ load = function1();
to:
double^ load = function1(globals);
it compiles and runs fine. If you still have a problem then it is not within this code.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
That's strange, I get the following error when trying to compile: function1.h(1) : error C2065: 'clGlobals' : undeclared identifier I have put the four files in a new project to isolate it from the rest of my code, but still I get this error message. Do yo have any clue where to look for the problem?
-
That's strange, I get the following error when trying to compile: function1.h(1) : error C2065: 'clGlobals' : undeclared identifier I have put the four files in a new project to isolate it from the rest of my code, but still I get this error message. Do yo have any clue where to look for the problem?
The best way to resolve this is to add the following lines:
#include "program.h"
#pragma onceat the beginning of
function.h
. And add:#pragma once
at the beginning of
program.h
. This should ensure that the class definition forclGlobals
is found in any file that includesfunction.h
. Also the#pragma once
statements ensure that the header files are processed once only per compilation unit, even if they are found in#include
statements more than once.txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
The best way to resolve this is to add the following lines:
#include "program.h"
#pragma onceat the beginning of
function.h
. And add:#pragma once
at the beginning of
program.h
. This should ensure that the class definition forclGlobals
is found in any file that includesfunction.h
. Also the#pragma once
statements ensure that the header files are processed once only per compilation unit, even if they are found in#include
statements more than once.txtspeak is the realm of 9 year old children, not developers. Christian Graus
That indeed solves the problem. Thanks a lot for your help!
-
That indeed solves the problem. Thanks a lot for your help!