1

I'm having a headerfile called cnVector.h whose implementation is written in cnVector.cpp. Those two files are located in the same directory.

cNormalCBP/
   + src/
       + cNormal/
           + cnUtils/
               - cnVector.h
               - cnVector.cpp
       - main.cpp

The header contains a simple class definition.

class cnVector {
    public:
        cnVector(double, double, double);

        inline cnVector cross(const cnVector&) const;
};

The implementation in the .cpp file is as follows:

#include "cnVector.h"
/* constructor */   cnVector::cnVector(double x, double y, double z)
        : x(x), y(y), z(z) {
}

cnVector cnVector::cross (const cnVector& vOther) const {
    return cnVector(
        y * vOther.z + z * vOther.y,
        z * vOther.x + x * vOther.z,
        x * vOther.y + y * vOther.x );
}

Now, the following code from main.cpp breaks at line 3 because of an undefined reference to cnVector::cross(cnVector const&) const;
Note how the constructor-implementation is recognized, but not the cnVector::cross method.

int main() {
    cnVector v1(1, 0, 0), v2(0, 1, 0);
    cnVector v3 = v1.cross(v2);
}

I also get an error-message warning: inline function 'cnVector cnVector::cross(const cnVector&) const' used but never defined.
Copying the implementation into main.cpp works.

Can you explain to me why I can construct a cnVector instance but the implementation of other methods are not recognized ?

5
  • "code from main.cpp breaks at line 3" -- by that you mean you get a linker error? Also are you sure you have cnVector.cpp listed in "Build target files" under "Build target tab" in "Project->Properties"? Finally can you post full main.cpp? Want to see #includes Commented Oct 25, 2011 at 19:56
  • Niklas, can you tell us what compiler/os you're using? And is there any code that's been omitted here? As I noted on the answer below, I cut-and-pasted your code and it works fine for me in VS 2010 on Win7. Commented Oct 25, 2011 at 20:01
  • @Joe Of course, sorry. Win7 CyGWin GCC. Yes there is omitted code, in the headerfile other method declarations and their implementation in the .cpp file as well as innecessary includes. In the main.cpp I missed the #include cNormal/cNormal.h". cNormal.h basically includes cnUtils.h which includes cnVector.h. Commented Oct 25, 2011 at 20:14
  • @thekashyap Yes, I do. I didn't modify the settings, I also have rewritten the whole cnVector class already because of thid problem. I will take a look at the settings, thanks. (can't now) Will paste the whole main.cpp tomorrow Commented Oct 25, 2011 at 20:17
  • "I didn't modify the settings" -> In Code Blocks when you add a new cxx you should. Though it would ask you, when you add a cxx, which targets (Debug/Release/both) do you want to add new source file to and you just have to say yes. Though I feel TheBuzzSaw probably has the problem identified. Commented Oct 25, 2011 at 20:29

1 Answer 1

2

Move your inline functions to your header file. Inline functions need their entire definitions in the header files because of how they integrate with the rest of your code. The compiler will (maybe) attempt to insert the code at all locations where the function is called, so it needs to be visible in the header file similar to how templates need to be entirely present in the header file.

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

5 Comments

The inline keyword should allow the definition of the inline function in the .cpp file. And in fact, when I cut-and-paste the code in the question into Visual Studio 2010, it compiles and executes just fine.
I use Code::Blocks and mingw myself. I got rid of all problems by placing the entire definitions in the header file. Personally, I don't recommend placing the implementation into the .cpp file. If it's a short function, it won't clutter things up in the header. If it's a long function, maybe it shouldn't be inline. :D
Can't say I disagree, and it should definitely make the problem disappear. But I am very curious to why he/she's having the problem in the first place, since the code appears valid.
Before I knew about moving them to the header file, I would use the inline keyword in both places (prototype and definition). That seemed to make it behave, but I learned later about keeping it all in one place.
Yes, this was the problem. I have thought that the compiler could, elsewise to templates, insert the object-compiled code instead of the real C++ code. Thanks !

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.