I know there are similar questions but none of these work in my case. Hi I cannot find why I have this issue.
Here is my individual.h file:
#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
And this is my individual.cpp file:
#include "individual.h"
Individual :: Individual(unsigned int ngenes){
cout << "something" << endl;
}
The error looks like this
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){};
I tried everything thats in stackoverflow but I still don't know how to solve this problem. Also
"#pragma once" does not work.
Individual(unsigned int ngenes){};
As you can see you have { } after your function declaration, which is a definition of an empty body.
Then you are trying to redefine the body of the function in the .cpp file. Remove { }.
Related
Im having trouble with a multi-file setup. Im working in visual studio, and, for whatever reason, my friend function in my class is not being defined in main. Any help would be appreciated, thanks.
BullCow.h:
#pragma once
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
class BullCow {
public:
BullCow();
friend int getWins();
static int Wins;
private:
int Attempts;
};
BullCow.cpp:
#include "stdafx.h"
#include "BullCow.h"
int BullCow::Wins = 0;
int getWins() {
return Wins;
}
BullCowMain.cpp:
#include "stdafx.h"
#include "BullCow.h"
int main()
{
srand(time(NULL));
std::cout << getWins();
return 0;
}
Note: It's an incomplete program, so some code (srand) is not used yet. I just included everything to better help figure out what's wrong.
getWins() needs at least a declaration in the .h file.
Since it's a friend, getWins() is not a member of the class, so it must be declared either directly in BullCowMain.cpp or in some file BullCowMain.cpp includes.
Add this somewhere outside of the class in your header:
int getWins();
Also, inside getwins, the return should be:
return BullCow::Wins;
Thanks #user4581301!
I have carefully looked into my code but don't see why this error comes out.
The error message is the following:
main.cc: In function ‘int main()’:
main.cc:12: error: conflicting declaration ‘traj dim’
main.cc:11: error: ‘dim’ has a previous declaration as ‘unsigned int dim’
and one can reproduce it with the following command
g++ -o a.out realvector.cc traj.cc main.cc
My main.cc is
#include "realvector.h"
#include "traj.h"
using namespace std;
int main() {
unsigned int dim=1000;
traj TRAJ(dim);
return 1;
}
traj is defined in traj.h as
#ifndef TRAJ
#define TRAJ
#include "realvector.h"
class traj{
public:
traj(unsigned int);
~traj();
void next(double &);
private:
unsigned int it,nt; // index, total array size
double dt; // step time
RealVector r,v,a;
};
#endif
the constructor is defined in traj.cc
#include "realvector.h"
#include "traj.h"
traj::traj(unsigned int dim) : nt(dim) {
RealVector r(nt),v(nt),a(nt);
it=0;
}
traj::~traj(){
r.~RealVector();
}
Any idea why this error comes out? Also, is the way to define r,v,a correct? RealVector is a home-defined class with its constructors defined as the following
#include "realvector.h"
using namespace std;
RealVector::RealVector() {}
RealVector::RealVector(unsigned int n)
: dim(n) {
data = new double[dim];
for (int i=0; i<dim; i++)
data[i]=0;
}
RealVector::~RealVector(){
delete[] data;
}
with realvector.h as
#ifndef REAL_VECTOR_H
#define REAL_VECTOR_H
#include <iostream>
class RealVector {
public:
RealVector();
RealVector(unsigned int n);
~RealVector();
int dim;
double* data;
};
#endif
The code is not complete... as a wild guess you also have a TRAJ macro that makes reading what the code really is impossible.
In traj.h you have
#define TRAJ
which defines TRAJ as an empty "string" and this leads to this replace by the preprocessor:
traj TRAJ(dim);
to
traj (dim);
which produces the error message.
I guess you should rename TRAJ in the include file to TRAJ_H and then it works.
Here is my main.cpp:
#include <iostream>
#include "function.cpp"
using namespace std;
extern int giveMain();
int main() {
int x = 4;
x = giveMain(x);
cout << x << endl;
}
And here is my function.cpp:
#include <iostream>
using namespace std;
int giveMain(int a) {
a = 3 + a;
return a;
}
But when I compile, it says that "Linker command failed". Can anyone helps me to solve this problem.
You declared the function int giveMain() in main.cpp but the function in function.cpp takes an int. Declare the correct function and it should work. Also extern is the default for functions so you don't need to include the keyword.
EDIT: Just noticed that you #include <function.cpp> in main.cpp. Never include .cpp files. The issue you were having was multiple definitions for int giveMain(int) because the contents of functions.cpp was being compiled twice.
I have the following files in the same project.
Don't bother reading all the blocks of code if you think it's not necessary,
the error messages appear only in the ship.cpp
main.cpp
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "chart.cpp"
#define N 10
using namespace std;
int main()
{
int i,j, flagi=-3, flagj=-3;
int test, ports_count=0, treas_count=0;
chart ***mapp;
mapp=new chart **[N];
for(i=0;i<N;i++){mapp[i]=new chart *[N];}
/*missing code initilazing chart ***mapp */
return 0;
}
chart.cpp
#include <iostream>
#include "ship.cpp"
using namespace std;
class chart{
bool isPort;
int weather;
int treasure;
ship* shipPtr;
public:
chart(){isPort=false; weather=0; treasure=0; shipPtr=NULL;}
bool getPort(){return isPort;}
int getWeather(){return weather;}
int getTreasure(){return treasure;}
ship* getShip(){return shipPtr;}
void setPort(bool port){isPort=port;}
void setWeather(int weath){weather=weath;}
void setTreasure(int treas){treasure=treas;}
void setShip(ship* shp){shipPtr=shp;}
};
and
ship.cpp
#include <iostream>
#define N 10
using namespace std;
class ship{
protected:
string name;
int maxhp, curhp, speed, curtreas, x_pos, y_pos;
public:
friend class chart;
//the line above gives error message " forward declaration of 'struct chart' "
static int shipcount;
ship(){shipcount++;}
string getName(){return name;}
int getMaxhp(){return maxhp;}
int getCurhp(){return curhp;}
int getSpeed(){return speed;}
int getCurtreas(){return curtreas;}
int getX_pos(){return x_pos;}
int getY_pos(){return y_pos;}
bool Move(chart ***mapp){
int x, y, blocked=0;
for(x=x_pos-1;x<=x_pos+1;x++){
if((x>-1) && (x<N)){
for(y=y_pos-1;y<=y_pos+1;y++){
if((y>-1) && (y<N)){
/* the line below gives error message "invalid use of undefined type 'struct chart'"*/
if((!mapp[x][y]->getPort) && (mapp[x][y]->getShip==NULL)){
blocked++;
}
}
}
}
}
if(blocked<2){
return false;
}
/* missing the rest of the body of bool Move cause it is too big */
}
}
The compiler gives the following error messages:
"invalid use of undefined type 'struct chart' " in ship.cpp -> line 39
"forward declaration of 'struct chart' " in ship.cpp -> line 12
Why are these errors showing up?
I know the code is probably complex but any help would be appreciated.
Thank you for your time.
The reason this code does not compile is that your ship.cpp file needs a definition of chart class in order to use its members. You fail to provide this definition, prompting the compiler to complain.
Since all of the members of class chart are defined in the class declaration, you can rename chart.cpp to chart.h, and add an #include for it in your ship.cpp file:
#include <iostream>
#include "chart.h"
#define N 10
... // The rest of ship.cpp code
Also replace the name chart.cpp in your main with chart.h.
So I am confused. I am getting a redefinition error when trying to implement a method previously declared in a header file. I added include guards to the header and still got the same error. If someone could explain to me what I am not seeing, that would be fantastic.
In file included from main.cpp:2:
./thing.cpp:7:12: error: redefinition of 'method'
int Thing::method(void)
^
./thing.hpp:12:6: note: previous definition is here
int method(void) {};
^
--EDIT--
I am now getting the following:
duplicate symbol __ZN5Thing6methodEv in:
main.o
thing.o ld: 1 duplicate symbol for architecture x86_64
thing.hpp:
#ifndef THING_H
#define THING_H
class Thing {
public:
int a;
int b;
char c;
int method(void) {};
Thing(int anA, int aB, char aC): a(anA), b(aB), c(aC) {};
};
#endif
thing.cpp
#include <iostream>
#include <stdio.h>
#include "thing.hpp"
using namespace std;
int Thing::method(void)
{
return 5;
}
main.cpp
#include <iostream>
#include "thing.cpp"
using namespace std;
Thing* thing = new Thing(5,5,'c');
int main(int argc, char ** argv)
{
cout << thing->method() <<endl;
}
In your header file you have:
int method(void) {};
That's an inline definition, not a declaration. The {} is actually providing the (albeit empty) function body to the compiler. Remove the {} if you want to define the function in another file.
Additionally, you have #include "thing.cpp" rather than #include "thing.hpp" at the top of your main.cpp file.
In main.cpp you need to change:
#include "thing.cpp"
to:
#include "thing.hpp"