Reputation: 93
I think I have a problem with the getline function giving me an error to do with charcters.
The error I'm getting is:
Error 1 error LNK2019: unresolved external symbol
"public: void __thiscall ArrayStorage::read(class std::basic_ifstream<char,struct std::char_traits<char> > &)"
(?read@ArrayStorage@@QAEXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main C:\Users\Lewis\SVN\project1\main.obj
Any ideas would be appreciated please. If anyone has a more effecient way of doing this task using this type of array I would take on any advice given.
Upvotes: 1
Views: 321
Reputation: 234644
blah blah blah unresolved external symbol
"public: void __thiscall
ArrayStorage::read(class std::basic_ifstream<char,struct std::char_traits<char> > &)"
blah blah blah
This is a linker error. It means that a definition for the function ArrayStorage::read
is missing. Why? Because the code has a definition of a function named read
, not ArrayStorage::read
. It should find it if you define ArrayStorage::read
:
//Array cpp:
void ArrayStorage::read(ifstream& fin)
// ...
Once you get past that, the program will probably be able to run. And you'll probably find bugs because of the read loop. while (! fin.eof() )
doesn't "[run] while the file is NOT at the end". It runs while the previous read operation didn't try to read past the end. Consider what must have already happened by the time that check is made:
while (! fin.eof() ) // in the last iteration the read didn't go beyond the end of the file
{ // so one more iteration is ran
getline (fin,line); // tries to read past the end, fails
if (line == "") continue; // line is unchanged, so it could be a non-blank line from before
myArray[arrayIndex]=line; // Saves that line in the array:
// even though no line was read
arrayIndex++;
} // goes back to the start of the loop, and only now !fin.eof() fails
// but it's too late, the damage has been done
You probably don't want this to happen. You want to stop reading as soon as reading fails. That's simple: just put the reading as the condition:
while (getline (fin,line)) // if reading a line fails, the loop is not entered
{ // and so no extra line is added to the array
Upvotes: 3
Reputation: 2616
If I recall, this linker error can sometimes result from your class or one of its bases having other virtual functions that are undefined. The error occurs on the function named, rather than the virtual function that's actually missing a definition, because the MSVC compiler waits until it finds the first CPP file with a defined virtual function for the class to stick definitions of class-header-defined functions in, and when it doesn't find such a CPP file, it never defines the class-header-defined function. GCC behaves differently than MSVC in this respect; I believe GCC sticks the definitions in every object file they're used, and discards extras at link time. MSVC defines it only once, hence is subject to accidently never defining it. The reason it can't "just know" that it "forgot" to insert the definition is because the compiler can be invoked incrementally at any time, on only some of the files at one time, so it doesn't have the big picture.
Upvotes: -1
Reputation: 46856
You're not providing a definition for the Array::read()
function. You're declaring a new function that has the name read()
, but is unrelated to the Array
class. The compiler and linker don't care that it's in a file named Array.cpp
.
Try this instead:
void Array::read(ifstream& fin)
{
//...
}
Upvotes: 2