Linker Errors LNK2019 LNK2001

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NorthernMonkey
    New Member
    • Oct 2008
    • 11

    Linker Errors LNK2019 LNK2001

    Hi,

    I'm new here so if anything I post is not allowed please correct it for me.

    I've been assigned a task of displaying a variable of unsigned int as a binary number in C++. I can do this fine using my own tests.

    I have a class Binary, which has just 1 value number, which is of type unsigned.

    I then overload the output << operator so that whenever cout << Binaryobject ; is called, the binary value of that number is displayed.

    I do this by using a for loop that goes through each bit (all 32 of them). I can display all numbers fine is I use the above call.

    So for example:

    Code:
    Binary b;
    b = b.Set(23);
    cout << b << endl;
    will print out 10111

    However, I have to use a predefined test main method, and herein lies the problem.

    The testing method is

    Code:
    int main()
    {
    	Binary b, b15; unsigned t = 4294967295;
    	ofstream output("test1");
    	if (output == NULL)
    		cerr << "File cannot be opened" << endl;
    	else
    	{
    		output << setw(2) << b.Get()  << " = " 
    			<< setw(6) << b << endl; 
    		for (int i=-28; i<-23;i++)
    		{
    			output << setw(3) << i << " = " 
    				<< setw(32) << Bin(i) << endl;
    			output << setw(3) << -i << " = " 
    				<< setw(32) << Bin(-i) << endl;
    		}
    		output << endl;
    		 b15.Set(15);
    		output << setw(2) << b15.Get()  << " = " 
    			<< b15 << endl << endl;
    		output << "big small binary number: " << t << " = "
    			<< Bin(t) << endl << endl;
    	} 
    	cout << "This is fine" << endl;
    	} 
    }
    This produces the linker errors mentioned above.
    If I miss out the entire else statement it compiles fine. It also compiles if I leave the first line of it - output << setw(2) << b.Get() << " = " ;

    Anything below that and I have this problem.

    The error message I get is

    error LNK2019: unresolved external symbol "class std::basic_ostr eam<char,struct std::char_trait s<char> > & __cdecl operator<<(clas s std::basic_ostr eam<char,struct std::char_trait s<char> > &,class Binary const &)" (??6@YAAAV?$bas ic_ostream@DU?$ char_traits@D@s td@@@std@@AAV01 @ABVBinary@@@Z) referenced in function _main

    The main() has just been provided so I'm not sure what requirements it has or why the error comes up.

    Could anyone help me?

    If you need to see any of my code please ask, but I didn't want to include too much in case it broke rules.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Did you tell the linker to include your (compiled) Binary class?

    kind regards,

    Jos

    Comment

    • NorthernMonkey
      New Member
      • Oct 2008
      • 11

      #3
      Sorry I've only had 3 weeks experience with C++. We had a year learning Java, then suddenly flung into C++ at the deep end.

      What do you mean by telling my linker?

      I have #include "Binary.h" at the top of the main.cpp file along with the other includes
      #include <iomanip>
      #include <fstream>
      #include<iostre am>

      But even if I just copy the main() and run it from within the binary.cpp the error is still there. :(

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I am guessing from the link that you are using a Microsoft compiler/IDE

        In the list of files in the project you need to have both main.cpp (I assume that is the name of the file containing the test routine) and binary.cpp

        If binary.cpp is not in the project list it will not be compiled and it will not be included when the compiled files are passed to the linker.

        Comment

        Working...