简体   繁体   中英

C++ Collection Objects & iStream File Input

I've been stumped on my code all day today. I have a number of classes to deal with a collection of tracks and albums. My code compiles and it reads in the file successfully, but as soon as i try to use the istream& operator in my Album Collection class it errors at run time.

This is the error:

   error LNK2001: unresolved external symbol "class std::basic_istream<char,struct    std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class AlbumCollection &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVAlbumCollection@@@Z)1>C:\Users\Shardy\Desktop\AlbumDatabase\Debug\AlbumDatabase.exe : fatal error LNK1120: 1 unresolved externals

I would really appreciate some help. Its become frustrating. None of my books nor google is shedding any light. Thanks for your time.

Some issues:

  1. main.c: If the file is successfully opened, you state that the file was loaded successfully. Why bother opening the file in the first place if it is already loaded? Hmm, how can that be?
  2. main.c: You never call any method that reads from the file.
  3. main.c: You can use if (file) instead of if (file.is_open())
  4. main.c: You're missing a closing } in the if statement.
  5. main.c: You're not using argc or argv so you can declare main as main(void) .
  6. You need to post the class declaration (header file) as well as the method definitions.
  7. duration.cpp: Your input method will gag when it encounters something not numeric or whitespace, such as ":".
  8. duration.cpp: Your addition operator doesn't perform time arithmetic correctly, especially overflows. Try adding "00:59:35" with "00:02:30".
  9. duration.cpp: Your operator== will return the result of the last expression, because you are using the comma, ',' operator. You probably want to use the && or logical AND operator.
  10. duration.cpp: Rule of 3: If you define a constructor, copy constructor or destruction you should declare all 3 and an assignment operator.
  11. duration.cpp: Rule of Math: If you define operator+ or operator- you should also implement operators += and -= as well. See Boost math library.
  12. duration.cpp: Rule of ordering: If you plan to order or compare, you should implement operators <, <=, ==, !=, >, and >= . Hint: convert the time into seconds, which should make your life easier when comparing. See boost::less_than_comparable.
  13. track.cpp: You're input method reads the track name first, then the duration. The data file shows duration first then track name. You also have no code to deal with the field separator between duration and column.

    Yep, a debugger would be helpful as well as pencil and paper to go through this code.

Trivially, in your main you have unmatched braces it appears. Your else statement should have a closing brace before (ie } else {...} )

More importantly, I will try to address the particular problem listed in your main (in regards to compilation). First, I list my assumptions. As it seems, your implementation and interface are appropriately separated (ie your class is declared in a header file and your implementation is in a source file) due to the use of your scoping ( :: ) for function implementation.

Furthermore, I assume your implementation of your operator<< is in the same file as your AlbumCollections implementation. This is fine, however, your main file does not know this. It is currently only being compiled for the source file. Without more information, I assume this is key to your problems. You will need to forward declare this operator so it works in other files.

In your header file for album collections, include the forward declaration after your AlbumCollection declaration as follows (I assume you are not using the using directive in your header. If you are, you probably shouldn't be):

std::istream& operator>>(std::istream& is, AlbumCollection& albums);

You may need to in your header #include <iostream> for this to properly compile. However, anywhere you include AlbumCollection.h (or whatever its corresponding header is called), this overload will be usable. When you link your program together, the definition you defined earlier will be used. Additionally, for this to work, it is likely that you will have to remove the inline qualifier from your definition.

Note that if this was the issue, the error output was probably horrendous looking. If you had this problem, a lot of the compiler error output will go away and it will be much simpler to debug any existing code compilation problems you may have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM