0

I have a class with a header and a .cpp file. I declare my functions in the header, and define them in the .cpp file, as you would.

Header:

#pragma once

// my #includes 

class CDNAGenerator
{
private:
    // stuff 
public:
    CDNAGenerator(int, int);
    ~CDNAGenerator();

    void FilterMeasurementsForOutliers(std::vector<double>& measurement_values);

    // plenty more things

};

CPP:

CDNAGenerator::CDNAGenerator( int genes, int chromosomes )
{
    // constructor code
}

void CDNAGenerator::FilterMeasurementsForOutliers(std::vector<double>& measurement_values)
{
    // function code
}

Then, from a separate project in the same solution I reference the .h file (but not the .cpp - that seems to lead to multiple definition errors):

#include "..\CalibrationTool\DNAGenerator.h"

And call those functions:

CDNAGenerator* dnaGenerator = new CDNAGenerator(30, 0);
dnaGenerator->FilterMeasurementsForOutliers(values);

But I get unresolved external errors for CDNAGenerator::CDNAGenerator(int, int) and for CDNAGenerator::FilterMeasurementsForOutliers(class std::vector > &)

I thought that I had hooked everything up correctly, so can anyone suggest why I would be getting this linker error?

technorabble
  • 391
  • 7
  • 16
  • You *have* added the source file to the project? Or built with it if you do it manually? – Some programmer dude Sep 18 '13 at 15:17
  • @joachim-pileborg yes, it's definitely in the project (assuming you mean the project where its .h file lives?) – technorabble Sep 18 '13 at 15:22
  • Seperate project, that's the key. If you want code in project A to use code in project B, then project B has to create a library which project A can link against. It doesn't happen just because both projects are part of the same solution. Or you could just add the source file to both projects. – john Sep 18 '13 at 15:35
  • @john it *can* happen, but you have to setup the projects as build-dependencies *and* ensure the dependent project has "Link Dependent Project Libraries" set (forgive me if I don't get the exact wording correct; I don't have devstudio running in front of me right now). Establishing a solution-scope A-references-B in the solution global configuration will setup most of this for you, though it is much more akin to C# than C++. – WhozCraig Sep 18 '13 at 16:11
  • @technorabble have you established a build-dependency between the two projects? (I'm guessing not). Head to the Project menu, select "Build Dependencies..." and ensure your This-depends-on-That chain is properly setup (should be just one checkbox checked if it is setup like you describe). I believe library-link-dependencies is on by default, so that should get you very close to what you want once the dependency is established. – WhozCraig Sep 18 '13 at 16:14

2 Answers2

0

Add the CPP file to the project

Matt
  • 6,010
  • 25
  • 36
  • Can confirm that the DNAGenerator.cpp file is and has always been part of the CalibrationTool project. Are you suggesting I should add it to the calling project? – technorabble Sep 18 '13 at 15:21
  • Yes, otherwise you need export this class and link the lib file – Matt Sep 18 '13 at 15:33
  • sadly that drags in all of the dependencies of the DNAGenerator.cpp file, flooding my Error List with unresolved externals. Is this expected? If I end up needing to pull across everything that defeats the purpose. It feels like there should be a cleaner way... – technorabble Sep 18 '13 at 16:03
  • Then you should export this Class in the other DLL and linked the lib file. Check this:http://stackoverflow.com/questions/27998/exporting-a-c-class-from-a-dll – Matt Sep 18 '13 at 16:08
0

What compiler are you using? Gcc (mingw) does'n supprort #pragma once Use code guards to avoid 'multiple definitions'.

#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass {
...
}
#endif
mugiseyebrows
  • 4,138
  • 1
  • 14
  • 15