Getline in c++ ifstream causing unwanted behavoir? - c++

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.

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

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)
{
//...
}

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.

Related

LNK2001 unresolved external symbol with CPP_XLOPER

I'm migrating a XLL from 32-Bit to 64-Bit with VS2015 and C++.
I started changing the datatypes. So I switched 'int' to '_int64'.
CPP_XLOPER Create_XLOperHeader_form_Str(const wchar_t*aBegin,_int64 strlen,bool aTranspose){
static CPP_XLOPER xlDefault(L" ");
_int64 l = strlen;
and it throws me the error:
Error
LNK2001 unresolved external symbol
"class CPP_XLOPER __cdecl Create_XLOperTable_from_Str(wchar_t const *,__int64,bool,bool)"
(?Create_XLOperTable_from_Str##YA?AVCPP_XLOPER##PEB_W_J_N2#Z)
I guess I have to modify 'class CPP_XLOPER', isn't it?
Any hint much appreciated,
thx in advance;
surplus
The method linker is complaining about has last two parameters of type bool - ...,int64,bool,bool), while the one you have modified has only one last parameter of type bool. Either you have deleted another existing method, or you have also removed one bool from the implementation (and only you know if this was or was not intentional). But yeah, in general you should fix the function declaration to match the definition.

c++ LNK2001 & LNK1120 compile errors [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am debugging some C++ software, and want to modify an existing function slightly, so that it changes the value of a particular variable used elsewhere in the program.
The function is currently defined as so:
void DataStore::setEraseSelected(){
...
// Function code here
...
}
As it stands, the function works correctly with no problems whatsoever. But, I now want to modify the function, so that it will change the value of a variable used elsewhere in the program. To do this, I have tried passing the variable into the function as a parameter (I have also updated the header file to reflect the changes to the function), and then assigning a value to the variable inside the function:
void DataStore::setEraseSelected(toAMS::DataMessage statusMsg){
...
// Function code here
...
statusMsg.CODE_ERASE = Types::Activated;
...
}
As mentioned, I have added the declaration to the header file, so that it now has the declaration for the function with the parameter, as well as the one for the function without the parameter:
void DataStore::setEraseSelected(toAMS::DataMessage statusMsg);
But when I try and build the code (using Visual Studio 2010), I get the following two compile errors:
error LNK2001: unresolved external symbol "public void_thiscall DataStore::setEraseSelected(void)" (? setEraseSelected#DataStore::QAEXXZ)
error LNK1120: 1 unresolved externals
The first error highlights the project .obj file, which I have tried deleting and building again, but get the same error, and second one highlights the project .exe file, which I have also tried deleting and building again, but get the same error.
Anyone have any ideas why? I've had a look on SO for questions regarding these errors, but none of them seem to clearly explain why I might be getting them. They all seem to suggest that the compiler is possibly looking in the wrong place, but if I undo my changes, then the code compiles with no problems, and I haven't told the compiler to look anywhere else when building the code with my changes...
void DataStore::setEraseSelected(int );
Pass that variable

C++ Visual Studio Linker Error

I made a boolean method which will return true or false. I want to see the actual value it returns, so I tried this in my main method:
bool answer = methodName(); // I made sure to include the parameters in my code
cout << answer << endl;
It gives me the following error:
error LINK2019: unresolved external symbol "bool ___cdec1" methodName(parameters)
Thanks in advance for the help!
The linker is complaining because you declared your function methodName() but you did not provide a definition for it.

Is there a way to prevent usage of unimplemented functions during compile time?

often I encounter hacks like
//lets say this is some class that still doesnt support...
//...all the functionality that it should based on the design docs
void MyClass::MyFunction()
{
throw std::exception("not implemented");
}
I guess this is a bad practice, but that aside:
Is there a way to do the same thing during compilation, but only if the function is used (aka if it is unused compile should succeed).
Edit: Im also interested in virtual mem functions.
If it is non-virtual function then you can simply comment out the definition.
If it is a virtual function declared in a base class, then you can't control the calls at compile time, so then your only option is some run-time error or exception.
If you remove the implementation entirely and only have the function declaration, there will be a linker error, which is basically compile time. Unfortunately, linker errors have a tendency to be ugly and hard to track down, but in the case of calling a function that hasn't been implemented yet I think they're pretty manageable.
Old question, but still...
I use a couple of simple helper for this. It'll give an error link-time that's fairly readable:
// Not implemented is not implemented :-)thing, it'll break:
struct NotImplHelper { static void notimplemented(); };
#define notimplemented() NotImplHelper::notimplemented();
#if defined(DEBUG) || defined(_DEBUG)
#define notimplementedvirtual() throw std::exception();
#else
#define notimplementedvirtual() static_assert(false, "You should implement virtual function calls before moving to production.");
#endif
Usage:
//lets say this is some class that still doesnt support...
//...all the functionality that it should based on the design docs
void MyClass::MyFunction()
{
notimplemented();
// or notimplementedvirtual() if MyFunction() is virtual...
}
Rationale:
IMHO if you use a function in your program, it should be available. When you try to compile something that you haven't implemented yet, it should give a compile-time or link-time error.
F.ex., in MSVC++ this'll give:
1>Test.obj : error LNK2019: unresolved external symbol "public: static void __cdecl NotImplHelper::notimplemented(void)" (?notimplemented#NotImplHelper##SAXXZ) referenced in function "[blahblahblah]"
Note that the 'referenced function' is there in MSVC++. I haven't tested it in other compilers.
As for not-implemented virtual function calls, the only option you have it to throw an exception. Not having these implemented in your debugger while developing is fine - however, the moment stuff gets serious, these might be called by your program, so they should be available. A static_assert ensures the latter. (So: combined with any continuous integration package, it'll basically fail.)
Obviously most people will accidentally mix up notimplemented and notimplementedvirtual. In reality this ain't a big problem: a simple solution is to always use the former, unless you want to get rid of the error because it's a WIP.
The simplest solution I can think of is to comment the unimplemented function.
Maybe not what you had in mind, but doing so will generate a compile-time error if anything tries to use it, and the resulting code should be identical to an empty function, which is usually optimized away.

What is __unwind$ in a linker map file

For VS2008 (C++) generated linker map files, what does the symbol "__unwind$" mean? I have a good chunk of them in the linker map file for my app.
I have a log which says a crash happens at a particular offset say 'x'. When I look at the linker map for this offset, I find this __unwind$41357 corresponding to the offset.
Also generally is there any reference to understand the file format of linker map files?
"Unwinding" is happens with a stack when an exception is thrown. The __ prefix indicates a compiler-generated symbol. So, based on the description, you get a crash between a throw and a catch. My assumption is that the destructors called are called from the __unwind$ functions. An inlined destructor wouldn't have its own stackframe, so it would show up in the calling __unwind$ function.
Only a guess, but I would say it is part of the code that handles stack unwinding when an exception is thrown.