0

I have the following two c++ files:

somNode.cpp:

#include <random>
#include <iostream>
#include <chrono>


/*
* This class represent a node in the som-grid
*/
class somNode
{
    private:
        // Weight of the node representing the color
        int nodeWeights[3];
        // Position in the grid
        double X, Y;
        // corner coorinates for drawing the node on the grid
        int top_Left, top_Right, bottom_Left, bottom_Right;

    public:
        // Constructor
        somNode(int tL, int tR, int bL, int bR)
        {
            top_Left = tL;
            top_Right = tR;
            bottom_Left = bL;
            bottom_Right = bR;
        }

};


void my_custom_function(int nodeWeights[])
{
  // construct a trivial random generator engine from a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator (seed);
  std::uniform_int_distribution<int> distribution(0,255);
  for(int i=0; i<3; i++)
  {
    nodeWeights[i] = distribution(generator);
  }
}

and main.cpp:

#include <iostream>
#include <ctime>
#include <cstdlib>

#include "somNode.cpp"

using namespace std;

int main ()
{
    return 0;
}

If I try to run this code I get:

enter image description here

Why do I get this error? I don't understand why the compiler complains about the inclusion? I'm a beginner in C++ programming and everything is a bit new to me x) I know I should use header files in inclusions but I'm just trying to experiment and learn the language :)

jjepsuomi
  • 4,223
  • 8
  • 46
  • 74
  • Thank you for your reply @Niall I have no reason, I'm just experimenting with the code. If I create a somNode.h file and include it would that fix my problem? :) – jjepsuomi Jun 06 '16 at 13:08
  • the actual error is "multiple definition of `my_custom_function(int*)`". The "first defined here" line is a clarification for that error. – Sander De Dycker Jun 06 '16 at 13:12
  • Thank you everybody! Appreciate it :) – jjepsuomi Jun 06 '16 at 13:14
  • 1
    @jjepsuomi As I see in your screenshot you're working with template code. You should also be aware of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – πάντα ῥεῖ Jun 06 '16 at 13:16
  • Thank you @πάνταῥεῖ I will check that link =) – jjepsuomi Jun 06 '16 at 13:17

1 Answers1

2

Don't #include "someNode.cpp" from within a source file.

Your compiler is producing more than one copy of my_custom_function, and the linker doesn't know which one to pick.

Use a header file instead which contains function prototypes and class declarations, and #include that if necessary.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thank you very much. Sorry for my newbie questions, I'm new in C++ and I can't find all the exact answers to my questions with googling x) So did I get this right: the compiler is defining the my_custom_function twice? First in the somNode.cpp file and the second time in the main.cpp file? – jjepsuomi Jun 06 '16 at 13:11
  • 2
    *Exactly* that. Think of `#include` as being equivalent to your copying the included file in place of that `#include` line. – Bathsheba Jun 06 '16 at 13:12
  • Aah, got it. appreciate it! :) – jjepsuomi Jun 06 '16 at 13:14