Expected initializer before * token - c++

So I have this header file:
#pragma once
#include "engine.hpp"
namespace spacecubes
{
extern engine* _engine;
}
and the included engine.hpp:
#pragma once
#include <iostream>
#include "glinclude.hpp"
#include "debug.hpp"
#include "convert.hpp"
#include "renderer.hpp"
#include "global.hpp"
namespace spacecubes {
void display();
class engine {
renderer renderengine;
public:
void start(int argc, char* argv[]);
void stop(int status = 0);
void poll();
renderer getRenderEngine() {return renderengine;}
};
}
What the compiler reported later on was:
g++ -c -o bin/obj/engine.o src/engine.cpp
In file included from src/engine.hpp:9,
from src/engine.cpp:1:
src/global.hpp:7: error: expected initializer before '*' token
src/engine.cpp: In function 'void spacecubes::display()':
src/engine.cpp:5: error: '_engine' was not declared in this scope
I don't get it. What did it mean that it expected an init? Thanks in advance!

Replace
#include "engine.hpp"
with a forward declaration:
namespace spacecubes { class engine; }

Related

how to solve Invalide use of incomplete type in c++ error

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

C++ Compile error:'HelpModel' was not declared in this scope - using an optional

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

Template function is not a member of a namespace

I have a header file, RandFunctions.hpp which contains a template function,
#ifndef _RANDFUNCTIONS_HPP_
#define _RANDFUNCTIONS_HPP_
#include <stdlib.h>
#include <time.h>
namespace surena
{
namespace common
{
template<typename RealT> inline
RealT
RealRandom()
{
return rand()/(RealT(RAND_MAX)+1);
}
};
};
#endif
and another header file, Search.hpp which includes RandFunctions.hpp,
#ifndef _SEARCH_HPP_
#define _SEARCH_HPP_
#include "RandFunctions.hpp"
#include <stdlib.h>
#include <time.h>
namespace surena
{
namespace search
{
template<typename RealT>
class CTest
{
public:
CTest() {srand((unsigned)(time(0)));}
RealT
GenRand(){ return common::RealRandom(); }
};
};
};
#endif
when I include Search.hpp in a cpp file, for example,
#include "Search.hpp"
int
main(int argc, char** argv)
{
CTest<float> test;
return(0);
}
I get the following compile time error:
‘RealRandom’ is not a member of ‘surena::common’
What is wrong here?
Since RealRandom is a template function with no parameters, you need to provide a template argument:
GenRand(){ return common::RealRandom<RealT>(); }
^^^^^^^
Also in your main you'd have to qualify your test variable with the proper namespaces:
surena::search::CTest<float> test;
^^^^^^^^^^^^^^^^

variable was not declare in the scope class in private can not be access by member function

//This is the header file (header.h)
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
~
~
//This is the cpp file (program.cpp)
#include "header.h"
#include <cstring>
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
I'm getting program.cpp:13: error: 'w' was not declared in this scope
I'm trying to just do the strcpy from data which contain some info to w which is from the private section of the class and using the member function to access them.
I'm not sure if I forgot anything and why I can't access them.
Thanks to the last answer from Sergey Vakulenko
The sequence of the header file is very important.
It should be
#include <cstring>
#include "header.h"
not
#include "header.h"
#include <cstring>
add these headers to your cpp file:
#include <stdio.h>
#include <string.h>
#include "nameofheader.h"
Edit (more full explication ):
for me, that exemple not give any error:
1.h:
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
1.cpp:
#include <stdio.h>
#include <string.h>
#include "1.h"
//This is the cpp file (program.cpp)
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
int main (int argc, char** argv) {
return 0;
}
compled with g++:
g++ 1.cpp -o 1
Your program, the way you are showing it to us here, should compile without problems:
ideone.com/Bj6VU
If you want more help, you should make the all of the two files you are compiling (program.cpp and header.h) available.

G++ Error: In file included, then Foo was not declared

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.