Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Multiple cpp files in a solution (http://www.go4expert.com/forums/multiple-cpp-files-solution-t3740/)

blackey 4Apr2007 03:25

Multiple cpp files in a solution
 
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.h tm"
1>GoodTimes - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

blackey 4Apr2007 03:46

Re: Multiple cpp files in a solution
 
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?

DaWei 4Apr2007 04:05

Re: Multiple cpp files in a solution
 
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.

blackey 4Apr2007 04:19

Re: Multiple cpp files in a solution
 
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!

DaWei 4Apr2007 05:02

Re: Multiple cpp files in a solution
 
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.

blackey 4Apr2007 05:13

Re: Multiple cpp files in a solution
 
Thank You for all of your help. =)


All times are GMT +5.5. The time now is 08:24.