I have a problem with my C++ code. If I insert #include "god.hpp" in neuron.hpp, g++ shows me the following error:
In file included from neuron.hpp:4,
from main.cpp:5:
god.hpp:11: error: ‘Neuron’ has not been declared
god.hpp:13: error: ‘Neuron’ was not declared in this scope
god.hpp:13: error: template argument 1 is invalid
god.hpp:13: error: template argument 2 is invalid
main.cpp: In function ‘int main()’:
main.cpp:36: error: no matching function for call to ‘God::regNeuron(Neuron*&)’
god.hpp:11: note: candidates are: long int God::regNeuron(int*)
In file included from god.hpp:5,
from god.cpp:3:
neuron.hpp:10: error: ‘God’ has not been declared
In file included from neuron.hpp:4,
from neuron.cpp:2:
god.hpp:11: error: ‘Neuron’ has not been declared
god.hpp:13: error: ‘Neuron’ was not declared in this scope
god.hpp:13: error: template argument 1 is invalid
god.hpp:13: error: template argument 2 is invalid
and here are the related (parts) of the necessary files:
//main.cpp
#include <string>
#include <vector>
#include "functions.hpp"
#include "neuron.hpp"
#include "god.hpp"
using namespace std;
int main()
{
God * god = new God();
vector<string>::iterator it;
for(it = patterns.begin(); it != patterns.end(); ++it) {
Neuron * n = new Neuron();
god->regNeuron(n);
delete n;
cout << *it << "\n";
}
}
The God ;) Who will handle all neurons...
//god.hpp
#ifndef GOD_HPP
#define GOD_HPP 1
#include <vector>
#include "neuron.hpp"
class God
{
public:
God();
long regNeuron(Neuron * n);
private:
std::vector<Neuron*> neurons;
};
#endif
//god.cpp
#include <iostream>
#include <vector>
#include "god.hpp"
#include "neuron.hpp"
using namespace std;
God::God()
{
vector<Neuron*> neurons;
}
long God::regNeuron(Neuron * n)
{
neurons.push_back(n);
cout << neurons.size() << "\n";
return neurons.size();
}
And at least, my Neuron.
//neuron.hpp
#ifndef NEURON_HPP
#define NEURON_HPP 1
#include "god.hpp" //Evil
class Neuron
{
public:
Neuron();
void setGod(God *g);
};
#endif
//neuron.cpp
#include <iostream>
#include "neuron.hpp"
#include "god.hpp"
Neuron::Neuron()
{
}
void Neuron::setGod(God *g)
{
std::cout << "Created Neuron!";
}
I hope someone can help me to find the error. It happens when I write #include "god.hpp" in neuron.hpp. I searched around three hours with Google, but I had no luck.
Kind regards
-Boris
Compiled with:
g++ -Wall -o getneurons main.cpp functions.cpp god.cpp neuron.cpp
Remove
#include "god.hpp"
and replace it with a forward declaration:
//neuron.hpp
#ifndef NEURON_HPP
#define NEURON_HPP 1
class God; //forward declaration
class Neuron
{
public:
Neuron();
void setGod(God *g);
};
#endif
Same for God.hpp:
//god.hpp
#ifndef GOD_HPP
#define GOD_HPP 1
#include <vector>
class Neuron; //forward declaration
class God
{
public:
God();
long regNeuron(Neuron * n);
private:
std::vector<Neuron*> neurons;
};
#endif
Note that you'll need the includes in your implementation files. (cpp files)
If you use pointers or references to objects as members or use that type as a return type or parameter, the full definition isn't required, so a forward declaration is enough.
Related
I am trying to understand the reason of the error I get while running main.cpp file.
I have two class (foo and gee) included in main.cpp file.
here is the error I get:
foo.cpp: In member function ‘void foo::save(gee&)’:
foo.cpp:13:10: error: invalid use of incomplete type ‘class gee’
13 | i->addfoo(this);
| ^~
foo.h:7:7: note: forward declaration of ‘class gee’
7 | class gee;
|
here is the declaration of foo class(foo.h):
#ifndef foo_H
#define foo_H
#include <set>
#include <string>
class gee;
class foo
{
public:
foo(std::string _str);
void save(gee& geeobj);
private:
std::string str;
std::set<gee*> geelist;
};
#endif
here is the definition of foo class(foo.cpp):
#include "foo.h"
foo::foo(std::string _str)
{
str = _str;
}
void foo::save(gee& geeobj)
{
geelist.insert(&geeobj);
for(auto i:geelist)
{
i->addfoo(this);
// std::cout << "how to run i->addfoo(this); \n"; // this line can be run.
}
return;
}
here is the declaration of gee class(gee.h):
#ifndef gee_H
#define gee_H
#include <set>
#include <string>
class foo;
class gee
{
public:
gee(std::string _name);
void addfoo(foo& _foo_obj);
private:
std::string name;
std::set<foo*> foolist;
};
#endif
here is the definition of gee class(gee.cpp)
#include "gee.h"
gee::gee(std::string _name)
{
name = _name;
}
void gee::addfoo(foo& _foo_obj)
{
foolist.insert(&_foo_obj);
return;
}
and here is the main file(main.cpp)
#include<iostream>
#include "gee.h"
#include "foo.h"
int main()
{
gee gobj("geename");
foo fobj("fooname");
fobj.save(gobj);
return 0;
}
I am also trying to understand the dependency of these files and why I can't execute main.cpp like is it the linker problem can't find the addfoo function body.
I thank you if you can give me in addition of the solution, an explanation of why we can't use this.
I am using g++ as compiler and to execute the main.cpp I just type these two line.
g++ gee.h foo.h
g++ main.cpp gee.cpp foo.cpp -o a
I'm getting the following error when compiling, using GCC 11.2.0:
/src/SearchController.h:12:23: error: ‘HelpModel’ was not declared in this scope
12 | std::optional<HelpModel> searchModel;
| ^~~~~~~~~
/src/SearchController.h:12:32: error: template argument 1 is invalid
12 | std::optional<HelpModel> searchModel;
| ^
I'm including the HelpModel class in the header, but this is pretty much my first C++ program so my understanding of this is pretty thin at the min.
Here's the SearchControlle.h file
#ifndef ARCH_HELP_SEARCH_CONTROLLER
#define ARCH_HELP_SEARCH_CONTROLLER
#include <string>
#include <optional>
#include "HelpModel.h"
class SearchController
{
public:
std::optional<HelpModel> searchModel;
void searchedFor(std::string searchTerm);
};
#endif
And here's the HelpModel.h file:
#ifndef ARCH_HELP_HELP_MODEL
#define ARCH_HELP_HELP_MODEL
#include <vector>
#include "Topic.h"
#include "TerminalView.h"
class HelpModel
{
public:
HelpModel(TerminalView view);
private:
TerminalView view;
std::vector<Topic> topics;
void getTopics();
void pushToView();
};
#endif
Here's TerminalView.h
#ifndef ARCH_HELP_TERMINAL_VIEW
#define ARCH_HELP_TERMINAL_VIEW
#include <vector>
#include <string>
#include "SearchController.h"
#include "Topic.h"
class TerminalView
{
public:
void makeHeader();
void update(std::vector<Topic> modelData);
private:
std::string searchTerm;
std::vector<Topic> helpData;
SearchController controller;
void makeSearchInput();
void printToTerminal();
void printAnswers(std::vector<std::string> answers);
};
#endif
What I would like to be able to do is then assign an instance of HelpModel to the SearchController like so - say in main.cpp:
HelpModel model(terminal);
SearchController controller;
controller.searcModel = model;
Any advice greatly appreciated
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 { }.
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.
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.