0

I am trying to learn how to make a modular program. So what I want to do is read an array of integers. Main:

#include <stdio.h>
#include <stdlib.h>
#define NMAX 10


void read (int *n, int a[NMAX]);

int main()
{
    int n, a[NMAX];
    read(&n,a);
    return 0;

}

Then I saved this file 'read.cpp':

#include <stdio.h>
#include <stdlib.h>
#define NMAX 10

void read (int *n, int a[NMAX])
{
    int i;
    printf("dati n:\n");
    scanf("%d",n);
    for (i=1;i<=*n;i++)
    {
        printf("a[%d]= ",i);
        scanf("%d\n",&a[i]);
    }

}

read.cpp compiles succesfully, but when I compile the main function I get the error "no reference to read".

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

1

Include read.cpp when compiling.

g++ -o out main.cpp read.cpp

or

add #include "read.cpp" in main program

sujin
  • 2,813
  • 2
  • 21
  • 33
  • i added the include, and I get a TON of errors I don`t understand. In file included from E:\codeblocks\modul2\read.cpp:1:0, from E:\codeblocks\modul2\read.cpp:1, from E:\codeblocks\modul2\read.cpp:1, from E:\codeblocks\modul2\read.cpp:1, from E:\codeblocks\modul2\read.cpp:1, from E:\codeblocks\modul2\read.cpp:1, and so on – user3078259 Dec 31 '13 at 10:20
  • which compiler and OS you are using? – sujin Dec 31 '13 at 10:21
  • i am sorry sujin, your commentary was very good and it works, I just made a mistake that I didn`t notice in that include. Thank you! – user3078259 Dec 31 '13 at 10:35
  • The right way to do is to have both files in the project. The `#include` cpp file solution is evil. – Jarod42 Dec 31 '13 at 10:38
  • What do you mean? I have both the main function and the read function in the same project. – user3078259 Dec 31 '13 at 10:45
  • @user3078259 your codeblock might have the option to `add file to project` . add your `read.cpp` file using that option then compile. i am not sure about this because i not used codeblocks. This is what @Jaroad42 said. – sujin Dec 31 '13 at 10:50
  • what does "undefined reference to WinMain@16" mean? – user3078259 Dec 31 '13 at 10:55