0

I am still learning C++, so reading all similar posts (on SO) about this problem did not give me clarity.

I want to create a method called getFixedLeg inside an object SwapFixedIborDefinition that will return AnnuityCouponFixedDefinition object.

While trying to solve the problem I found that in the Debug folder of the Project these objects where created:

main.obj
SwapFixedIborDefinition.obj
GeneratorSwapFixedIbor.obj

If I comment out the method and all the mentioning of the AnnuityCouponFixedDefinition class then everything works fine.

I have 2 questions:

  1. How to locate and resolve the error I have?
  2. Why do I have different errors when I launch my project (I work in VS2017) in Debug and Release modes?

In case of Debug I get:

    Error   LNK2019 unresolved external symbol "public: __thiscall AnnuityCouponFixedDefinition::AnnuityCouponFixedDefinition(double,class GeneratorSwapFixedIbor,double)" (??0AnnuityCouponFixedDefinition@@QAE@NVGeneratorSwapFixedIbor@@N@Z) referenced in function "public: class AnnuityCouponFixedDefinition __thiscall SwapFixedIborDefinition::getFixedLeg(double,class GeneratorSwapFixedIbor,double)" (?getFixedLeg@SwapFixedIborDefinition@@QAE?AVAnnuityCouponFixedDefinition@@NVGeneratorSwapFixedIbor@@N@Z)   ProjectGenchoCurveConstruction  C:\Users\yanto\source\repos\ProjectGenchoCurveConstruction\ProjectGenchoCurveConstruction\SwapFixedIborDefinition.obj   1   

and

    Error   LNK1120 1 unresolved externals  ProjectGenchoCurveConstruction  C:\QuantLib-1.11\Debug\ProjectGenchoCurveConstruction.exe   1   

while for Release mode I get:

    Error   LNK1104 cannot open file 'deffile'  ProjectGenchoCurveConstruction  C:\Users\yanto\source\repos\ProjectGenchoCurveConstruction\ProjectGenchoCurveConstruction\LINK  1   

all my code is 3 classes and main file, i.e. 7 files:

main.cpp

#include <iostream>
#include <memory>
#include "GeneratorSwapFixedIbor.h"
#include "SwapFixedIborDefinition.h"
// #include "AnnuityCouponFixedDefinition.h"
int main()
{
    GeneratorSwapFixedIbor generator;
    double etc = 0;
    double mktQuote = 0;

    //SwapFixedIborDefinition mySwap(mktQuote, generator, etc);
    std::cout << " finish";

    return 0; 
}

GeneratorSwapFixedIbor.h

#pragma once
class GeneratorSwapFixedIbor
{
    double somePrimitive;
    double startDate;
    double endDate;
    double notional;
    double rate;
    int zero;
public:
    GeneratorSwapFixedIbor();
    ~GeneratorSwapFixedIbor();
    double getSomePrimitive();

};

GeneratorSwapFixedIbor.cpp

#include "GeneratorSwapFixedIbor.h"
#include <iostream>

GeneratorSwapFixedIbor::GeneratorSwapFixedIbor()
    //: somePrimitive{ somePrimitive }
{
    somePrimitive = 101;
    std::cout << "Object GeneratorSwapFixedIbor is being created" << somePrimitive;
}

GeneratorSwapFixedIbor::~GeneratorSwapFixedIbor(){}

double GeneratorSwapFixedIbor::getSomePrimitive()
{
    return somePrimitive;
}

AnnuityCouponFixedDefinition.h

#pragma once
#include "GeneratorSwapFixedIbor.h"
#include "AnnuityCouponFixedDefinition.h"


class AnnuityCouponFixedDefinition
{
//private:
    GeneratorSwapFixedIbor generator;
    double mktQuote;
    double etc;
public:
    AnnuityCouponFixedDefinition();
    AnnuityCouponFixedDefinition(double mktQuote, GeneratorSwapFixedIbor generator, double etc);
    ~AnnuityCouponFixedDefinition();
};

AnnuityCouponFixedDefinition.cpp

#include <iostream>
#include "SwapFixedIborDefinition.h"
#include "GeneratorSwapFixedIbor.h"
#include "AnnuityCouponFixedDefinition.h"

AnnuityCouponFixedDefinition::AnnuityCouponFixedDefinition()
{
    std::cout << "Object AnnuityCouponFixedDefinition is being created";
}

AnnuityCouponFixedDefinition::AnnuityCouponFixedDefinition(double mktQuote, GeneratorSwapFixedIbor generator, double etc)
{
    std::cout << "Object AnnuityCouponFixedDefinition is being created";
}

AnnuityCouponFixedDefinition::~AnnuityCouponFixedDefinition(){}

SwapFixedIborDefinition.h

#pragma once
#include "GeneratorSwapFixedIbor.h"
#include "AnnuityCouponFixedDefinition.h"

class SwapFixedIborDefinition
{
//private:
    GeneratorSwapFixedIbor generator;
    double mktQuote;
    double etc;

public:
    SwapFixedIborDefinition();
    SwapFixedIborDefinition(double mktQuote, GeneratorSwapFixedIbor generator, double etc);
    AnnuityCouponFixedDefinition getFixedLeg(double mktQuote, GeneratorSwapFixedIbor generator, double etc);
    ~SwapFixedIborDefinition();
    double get_smth();
};

SwapFixedIborDefinition.cpp

#include <iostream>
#include "SwapFixedIborDefinition.h"
#include "GeneratorSwapFixedIbor.h"
#include "AnnuityCouponFixedDefinition.h"


SwapFixedIborDefinition::SwapFixedIborDefinition() {}

SwapFixedIborDefinition::SwapFixedIborDefinition(double mktQuote, GeneratorSwapFixedIbor generator, double etc)
    : mktQuote{ mktQuote }, generator{ generator }, etc{ etc }
{
    std::cout << "Object SwapFixedIbor is being created";
}

AnnuityCouponFixedDefinition SwapFixedIborDefinition::getFixedLeg(double mktQuote, GeneratorSwapFixedIbor generator, double etc)
{
    return AnnuityCouponFixedDefinition(mktQuote, generator, etc);
}

double SwapFixedIborDefinition::get_smth() {
    std::cout << " we can call smth ";
    return 100;
}

SwapFixedIborDefinition::~SwapFixedIborDefinition(){}

This is how my solution explorer looks like.

solution explorer

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
gencho
  • 3
  • 4
  • 2
    You have to compile all involved cpp files and will get the resp. object files: `main.obj`, `SwapFixedIborDefinition.obj`, **and** `GeneratorSwapFixedIbor.obj`, `AnnuityCouponFixedDefinition.obj`. All these files have to be linked to resolve all needed symbols. If you use an IDE (e.g. VS2017), just add _all_ involved cpp files to your project (and the rest will be done automatically). – Scheff's Cat Feb 18 '18 at 13:34
  • Please, consider: `#include` is needed for (resolved by) the compiler. The linker has to be told which object files to link. These are two separate things. – Scheff's Cat Feb 18 '18 at 13:37
  • @Scheff , could you please explain in different words? I made an update ( all xxx.obj are created except `AnnuityCouponFixedDefinition.obj`) I never touched the Properties of the project, like Linker, etc and it was ok until I created _AnnuityCouponFixedDefinition_ class. Am I confusing something with the Linker? – gencho Feb 18 '18 at 13:46
  • Is `AnnuityCouponFixedDefinition.cpp` in the `sources` folder of your VS2017 project? I don't mean a directory in the file system. I mean the folder "sources" icon in the Project Explorer of your VS2017. Sorry, in mine it is called "Projektmappen-Explorer" and I don't know how it is exactly titled in the English version - or your localized version. – Scheff's Cat Feb 18 '18 at 13:50
  • Got it. In the English version, it's the Solution Explorer. I found it in the doc. on MSDN: [Solutions and Projects in Visual Studio](https://msdn.microsoft.com/en-us/library/b142f8e7.aspx) – Scheff's Cat Feb 18 '18 at 13:55
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Jesper Juhl Feb 18 '18 at 14:02
  • I might screwed everything up, as I deleted the Header Files, Resource Files, Source Files folder.. and I am throwing all the files in the project directly, please see screenshot at the end of my question. @JesperJuhl, I saw that question. That question has a too general answer -> difficult to see solution to my problem. – gencho Feb 18 '18 at 14:02
  • Yepp, that looks strange. May be, the most easy way is to throw this project away. You don't need to delete/re-type your cpp files. Create a fresh project, and add your existing cpp files to the Source Files folder. – Scheff's Cat Feb 18 '18 at 14:08
  • ok. That solved the problem.. Thank you very much, @Scheff! Could I ask the last question about difference of Debug and Release errors? Why are they different? What is the best way to develop code and test its correctness? ( Please put your suggestion as an answer, I will accept it) – gencho Feb 18 '18 at 14:37
  • In Debug mode, a lot of additional information is compiled into the code. (A lot of libaries incl. the standard libaries are equipped with extra tests which a considered in debug mode only.) This helps to find problems but slows execution. (Aside the effect that it simplifies reverse engineering of executable code.) In Release mode, the compiler produces optimized code which runs much faster but prevents easy visual source code debugging. Actually, Debug and Release mode are default compiler configurations. You can add your own (may be, later). – Scheff's Cat Feb 18 '18 at 14:42
  • If you want to deploy/deliver your application, it is a good idea to involve the basic Microsoft DLLs which are necessary to run your executable. For this, the VS2017 includes a [redist](https://social.msdn.microsoft.com/Forums/vstudio/en-US/776c1d78-28f3-40e1-9570-c5fc6b3db4fc/redistributables-not-available-for-c-2017-on-client-mfc140uddll?forum=vcgeneral)ributable. For VS2013, where I work with, we once downloaded it from MSDN. According to the link, it should be included in VS2017. However, it's only available for Release builds. MS don't want that debug builds are distributed. (Security!) – Scheff's Cat Feb 18 '18 at 14:49
  • Danke schön, @Scheff ! – gencho Feb 18 '18 at 15:40

0 Answers0