Multi - File Program not finding files

This is a discussion on Multi - File Program not finding files within the C++ Programming forums, part of the General Programming Boards category; I am working on a databse type program now, and I am still pretty new, so it is my first ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    7

    Multi - File Program not finding files

    I am working on a databse type program now, and I am still pretty new, so it is my first multi file program I've written. The problem is I have variables, arrays and functions that are defined within one .cpp file that we'll call file2. I have also created a file2.h, and include it in the preprocessor directives at the beginning of my main program file, we'll call file1.cpp. However, every time I try to display a variable that was defined in file2, it won't work. It says,

    error C2065: 'bookTitle' : undeclared identifier
    error C2109: subscript requires array or pointer type

    It's like its not reading the other files to the program. Is there something I need to do to link them together? Please help!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >error C2065: 'bookTitle' : undeclared identifier
    If bookTitle is declared in file2.h, then it should find it.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    When you do this, you need to set up your code like so:

    file2.cpp
    Code:
     int var1;
     int var2;
     float var3;
     
     int myFunc(int pass1,int pass2)
     {
     	 return pass1+pass2;
     }
    file2.h
    Code:
     #ifndef FILE2_H_
     #define FILE2_H_
     
     extern int var1;
     extern int var2;
     extern float var3;
     int myFunc(int pass1,int pass2);
     
     #endif
    file.cpp
    Code:
     #include "file2.h"
     #include <iostream.h>
     
     int main()
     {
     	 var1=3;
     	 var2=5;
     	 cout << myFunc(var1,var2) << endl;
     	 cout << "var3==" << var3 << endl;
     
     	 return 0;
     }
    So you basically have a triangle setup for your code:

    Code:
     	  file2.h
     	   /	 \
     	file.cpp  file2.cpp
    -hope that helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 06:04 PM
  3. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 11:31 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21