0

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.

3
  • 2
    Are both cpp files part of the (same) project? Commented Feb 2, 2014 at 22:04
  • I succesfully compiled your code in CodeBlocks, but had to change void main to int main. It turns out void main is VS specific. Commented Feb 2, 2014 at 22:10
  • Out of curiosity, I tried it in VS 2013, and it successfully compiles for me. Commented Feb 2, 2014 at 22:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

2 Comments

stackoverflow.com/questions/636829/… Some compilers accept it, and Visual C++ compiler does.
@Michael: Here's a random thought: You aren't using the int parameter in your constructor. Try using it for something or remove it altogether. Unused parameters will usually only cause warnings, but perhaps you have some options turned on to call them errors or do something else? I don't know, I'm just reaching here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.