"Unresolved External Symbol" Error

This is a discussion on "Unresolved External Symbol" Error within the C++ Programming forums, part of the General Programming Boards category; Gosh I hate this message. I'm getting tired of seeing this message, finding the solution, and then forgetting all over ...

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Angry "Unresolved External Symbol" Error

    Gosh I hate this message. I'm getting tired of seeing this message, finding the solution, and then forgetting all over again. Specifically, here's the message:
    Code:
    error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_ifstream<char,struct std::char_traits<char> 
    >::basic_ifstream<char,struct std::char_traits<char> >(char const *,int)"  (__imp_??0?$basic_ifstream@DU?$char_traits@D@std@@@std@@QAE@PBDH@Z) referenced in function
    Like I said, I've had this message before and fixed it but I just can't remeber what I did. Here's the situation for this particlar problem:
    I'm writing a class (BTW, I'm writing a windowed application, so don't complain about what I've included ). Some_Class.h:
    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <string>
    #include <iostream>
    #include <fstream>
    
    class Some_Class:
    {
          ...
    public:
          bool SomeFunction(std::string fileName);
          ...
    }
    I define the functions in Some_Class.cpp:
    Code:
    #include "Some_Class.h"
    
    bool Some_Class::Some_Function(std::string fileName)
    {
          std::ifstream in(fileName.c_str());
          if (in.bad()) return false;
          return true; //Of course I'm going to add more here, instead of just returning
    }
    The first line (where I declare the ifstream) is where the error is occuring. Can anyone tell me why? I've tried including all of the same things as Some_Class.h, which didn't work.
    I also tried sticking this function in Some_Class.h, which did work! Can someone please explain this to me? I'm getting tired of getting this message. Thanks.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Looks like you aren't linking with the iostreams library. Nothing you do in your code will fix this -- you have to fix the link.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > bool SomeFunction(std::string fileName);

    >bool Some_Class::Some_Function(std::string fileName)
    Is that a typo?

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by swoopy View Post
    > bool SomeFunction(std::string fileName);

    >bool Some_Class::Some_Function(std::string fileName)
    Is that a typo?
    Yeah, sorry, just a typo. That's not my real class and function .

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    So what library would that be? It's most likely because I just had to erase all files on my computer do to a computer crash, causing all of my settings (and programs) to be lost. *sigh*

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    libstdc++.a for GCC (or libstdcxx.a for DJGPP) -- so I'd imagine stdc++.lib for MSVC.

    But really, unless your project is a C project, this should be done automatically. I suspect something might be messed up with your installation of MSVC.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    I believe you're right, dwks. I just tried this:
    Code:
    std::ifstream;
    Which compiles for some reason. In fact, even this compiles:
    Code:
    std::ifstream;;
    The only thing that doesn't compile is when I add the filename in the declaration. ??

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, that's probably because this
    Code:
    std::ifstream;
    is just a declaration. It's like a prototype. It doesn't actually do anything. This, on the other hand,
    Code:
    std::ifstream in;
    is a variable declaration. You're actually using std::ifstream. And this
    Code:
    std::ifstream in("file.txt");
    is using the std::ifstream::ifstream(const char *) constructor.

    See if this works.
    Code:
    #include <fstream>
    
    int main() {
        std::ifstream file("hello.txt");
        return 0;
    }
    Nice and simple. If that doesn't work, your compiler is seriously messed up. If that does work, post some more of your code if possible, because your code is seriously messed up.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Then my compiler must be messed up. Like I said, I'm doing a windows application so I couldn't do it in "main" but I stuck under WM_INITDIALOG, just for the sake of things. It, of course, didn't work. Then, I tried just making doing exactly as you said, which, didn't work. I'm using Visual C++ 2008 Express Edition, and everything worked before my computer crash. The thing I'm wondering is why it lets me use ifstreams in my header file, but no where else. It has all of the Intellisence support and everything, so it knows that it's there. What now?

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Because the symbol is in namespace std and in the header you are including. It's complaining about not being able to find the function body for the declared function in the header. So when you try to use it the linker attempts to link with the module containing the function body and code and cannot find it.

    So it's not an undefined symbol since the header has it declared. It's just that there isn't a function that implements what has been declared.

    Linker errors are a pain in the rump because very rarely does MSVC give you any clue as to what library is missing. The errors are usually extremely long and extremely cryptic. In all fairness it really cannot know exactly what library it needs, it just knows that it cannot find the implementation of a function is has the protoype for.

    Take Direct3D as an instance. I can include the headers and forget to specify which libraries to link to. I can even point the compiler to the correct library folders and it will still give unresolved externals. It simply does not know which libraries contain the implementations.

    I did encounter this error today with ifstream and the problem was that it could not find the cpp file that implemented a function that used ifstream. It had nearly the same exact error message. Perhaps you are missing some of your own impementation files and/or libraries. I'm not so sure it's saying it cannot find ifstream because I've gotten this error before and ifstream was not the problem.
    Last edited by VirtualAce; 12-19-2007 at 05:48 PM.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You might try this.

    • See if you can use the C standard library. [1]
    • See if you can use other parts of the C++ standard library. [2]
    • Create a new project, if you haven't already.
    • Compile some source code from the command line.
    • Disable, or at least rebuild, your precompiled headers.
    • Google for other people having similar troubles . . . .


    What happened when your computer crashed? Did you re-install MSVC? If not, maybe it lost some of its files.

    [1] Something like this:
    Code:
    #include <stdio.h>
    
    int main() {
        printf("Hello, World!\n");
        return 0;
    }
    [2]
    Code:
    #include <string>
    
    int main() {
        std::string str = "Hello, World!";
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    @Bubba
    I'm sorry, I'm rather confused by your post. I don't really understand what I'm supposed to do...
    @dwks
    Yes, I re-installed MSVC - it was erased from the computer!
    Anyway, I tried the two examples:
    [1]. Compiles fine, works fine.
    [2]. Compiles fine. However, when I run I get this error:
    Code:
    Run-Time Check Failure #2 - Stack around the variable 'str' was corrupted.
    Not sure if that has to do with the compiler...
    Thanks for all of the suggestions everyone. I wonder why I didn't have to do this last time...

    I still need help, if anyone is willing. (Maybe people thought I was finished?)
    Last edited by mikeman118; 12-19-2007 at 10:13 PM.

  13. #13
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Alright, I now have everything working. It was as I thought, I had to install the platform SDK and do some other things, which I had done before, before it would work. For all who want to know, you can find a tutorial here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21