473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
Apr 8 '07 #1
4 4918
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
Apr 8 '07 #2
On Sun, 8 Apr 2007 12:15:55 +0200, "Bo Persson" <bo*@gmb.dkwrote:
>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
Apr 8 '07 #3
Bo Berglund wrote:
:: On Sun, 8 Apr 2007 12:15:55 +0200, "Bo Persson" <bo*@gmb.dkwrote:
::
::: 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
Apr 8 '07 #4
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.
>
#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.
Then you must "#include <subdir/SentinelAGI.h>".
>
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

Apr 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
13
by: I. Dancay | last post by:
Can someone help me work out this project? I need someone who is a C++ expert to help me work this one out. Here it is below. implement an abstract data > type (ADT) Student as a C++ class....
5
by: moumou | last post by:
Hello world ! This may be a naive question, so I apologize in advance ! I'm wonderting if it is possible to find a program that takes a C source code file and split it into multiple files, one...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
15
by: Uday | last post by:
Hi All, Is there way to list all functions in a C files? I've to rename all the functions. thanks, Uday
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any corrections? ...
10
by: Joris Dolderer | last post by:
Hello, I want to compile and link a program statically against the dietlibc. Of course, the dietlibc includes functions my program never needs. Is there any way to throw them out of the final...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.