Menu

Cookbook example

Help
Izo
2006-12-20
2013-04-22
  • Izo

    Izo - 2006-12-20

    Hello, I'm already stuck on the cookbook example.
    When I run the initial example it complains
    about the ComplexNumberTest overloading == when linking:

    Test error LNK2005: "bool __cdecl operator==(class Complex1 const &,class Complex1 const &)" (??8@YA_NABVComplex1@@0@Z) already defined in Complex1.obj

    I get two (presumably from each include). I suspect this is my dodgy C++
    (usually do Java) than the example. Any ideas? I'm using VS 2003.

     
    • Code Guru

      Code Guru - 2006-12-20

      One way (and in my opinion, the prefered way) to avoid this type of error is to create two files for your Complex class:

      // complex.h
      #ifndef COMPLEX_H
      #define COMPLEX_H

      class Complex {
        // private, public, and friend *delcarations* go here
        // for example:

        public:
          Complex(double real, double imaginary);

          friend bool operator==(const Complex& a, const Complex& b);

        private:
          double real, img;
      };

      #endif

      ----------------------------------------------------------

      // complex.cpp

      // This file contains function *definitions*
      // for example:

      Complex::Complex(double real, double imaginary)
        : real(real), img(imaginary) {
      }

      bool operator==(const Complex& a, const Complex& b) {
        return a.real == b.real && a.img == b.img;
      }

      ----------------------------------------------------------

      Notice that this is quite different from Java where you give function declarations separate from their implementation.  In fact, using Java-style coding in C++ can cause some significant problems.

      I hope this answers your questions.  You might find it helpful to understand the difference between a declaration and definition in C++.  This might seem like an oddity since Java does not have this issue, but it is part of the C++ design.

       
    • Izo

      Izo - 2006-12-21

      Thanks very much Layne, that's the problem. Yes, I thought it might
      be to do with having to move the bool operator overloading out
      of the header file and into the CPP file after scanning my C++
      stuff, but since you've made it explicit I can see exactly how
      it's done.

      Thanks a lot.

       
      • Code Guru

        Code Guru - 2006-12-22

        Just an FYI.  This is not specific to operator overloading.  In general, a .h file should NOT contain any executable code.  ALL functions should be in an appropriate .cpp file and the corresponding .h file should only contain function and class declarations.  This goes equally well for global functions and class member functions.

         
    • Izo

      Izo - 2007-01-03

      Thanks Layne. I'm afraid I have another question about the example
      At the very bottom, he says:

      "Now, you need to run your application after compilation.

      With Visual C++, this is done in Project Settings/Post-Build step, by adding the following command: . It is expanded to the application executable path. Look up the project examples/cppunittest/CppUnitTestMain.dsp which use that technic."

      Presumably in VS2003 this is Project> Properties > Config Properties >Post Build Event
      and then just sticking a "." in.

      I can run it without doing any of that and it seems to work fine. I checked
      by changing the operator == so it just returns true, the test fails then
      passes when I fix it.

      My question is, what is the point of the last step, if any?

       
      • Code Guru

        Code Guru - 2007-01-03

        If I understand correctly, the "last step" is to run the program you wrote (which contains the test suite).  So are you asking, "What is the point of running the tests"?  Well, the obvious answer is to make sure your code passes the tests (and therefore behaves as expected).  This seems too simplistic to me, so I think I don't understand your question.  If you can clarify what you are asking, that would help.

        Layne

         
    • Izo

      Izo - 2007-01-28

      Sorry, yes. What I meant was specifically adding the post build event "." in cofig properties. I ran my tests without doing this and it worked fine. Is that just to point the compiler to the current project directory or something? I'm guessing it does this by default in VS2003 as I didn't have to do it to run the tests successfully, I just run the project.
      Evidently it's not that important since it works anyway.

      Thanks,

       
      • Code Guru

        Code Guru - 2007-01-29

        I am not familiar with Visual Studio.  That might be why I misunderstood your question.  Sorry that I can't give any suggestions/answers here.

         
    • Izo

      Izo - 2007-02-08

      No problem Layne, it seems to work fine anyway (had the MFC runner
      going the other day). You've been very helpful to me anyway.

      Cheers,

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.