Thread: Problems with multiple files

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Problems with multiple files

    Hi

    I am working with multiple files in my program, and I have really confused myself. I have tried condensing my problem for this question. First an overview of the 4 files in the program:

    My main.cpp is
    Code:
    #include <iostream>
    #include <vector>
    #include "constants.h"
    #include "ext1.h"
    
    using namespace std;
    
    int main() {
    	vector<double> temp;
    	vector<double>::iterator i;
    
    	ext_func();
    
    	return 0;
    }
    There is one external file "ext1.cpp". The variable b must only be visible to functions within "ext1.cpp", but all other variables declared in "main.cpp" it should have access to.
    Code:
    #include <iostream>
    #include <vector>
    #include "constants.h"
    
    int b = 2; // this must only be visible to ext1.cpp
    
    int ext_func() {
    
    	i = temp.begin();
    	cout << *i << endl;
    }

    The header for "ext1.cpp", "ext1.h"
    Code:
    #ifndef EXT1_H
    #define EXT1_H
    
    int ext_func();
    
    #endif

    The final header "constants.h", which is visible to both .cpp-files
    Code:
    #ifndef CONSTANTS_H
    #define CONSTANTS_H
    
    const int a = 2;
    
    #endif
    My program does not compile. There is an error "1 unresolved externals". Can I get a hint to the error? Besides that I am not sure that the scope for the different variables is as I have intended, but once I can compile, I will be able to test it out.

    Thanks for the help in advance.

    Best,
    Niles.
    Last edited by Niels_M; 10-17-2012 at 02:03 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So if you do "build all", do you see both "ext1.cpp" and "main.cpp" being compiled.

    Posting the actual name of the thing which is unresolved would help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Hi, thanks for replying.

    Quote Originally Posted by Salem View Post
    So if you do "build all", do you see both "ext1.cpp" and "main.cpp" being compiled.
    Oh, I hadn't compiled "ext1.cpp". But now there are 6 errors, all related to "ext1.cpp". "i", "cout", "endl" and "temp" are undeclared identifiers and the final two errors are related to "<<" and ">>". So somehow iostream is not included in "ext1.cpp", but I have included it explicitly.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But ext1.cpp doesn't have
    using namespace std;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Ah, you are right. "ext1.cpp" still can't see "i" and "temp", I was under the impression that everything declared in "main.cpp" was visible to all other "sub"-files(?)

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Niels_M View Post
    "sub"-files(?)
    There is nothing as 'sub' files.
    Perhaps, that is where your confusion comes from.
    The only tool you have is the #include directive (and #define header guards, to prevent problems with that 'inclusion' model) , so,

    if X is declared in x.h and you want to 'see' it in y.cpp, you have to #include "x.h" to get it.
    But, don't #include files which contain function definitions, as the declaration is sufficient for using them.
    Everything else is ideally done by the linker.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by manasij7479 View Post
    There is nothing as 'sub' files.
    Perhaps, that is where your confusion comes from.
    The only tool you have is the #include directive (and #define header guards, to prevent problems with that 'inclusion' model) , so,

    if X is declared in x.h and you want to 'see' it in y.cpp, you have to #include "x.h" to get it.
    But, don't #include files which contain function definitions, as the declaration is sufficient for using them.
    Everything else is ideally done by the linker.
    Thanks to both of you, that helped me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  4. Having problems using multiple files
    By crazyeyesz28 in forum C++ Programming
    Replies: 6
    Last Post: 03-22-2005, 06:40 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM