سؤال

I keep getting an ununderstood "unresolved externals error from C++ from Visual Studio 2013. Thank you very much for your help so far. I have reduced the code even more to the following form (but the Problem persists):

main.cpp:

 #include "Fibonacci.h"

using namespace std;

int main(void){

    int RandInteger = 3;
    Fibonacci Fib(RandInteger);

}

Fibonacci.h

class Fibonacci{
    public:
        Fibonacci(int n=0);

    protected:
        int m_n0, m_n1, m_n;
};

Fibonacci.cpp:

#include "Fibonacci.h"

Fibonacci::Fibonacci(int n){
    m_n0 = 0;
    m_n1 = 1;
    m_n = n;
}

This code produces the following error in Visual Studio 2013:

Error 2 error LNK1120: 1 unresolved externals C:\Dropbox\todo\c++\Exam\Ex2\doesn't work\Exercise 2\fibo1\Fibo1\Debug\Fibo1.exe Fibo1 Error 1 error LNK2019: unresolved external symbol "public: __thiscall Fibonacci::Fibonacci(int)" (??0Fibonacci@@QAE@H@Z) referenced in function _main C:\Dropbox\todo\c++\Exam\Ex2\doesn't work\Exercise 2\fibo1\Fibo1\main.obj Fibo1

I persists, but as soon as I replace the line in main.cpp with

        Fibonacci Fib();

i.e. I do not pass the integer to the constructor, everything works (well it compiles an does nothing as expected).

Thanks for your help! I am really still stuck.

هل كانت مفيدة؟

المحلول

I finally found my error. Turns out that there is really nothing wrong with the code itself, but I've somehow destroyed my VisualStudio project. I am really not an expert for these things, but here is what worked for me:

  1. create a new empty project in Visual Studio
  2. copy your cpp files (all of them also the *.h files) into the new project folder
  3. add them to this new project by right clicking the project and using "add New item"

I know this is the straightforward way to do it, but I cannot see how I broke the old project (it should be simple enough after all)

Thanks to all of you - thanks especially to otisonoza and Angew, for setting me on the right track that there is nothing wrong with the code (which works on their end), but with the Visual Studio project.

نصائح أخرى

Your main function should return int

void main(void){

Should be

int main(){

EDIT: Thanks to otisonoza in the comments for pointing out that some compilers accept this definition of main. Other than this, I can't see any cause for compiler errors in your system. Are you sure you pasted the code exactly as you wrote it?

Also, what's up with the random tick after your definition of main?

}`

Also, you don't need to have semi-colons after each function in your .cpp file:

Fibonacci::Fibonacci(int na){
   m_n0 = 0;
   m_n1 = 1;
   m_n = 2;
};

int Fibonacci::getNext(int FnM1, int FnM2){
   return FnM1 + FnM2;
};

can be

Fibonacci::Fibonacci(int na){
   m_n0 = 0;
   m_n1 = 1;
   m_n = 2;
}

int Fibonacci::getNext(int FnM1, int FnM2){
   return FnM1 + FnM2;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top