Thread: Help on error LNK2019

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    14

    Question Help on error LNK2019

    Hello,
    The message from the linker is:

    Code:
    main.obj||error LNK2019: unresolved external symbol "class std::vector<int,class
     std::allocator<int> > __cdecl vfnd(class std::vector<int,class std::allocator<int> >,int)"
     (?vfnd@@YA?AV?$vector@HV?$allocator@H@std@@@std@@V12@H@Z) referenced in
     function _main|
    It seems to me it is finding some kind of problem with the types of the parameters when I call function vfnd.cpp (a function to find the positions in a vector of occurrences of a given integer value).
    The main function is as follows:

    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include "rddt.h"
    #include "vfnd.h"
    #include "wvct.h"
    
    using namespace std;
    
    int main(){
    
    //----------------------------------------------------------------- Variable declaration
    
    	int sid, infv, ndlb, dmn;
    	string adj = "adj.txt";
    	string cst = "cst.txt";
    
    	vector<vector<int> > adjm;
    	vector<vector<int> > cstm;
    	vector<vector<int> >& padj = adjm;
    	vector<vector<int> >& pcst = cstm;
    
    	vector<int> prm, dlb, prd, tmp, rchv, pns, vadj;
    
    //----------------------------------------------------------------- Calling in input variables
    
    	rddt(adj, padj);                                                // it modifies adjm
    	rddt(cst, pcst);                                                // it modifies cstm
    
    	cout << "source? ";
    	cin >> sid;
    
    //----------------------------------------------------------------- Initializations
    
    	typedef vector<int>::size_type v_sz;
    	v_sz n;
    
    	n = adjm[0].size();                                             // order of network
    
    	infv = 10000000;                                                // infinity definition
    
    	dlb.assign(n, infv);                                            // distance labels initialization
    	prd.assign(n, infv);                                            // predeccesor list initialization
    
    	dlb[sid] = 0;                                                   // starting alteration of control vectors
    	prd[sid] = 0;
    
    //----------------------------------------------------------------- Core process
    
    	while (prm.size() != n){
    
    		prm.push_back(sid);
    		tmp[sid] = 0;
    		vadj = adjm[sid];
    		rchv = vfnd(vadj, 1);                                      // see vfnd.cpp for details
    
    		vector<int>::const_iterator i, j;
    
    		for(i = rchv.begin(); i != rchv.end(); i++){               // Update of distance labels
    
    			ndlb = dlb[sid] + cstm[sid][*i];
    
    			if (dlb[*i] > ndlb){
    				dlb[*i] = ndlb;
    				prd[*i] = sid;
    			}
    		}
    
    		pns = vfnd(tmp, 1);                                        // possible new sources
    		dmn = infv;
    
    		for(j = pns.begin();j != pns.end(); j++){                  // selection of new source
    
    			if(dlb[*j] < dmn){
    				sid = *j;
    				dmn = dlb[*j];
    			}
    
    		}
    
    	}
    
    	string sdlb = "dlb.txt";
    	string sprd = "prd.txt";
    
        wvct(sdlb, dlb);
        wvct(sprd, prd);
    
    	return 0;
    }
    The headers and definition of the vfnd.cpp function are:

    Code:
    #ifndef VFND_H_INCLUDED
    #define VFND_H_INCLUDED
    
    #include <vector>
    
    using std::vector;
    
    vector<int> vfnd(vector<int>, int);
    
    #endif // VFND_H_INCLUDED
    and

    Code:
    #include <vector>
    
    std::vector<int> vfnd(std::vector<int> vct, int tvl){
    
    	int dmvl;
    	std::vector<int> rchv;
    	std::vector<int>::const_iterator i, ov;
    	ov = vct.begin();
    
    	for (i = vct.begin(); i != vct.end(); i++){
    
    		if (*i == tvl){
    			dmvl = i - ov;
    			rchv.push_back(dmvl);
    		}
    	}
    	return rchv;
    }
    The function is called twice. The first time using a vector extracted from a vector of vectors (adjm). The call always looks for the integer 1:

    Code:
    vadj = adjm[sid];
    rchv = vfnd(vadj, 1);
    The second call is made for a pure vector (tmp):

    Code:
    pns = vfnd(tmp, 1);
    If you could help me pointing out what is wrong here, I'd appreciate it.
    Thanks.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Don't put using statements in header files.

    I don't see a #include "vfnd.h" in your vfnd.cpp file.

    Are you linking with the object file where your function is? vfnd.obj for example.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by jlbfunes View Post
    It seems to me it is finding some kind of problem with the types of the parameters when I call function vfnd.cpp
    I wouldn't say it's a problem with the arguments, since those kind of errors are detected by the compiler normally, and not the linker.

    Maybe your vfnd.cpp source file is not getting compiled ? Did you exclude it from the build ? Because as far as I know this is the kind of error you'll get when a function is declared but the function isn't defined anywhere (which happens if it the source code hasn't been compiled).
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    14

    Thumbs up It compiled!

    Hello guys.
    Let's start with the good news. I could compile the thing. I didn't include the rights files into my project as foxman pointed out. Thank you! This mistake will show you that I am a newbie on this and I am learning by myself (... and yes, you would add a little retarded).
    Now, and consequently with the last confession I would like to ask you some details about these statements.

    Quote Originally Posted by cpjust View Post
    Don't put using statements in header files.
    I don't see a #include "vfnd.h" in your vfnd.cpp file.
    a). Why is not right to use using statements? Isn't it the same as using std::whatever?
    b). I was able to compile the program without using the #include "vfnd.h" statement in the .cpp file of the function. I thought that it was only needed in main.cpp. Isn't that just right?

    Thank you to all of you! You really gave me a hint on this.

    Bye.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. There's a difference between "not using 'using' statements" and "not using 'using' statements in header files".
    2. In this case, everything's fine. It's not unusual, though, for the .h file to be necessary in the .cpp file (for example, if the functions in the .cpp file call each other, then you'll need the prototypes for those functions; or if a class or a struct is defined in the .h file, then that new type wouldn't be visible unless the .h file was included).

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jlbfunes View Post
    a). Why is not right to use using statements? Isn't it the same as using std::whatever?
    b). I was able to compile the program without using the #include "vfnd.h" statement in the .cpp file of the function. I thought that it was only needed in main.cpp. Isn't that just right?
    a) When you put using std::vector; or worse yet using namespace std; in a header file it means that any person that #includes that header will now have that namespace imported. If they have their own class called vector, the compiler could choose the wrong class or give an error about an ambiguous statement...
    Similarly, putting a using statement before an #include could change the meaning of the code inside the header that you're including.
    If you want to put using statements in your code, put them in .cpp files after all your #include statements.

    See: Chapter 59. Don't write namespace usings in a header file or before an #include

Popular pages Recent additions subscribe to a feed