Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How can i call a function in another .cpp file?

akfuccakfucc Posts: 6Member
[b][red]This message was edited by akfucc at 2006-5-31 20:10:31[/red][/b][hr]
Hi,everyone,I have a problem that is how How can I call a function in another .cpp file?

Take the following code for example!

the code in main.cpp
[code]
#include
#include "puts.cpp"
struct student
{
int num;
char name[20];
}stud info[2];
extern void add();
void main()
{
add();
}
[/code]
the code in puts.cpp:

#include "main.cpp"

[code]void add()
{
int i;
for(i=0;i<2;i++)
{
scanf("%d",&info[i].num);
gets(info[i].name);
}
}[/code]
Then there are one warning and one error.

warning C4182: #include nesting level is 363 deep; possible infinite recursion

atal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit

I have solve it with using .h file.
Can you tell me how can do it with using .cpp file?

Best regards
akfucc


Comments

  • JaqoupJaqoup Posts: 2Member
    : [b][red]This message was edited by akfucc at 2006-5-31 8:49:8[/red][/b][hr]
    : Hi,everyone,I have a problem that is how How can I call a function in another .cpp file?
    :
    : Take the following code for example!
    :
    : the code in main.cpp
    :
    : #include
    : [blue]#include "puts.cpp"
    : struct student
    : {
    : int num;
    : char name[20];
    : }stud info[2];
    : extern void add();
    : void main()
    : {
    : add();
    : }
    : [/blue]
    : the code in puts.cpp:
    :
    : [blue]
    : #include "main.cpp"
    :
    : void add()
    : {
    : int i;
    : for(i=0;i<2;i++)
    : {
    : scanf("%d",&info[i].num);
    : gets(info[i].name);
    : }
    : }
    : [/blue]
    : Then there are one warning and one error.
    :
    : warning C4182: #include nesting level is 363 deep; possible infinite recursion
    :
    : atal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
    :
    : I have solve it with using .h file.
    : Can you tell me how can do it with using .cpp file?
    :
    : Best regards
    : akfucc
    :
    hi i think that this problem can be solved by removing
    #include "main.cpp"
    from ur other file
    only

  • akfuccakfucc Posts: 6Member
    [b][red]This message was edited by akfucc at 2006-5-31 9:19:49[/red][/b][hr]
    : hi i think that this problem can be solved by removing
    : #include "main.cpp"
    : from ur other file
    : only
    :
    :

    thx.
    I have done as you told me. But when i complie the puts.cpp,some errors show that the structure is undeclared.


  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    [b][red]This message was edited by HK_MP5KPDW at 2006-5-31 9:46:39[/red][/b][hr]
    : [b][red]This message was edited by akfucc at 2006-5-31 8:49:8[/red][/b][hr]
    : Hi,everyone,I have a problem that is how How can I call a function in another .cpp file?
    :
    : Take the following code for example!
    :
    : the code in main.cpp
    :
    : #include
    : [blue]#include "puts.cpp"
    : struct student
    : {
    : int num;
    : char name[20];
    : }stud info[2];
    : extern void add();
    : void main()
    : {
    : add();
    : }
    : [/blue]
    : the code in puts.cpp:
    :
    : [blue]
    : #include "main.cpp"
    :
    : void add()
    : {
    : int i;
    : for(i=0;i<2;i++)
    : {
    : scanf("%d",&info[i].num);
    : gets(info[i].name);
    : }
    : }
    : [/blue]
    : Then there are one warning and one error.
    :
    : warning C4182: #include nesting level is 363 deep; possible infinite recursion
    :
    : atal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
    :
    : I have solve it with using .h file.
    : Can you tell me how can do it with using .cpp file?
    :
    : Best regards
    : akfucc
    :

    [blue]Please use code tags when posting code samples...

    First, you need to understand what the [italic]#include[/italic] directive does. It takes the contents of the file being included and inserts it into the source file in question (in a sense). Thus, [italic]main.cpp[/italic] includes the contents of [italic]puts.cpp[/italic]. But [italic]puts.cpp[/italic] itself includes the contents of [italic]main.cpp[/italic]. But [italic]main.cpp[/italic] includes the contents of [italic]puts.cpp[/italic]. This goes on and on and on and on... The way you've set things up the cpp files end up recursively including eachother which is why you get the above error. You should not be [italic]#include[/italic]ing other source (cpp) files, only header files.

    The structure needs to be defined such that it is visible in both files but the specific instance array of said structure can only be declared once or you will get linker errors. As a suggestion, if you needed to access the [italic]info[/italic] array in more than one cpp file, I would do this:

    [b]student.h[/b]
    [code]
    #ifndef STUDENT_H
    #define STUDENT_H

    struct student
    {
    int num;
    char name[20];
    };

    extern student info[2];

    void add(); // Function prototype
    #endif
    [/code]

    [b]main.cpp[/b]
    [code]
    #include "student.h"

    student info[2]; // Define specific instance of array of structure only once

    [blue]int[/blue] main()
    {
    add();
    [blue]return 0;[/blue]
    }
    [/code]

    [b]puts.cpp[/b]
    [code]
    #include <stdio.h>
    #include "student.h"

    void add()
    {
    int i;
    for(i=0;i<2;i++)
    {
    scanf("%d",&info[i].num);
    gets(info[i].name);
    }
    }
    [/code]

    That is one way, there are variations of the above you could also do to solve this issue. If you don't need to access [italic]info[/italic] from [italic]main.cpp[/italic] at all, then you should probably just declare the struct and array in [italic]puts.cpp[/italic].
    [/blue]


  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-5-31 9:47:9[/red][/b][hr]
    First, never ever include one *.cpp or *.c in another one. Compile them separately and link them both together. Most compilers for MS-Windows os will let you create projects which make that a lot easier for you.

    If two or more *.cpp files need to access common structures or classes, put the declarations of the common items in a header file with .h extension then include that header file in each of the source files.


  • JaqoupJaqoup Posts: 2Member
    : [b][red]This message was edited by akfucc at 2006-5-31 9:19:49[/red][/b][hr]
    : : hi i think that this problem can be solved by removing
    : : #include "main.cpp"
    : : from ur other file
    : : only
    : :
    : :
    :
    : thx.
    : I have done as you told me. But when i complie the puts.cpp,some errors show that the structure is undeclared.
    :
    :
    :
    hi again have u tried to send the variable to the function
    n donot make it global as the second file doesn't see it
  • akfuccakfucc Posts: 6Member
    Many thanks for your help. I think I have leart more here.

    to Jaqoup:
    I had sent the variable to the function,but it is no use. I think if you want to make all of cpp files see the the structure,you must use header files.

    to HK_MP5KPDW:
    I'm sorry I can't use the code tags before.I have edited the message.

    OK,thanks again.
Sign In or Register to comment.