【问题标题】:g++ compiler "redefinition…previously defined"g++ 编译器“重新定义……先前定义”
【发布时间】:2018-04-27 20:17:38
【问题描述】:

我知道有类似的问题,但在我的情况下这些都不起作用。嗨,我找不到为什么我有这个问题。 这是我的个人.h 文件:

#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H

#include <vector>
#include <stdlib.h>
#include <time.h> 
#include <iostream>


using namespace std;

class Individual{
    private:
        vector<unsigned int> chromosome;
        unsigned int n_genes;
        unsigned int N_colours_used = 0;
        unsigned int fitness = 0;

    public:
        Individual(unsigned int ngenes){};

};

#endif

这是我的个人.cpp 文件:

#include "individual.h"


Individual :: Individual(unsigned int ngenes){
    cout << "something" << endl;
}

错误看起来像这样

src/individual.cpp:4:1: error: redefinition of ‘Individual::Individual(unsigned int)’
Individual :: Individual(unsigned int ngenes){
 ^
In file included from src/individual.cpp:1:0:
include/individual.h:24:13: note: ‘Individual::Individual(unsigned int)’ previously defined here
             Individual(unsigned int ngenes){};

我尝试了stackoverflow中的所有内容,但我仍然不知道如何解决这个问题。还有
“#pragma once”不起作用。

【问题讨论】:

标签: c++ c++11 gcc makefile g++


【解决方案1】:
    Individual(unsigned int ngenes){};

如您所见,函数声明后有{ },这是一个空体的定义。

然后您尝试在.cpp 文件中重新定义函数体。删除{ }

【讨论】:

  • 非常感谢,我花了两个小时试图解决这个问题。
猜你喜欢
  • 2010-10-17
  • 2010-11-25
  • 2023-03-29
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2015-02-27
相关资源
最近更新 更多