在包含.cpp文件后首次在此处定义错误 我有以下两个C++文件:

在包含.cpp文件后首次在此处定义错误 我有以下两个C++文件:,c++,C++,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];

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);
  }
}
#包括
#包括
#包括
/*
*此类表示som网格中的节点
*/
类somNode
{
私人:
//表示颜色的节点的权重
int节点权重[3];
//在网格中的位置
双X,Y;
//用于在网格上绘制节点的角坐标
int左上、右上、左下、右下;
公众:
//建造师
somNode(inttl、inttr、intbl、intbr)
{
左上=tL;
右上=tR;
左下=bL;
右下=bR;
}
};
void my_custom_函数(int节点权重[])
{
//从基于时间的种子构造一个平凡的随机生成器引擎:
unsigned seed=std::chrono::system_clock::now();
std::默认随机引擎生成器(种子);
标准:均匀分布(0255);

对于(int i=0;i不要在源文件中包含“someNode.cpp”

您的编译器正在生成多个
my\u custom\u函数的副本
,而链接器不知道选择哪一个


改为使用包含函数原型和类声明的头文件,并在必要时包含该头文件。

不要在源文件中包含“someNode.cpp”

您的编译器正在生成多个
my\u custom\u函数的副本
,而链接器不知道选择哪一个


改为使用包含函数原型和类声明的头文件,必要时使用
#include

谢谢您的回复@Niall I's no reasons,我只是在试验代码。如果我创建一个somNode.h文件并包含它,可以解决我的问题吗?:)实际错误是“my_custom_function(int*)”的多个定义。“first defined here”一行是对该错误的澄清。谢谢大家!非常感谢:)@jjepsumi,正如我在您使用模板代码的屏幕截图中看到的那样。您也应该知道。谢谢@πάνταῥεῖ 我将检查该链接=)谢谢你的回复@Niall我没有理由,我只是在试验代码。如果我创建一个somNode.h文件并包含它,会解决我的问题吗?:)实际错误是“我的自定义函数(int*)的多个定义”“这一行是对该错误的澄清。谢谢大家!感谢:)@jjepsuomi,正如我在您的截图中看到的,您正在使用模板代码。您也应该知道。谢谢@πάνταῥεῖ 我会检查链接=)非常感谢。抱歉,我的新手问题,我是新的C++,我找不到Google X的所有问题的确切答案。我是不是这样做了:编译器定义MySuxFube函数两次?第一个在SMONDEDE.CPP文件中,第二次是在Me.CPP文件中。ODE 相当于你复制的包含的文件代替了那个代码>包含了行。AAH,得到它。欣赏它。非常感谢。对于我的新手问题,我是新的C++,我找不到Google X的所有问题的确切答案。所以我得到了这个结论:编译器定义MyOxCuffiSoTi功能。两次?第一次是在somNode.cpp文件中,第二次是在main.cpp文件中?正是这样。将
#include
视为等同于复制包含的文件而不是
#include
行。啊,明白了。感谢!)
#include <iostream>
#include <ctime>
#include <cstdlib>

#include "somNode.cpp"

using namespace std;

int main ()
{
    return 0;
}