Multiple cpp files in a solution

Discussion in 'C++' started by blackey, Apr 3, 2007.

  1. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I am currently using MS VS 2K5 and when doing something as simple as putting the main in one file and the functions in another file, on build, the solution throws errors.

    The answer must be simple, i just can't figure it out, Thanks! :confused: :confused:

    Index.cpp
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    // prototypes
    template <typename T>
    T func1(T *a);
    
    int main()
    {
    	int ary1[] = {5,4,3,2,1};
    	double ary2[] = {1.1,2.2,3.3,4.4,5.5};
    
    	cout << func1(ary1) << "  " << func1(ary2);
    
    cout << "\n\n\n\n\n";
    system("pause");
    return 0;
    }
    
    funcs.cpp
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // function func1
    template <typename T>
    T func1(T *a)
    {
    	T large=0;
    	for (int i=0; i<5; i++)
    	{
    		if (large < *(a+i))
    		large = *(a+i);
    	}
    	return large;
    }
    
    Errors:
    1>------ Build started: Project: GoodTimes, Configuration: Debug Win32 ------
    1>Compiling...
    1>funcs.cpp
    1>index.cpp
    1>Generating Code...
    1>Linking...
    1>index.obj : error LNK2019: unresolved external symbol "int __cdecl func1<int>(int *)" (??$func1@H@@YAHPAH@Z) referenced in function _main
    1>index.obj : error LNK2019: unresolved external symbol "double __cdecl func1<double>(double *)" (??$func1@N@@YANPAN@Z) referenced in function _main
    1>C:\Documents and Settings\BLackey\My Documents\Visual Studio 2005\Projects\GoodTimes\Debug\GoodTimes.exe : fatal error LNK1120: 2 unresolved externals
    1>Build log was saved at "file://c:\Documents and Settings\BLackey\My Documents\Visual Studio 2005\Projects\GoodTimes\GoodTimes\Debug\BuildLog.htm"
    1>GoodTimes - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     
  2. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Well i have found that the error lies someplace within the templates. If the two files are dumbed down it works fine. Still... Does anybody have an idea as to why the code above doesn't work?
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Templates can be a problem if not handled correctly. The build process works roughly as follows: the preprocessor modifies the source in accordance with preprocessor definition. The compiler compiles that source, resorting to declarations as a guideline. The compiler does not have to know the contents of external procedures or variables, just how to use them.

    The compiler compiles additional sources, as single entities, according to the same guidelines. The linker puts it all together, requring that code exists that conforms to the specification of mere guidelines (declarations) which were enough to satisfy the compiler.

    Remember that the compiler is only dealing with one file at a time (as modified or clarified by included files, which are pasted in by the preprocessor).

    This means that when there is an attempt to expand upon and produce code for a template, that information must be available immediately. This immediacy is not satisfied by information contained in a compilation that may or may not have been produced at a given point in the build process.

    Put yourself in the position of the compiler. If all the required information is not available in the source file, as modified by include files, then you just can't succeed.
     
  4. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Your response sheds some light on how the compilation process works. However i would much appreciate it if you could specifically say where my errors are, and what needs to happen in order to get the templates code working properly. Cheers!
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Your main file cannot instantiate the template. It doesn't know how. It isn't going to seach all the files on your system to find out how. Put your template in your main file or in a file that it includes. It doesn't cost anything, because it's a template. No resources are used until it's called upon.

    Find out what templates and classes are and think about that. You can't, of course, think about it if you don't know how the build process works. Thus, my original post.
     
    Last edited: Apr 4, 2007
  6. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Thank You for all of your help. =)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice