How to use functions in other files in a C++ program?

B

Bo Berglund

I have defined a few functions in a file, let's say it is called
functions.cpp. There are no objects involved, these are pure simple
functions.

In my test application I want to call this function so in the code I
reach by doubleclicking the button in the IDE I enter:
InitKey();

But when I compile this VS2005 displays an error:
1>c:\engineering\vs2005\vstest32\vstest32\MainForm.h(119) : error
C2065: 'InitKey' : undeclared identifier

But it *is* declared in my file functions.cpp and this file *is*
listed among the project files...

Is there something else I have to do?

Note: I am a Delphi/VisualBasic programmer and have used ANSI C a long
time ago. I am a complte newbie to C++, but I have to port some code
into this environment.
At the time I wrote ANSI C such functions were declared in a H file
and this was included into the using C file. But Visual Studio
actually brings me to a H file to begin with when I doubleclick the
button, so I am at a loss here. I expected to be brought to the proper
cpp file rather than to an h file. And there are no includes inside
the h file...

TIA...


Bo Berglund
bo.berglund(at)nospam.telia.com
 
B

Bo Persson

Bo Berglund wrote:
:: I have defined a few functions in a file, let's say it is called
:: functions.cpp. There are no objects involved, these are pure simple
:: functions.
::
:: In my test application I want to call this function so in the code I
:: reach by doubleclicking the button in the IDE I enter:
:: InitKey();
::
:: But when I compile this VS2005 displays an error:
:: 1>c:\engineering\vs2005\vstest32\vstest32\MainForm.h(119) : error
:: C2065: 'InitKey' : undeclared identifier
::
:: But it *is* declared in my file functions.cpp and this file *is*
:: listed among the project files...
::
:: Is there something else I have to do?
::
:: Note: I am a Delphi/VisualBasic programmer and have used ANSI C a
:: long time ago. I am a complte newbie to C++, but I have to port some
:: code into this environment.
:: At the time I wrote ANSI C such functions were declared in a H file
:: and this was included into the using C file. But Visual Studio
:: actually brings me to a H file to begin with when I doubleclick the
:: button, so I am at a loss here. I expected to be brought to the
:: proper cpp file rather than to an h file. And there are no includes
:: inside the h file...
::

C++ works just like C with regards to .h files. The functions.cpp file
should have a functions.h file declaring those functions that are supposed
to be called from somewhere else. Then #include this .h file "somewhere
else", to make the functions visible there.


You could also consider a better name than functions.cpp for this file. :)


Bo Persson
 
B

Bo Berglund

Bo Berglund wrote:
:: I have defined a few functions in a file, let's say it is called
:: functions.cpp. There are no objects involved, these are pure simple
:: functions.
::
:: In my test application I want to call this function so in the code I
:: reach by doubleclicking the button in the IDE I enter:
:: InitKey();
::
:: But when I compile this VS2005 displays an error:
:: 1>c:\engineering\vs2005\vstest32\vstest32\MainForm.h(119) : error
:: C2065: 'InitKey' : undeclared identifier
::
:: But it *is* declared in my file functions.cpp and this file *is*
:: listed among the project files...
::
:: Is there something else I have to do?
::
:: Note: I am a Delphi/VisualBasic programmer and have used ANSI C a
:: long time ago. I am a complte newbie to C++, but I have to port some
:: code into this environment.
:: At the time I wrote ANSI C such functions were declared in a H file
:: and this was included into the using C file. But Visual Studio
:: actually brings me to a H file to begin with when I doubleclick the
:: button, so I am at a loss here. I expected to be brought to the
:: proper cpp file rather than to an h file. And there are no includes
:: inside the h file...
::

C++ works just like C with regards to .h files. The functions.cpp file
should have a functions.h file declaring those functions that are supposed
to be called from somewhere else. Then #include this .h file "somewhere
else", to make the functions visible there.


You could also consider a better name than functions.cpp for this file. :)
That was just an example name I made up...
Now I have created the real h file SentinelAGI.h and put a reference
into the MainForm.h as follows (finish part of file shown):

<snipped automatically generated stuff that I don't understand>
#pragma endregion <== What is this????

#include "SentinelAGI.h" <== reference to my new h file

private: System::Void MainForm_Load(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void btnInit_Click(System::Object^ sender,
System::EventArgs^ e) {
lblInit.Text = "Start";
if (InitKey)
{ MainForm.lblInit.Text = "OK";
}
else
{ MainForm.lblInit.Text = "Error";
}
}
private: System::Void btnFindKey_Click(System::Object^ sender,
System::EventArgs^ e) {
}
};
}

I have added the SentinelAGI.h file to the project as a "header" file.
It is located in a subdir of its own.

Now VS2005 generates this error:
1>c:\engineering\vs2005\vstest32\vstest32\MainForm.h(115) : fatal
error C1083: Cannot open include file: 'SentinelAGI.h': No such file
or directory

Why can it not find a file that is clearly part of the project????


Bo Berglund
bo.berglund(at)nospam.telia.com
 
B

Bo Persson

Bo Berglund wrote:
::
::: Bo Berglund wrote:
::::: I have defined a few functions in a file, let's say it is called
::::: functions.cpp. There are no objects involved, these are pure
::::: simple functions.
:::::
::::: In my test application I want to call this function so in the
::::: code I reach by doubleclicking the button in the IDE I enter:
::::: InitKey();
:::::
::::: But when I compile this VS2005 displays an error:
::::: 1>c:\engineering\vs2005\vstest32\vstest32\MainForm.h(119) : error
::::: C2065: 'InitKey' : undeclared identifier
:::::
::::: But it *is* declared in my file functions.cpp and this file *is*
::::: listed among the project files...
:::::
::::: Is there something else I have to do?
:::::
::::: Note: I am a Delphi/VisualBasic programmer and have used ANSI C a
::::: long time ago. I am a complte newbie to C++, but I have to port
::::: some code into this environment.
::::: At the time I wrote ANSI C such functions were declared in a H
::::: file and this was included into the using C file. But Visual
::::: Studio actually brings me to a H file to begin with when I
::::: doubleclick the button, so I am at a loss here. I expected to be
::::: brought to the proper cpp file rather than to an h file. And
::::: there are no includes inside the h file...
:::::
:::
::: C++ works just like C with regards to .h files. The functions.cpp
::: file should have a functions.h file declaring those functions that
::: are supposed to be called from somewhere else. Then #include this
::: .h file "somewhere else", to make the functions visible there.
:::
:::
::: You could also consider a better name than functions.cpp for this
::: file. :)
:::
:: That was just an example name I made up...
:: Now I have created the real h file SentinelAGI.h and put a reference
:: into the MainForm.h as follows (finish part of file shown):
::
:: <snipped automatically generated stuff that I don't understand>
:: #pragma endregion <== What is this????

I don't know. It seems like something from the C++/CLI language, not ISO
C++.

Two different languages!

::
:: #include "SentinelAGI.h" <== reference to my new h file
::
:: private: System::Void MainForm_Load(System::Object^ sender,
:: System::EventArgs^ e) {
:: }
:: private: System::Void btnInit_Click(System::Object^ sender,
:: System::EventArgs^ e) {
:: lblInit.Text = "Start";
:: if (InitKey)
:: { MainForm.lblInit.Text = "OK";
:: }
:: else
:: { MainForm.lblInit.Text = "Error";
:: }
:: }
:: private: System::Void btnFindKey_Click(System::Object^ sender,
:: System::EventArgs^ e) {
:: }
:: };
:: }
::
:: I have added the SentinelAGI.h file to the project as a "header"
:: file. It is located in a subdir of its own.
::
:: Now VS2005 generates this error:
:: 1>c:\engineering\vs2005\vstest32\vstest32\MainForm.h(115) : fatal
:: error C1083: Cannot open include file: 'SentinelAGI.h': No such file
:: or directory
::
:: Why can it not find a file that is clearly part of the project????

The compiler doesn't know much about your project, which is managed by the
IDE (Visual Studio).

You have several alternatives here, like:
- putting the .h file (directly) in the project directory
- add the directory to the "Additional Include Directories" of the project
- add it to the "VC++ Directories" in the general Visual Studio options
(Tools, Options)



Bo Persson
 
B

Ben Voigt

That was just an example name I made up...
Now I have created the real h file SentinelAGI.h and put a reference
into the MainForm.h as follows (finish part of file shown):

<snipped automatically generated stuff that I don't understand>
#pragma endregion <== What is this????

#pragma always introduces a compiler-specific feature, which you can usually
ignore. Some interesting ones are #pragma warning which can disable certain
warning messages, #pragma error which stops the compile immediately. This
one is used by the MS editor, along with #pragma region, to create a
collapsible outline block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top