expected class-name before ‘{’ token error - c++

I'm working on SFML here and I want to create my own RenderWindow class with Legacy. But the g++ prints this error: expected class-name before ‘{’ token at line 7 in .hpp
I all ready ask Google about it but I didn't find what I was expecting at so I hope you will be able to help me to find this out :)
my_render_window.hpp
#ifndef MY_RENDER_WINDOW_HPP_
# define MY_RENDER_WINDOW_HPP_
# include <SFML/Window.hpp>
class MyRenderWindow : public sf::RenderWindow
{
public:
MyRenderWindow();
protected:
};
#endif /* !MY_RENDER_WINDOW_HPP_ */
my_render_window.cpp
#include "my_render_window.hpp"
MyRenderWindow::MyRenderWindow(): sf::RenderWindow()
{
}
I compile correctly without these files and taking base sf::RenderWindow class to run with.

Okay, I dont know why but with this include #include <SFML/Graphics.hpp> just after the #include <SFML/Window.hpp> it works !
So weird... :(
#include <SFML/Graphics.hpp> may includes a file that I need to.

Related

c++ error: expected class-name before '{' token

screenshot of Pose.hpp
I am trying to catkin_make a simple package and I am getting the error
.../Pose.hpp:17:1: expected class-name before token...
.../Odometry.cpp:12:3: expected class-name before token...
The responsible Pose header file is sampled here as:
#ifndef POSE_HPP
#define POSE_HPP
#include <string>
#include <vector>
#include <ostream>
#include "ros/serialization.h"
#include "ros/builtin_message_traits.h"
#include "ros/message_operations.h"
#include "ros/message.h"
#include "ros/time.h"
namespace turtle
{//line 17
template <class ContainerAllocator>
struct Pose_ : public ros::Message
{
typedef Pose_<ContainerAllocator> Type;
}; // struct Pose
...
} // namespace turtle
while on the referred header file is referenced in odometry.cpp shown as
#include <geometry_msgs/TwistWithCovarianceStamped.h>
#include <tf/transform_datatypes.h>
#include <robot_localization_demo/odometry.hpp>
namespace robot_localization_demo {
TurtleOdometry::TurtleOdometry(ros::NodeHandle node_handle, double frequency):
node_handle_{node_handle},
turtle_pose_subscriber_{node_handle_.subscribe("turtle1/pose", 16, &TurtleOdometry::turtlePoseCallback, this)},
turtle_twist_publisher_{node_handle_.advertise<geometry_msgs::TwistWithCovarianceStamped>("turtle1/sensors/twist", 16)},
frequency_{frequency},
{//line 12
;
}
and odometry includes Pose as well.
what am I missing here?
The official documentation for ros::Message says:
This base-class is deprecated in favor of a template-based serialization and traits system.
In the official source code it looks like it's still defined, but in a contrib version I found, the whole class definition is removed by #if 0:
namespace ros {
#if 0
class Message {
//
};
#endif
}
So, you most probably need to find a different base class.

Clang++: weird error message

I try to compile a simple class, but I always get the same error:
≥ clang++ LIF_network.cpp -std=c++11
LIF_network.cpp:3:1: error: expected unqualified-id
public LIF_network::LIF_network(){
^
1 error generated.
.hpp:
#ifndef LIF_NETWORK_HPP
#define LIF_NETWORK_HPP
#include <vector>
#include <cstdlib>
#include "LIF_neuron.hpp"
#include "currentTimer.hpp"
#define MAX_TIME 15
class LIF_network{
public:
LIF_network();
};
#endif //LIF_NETWORK_HPP
.cpp:
#include "LIF_network.hpp"
public LIF_network::LIF_network(){
mNumNeurons = 0;
mNeurons = std::vector<std::vector<LIF_neuron>>();
mOutput = std::vector<double>();
mCurrentTimer = CurrentTimer(MAX_TIME, mNumNeurons);
}
The error message does not really help me. Can you spot my mistake? Thanks a lot!
The access specifier (public: private: and protected:) only works within a class definition and not outside of it. It looks like you are trying to port Java code.

My class isn't defined in the scope?

I'm just starting to learn C++ as an already-experienced programmer in several other languages. The issue I'm having is probably a very obvious error on my end.
I have a class called Test in its own file, and it also has the respective header file. However, when I try to create an instance of it in main, I get this error:
Error: Test was not declared in this scope.
Error: Expected ';' before 'to'
This is my main:
#include <iostream>
using namespace std;
int main()
{
Test to;
return 0;
}
This is the Header for Test:
#ifndef TEST_H
#define TEST_H
class Test
{
public:
Test();
};
#endif // TEST_H
This is the Test Class:
#include "Test.h"
#include <iostream>
using namespace std;
Test::Test()
{
}
As you can see it is a simple class with an empty constructor. I have no idea what I could possibly be doing wrong here, so any help is much appreciated.
Edit: Thanks for the help, I knew it would be something stupidly obvious :P
#include "Test.h"
in your main file also.
You forgot to include the header containing the Test class so the main can "see" it. Try this code instead:
#include <iostream>
#include "Test.h"
int main()
{
Test to;
return 0;
}

Expected class-name before ‘{’ token

I'm receiving the error:
In file included from proprietario.h:5,
from veiculo.h:4:
motocicleta.h:8: error: expected class-name before ‘{’ token
Motocicleta.h:
#ifndef __MOTOCICLETA__
#define __MOTOCICLETA__
#include <iostream>
#include "veiculo.h"
#include "proprietario.h"
using namespace std;
class Proprietario;
class Motocicleta:public Veiculo{
public:
Motocicleta(int nPassageiros, string modelo, string placa, int aFabricacao, Proprietario* pai, int nRodas, int aro);
~Motocicleta();
Motocicleta (const Motocicleta& source);
Motocicleta& operator= (const Motocicleta& source);
string toString();
};
#endif
Proprietario.h
#ifndef __PROPRIETARIO__
#define __PROPRIETARIO__
#include <iostream>
#include "motocicleta.h"
#include "caminhao.h"
#include "carreta.h"
#include "carro.h"
using namespace std;
class Carro;
class Carreta;
class Caminhao;
class Motocicleta;
class Proprietario{
protected:
string nome;
string cpf;
Motocicleta* mMoto;
Caminhao* mCaminhao;
Carreta* mCarreta;
Carro* mCarro;
};
Veiculo.h:
#ifndef __VEICULO__
#define __VEICULO__
#include <iostream>
#include "proprietario.h"
#include "roda.h"
#include "motor.h"
using namespace std;
class Motor;
class Proprietario;
class Veiculo{
protected:
int nPassageiros;
string modelo;
string placa;
int aFabricacao;
Proprietario* pai;
Roda* rodas;
Motor* mMotor;
int nRodas;
};
I removed the methods, because if i added those the question will be to long, sorry, the code is in PT-BR.
I saw that the problem is usually is forward declaration.
But i cannot find out the problem, i looked in so many forums but i cannot find out the problem..
Someone can help me?
Need any other part of the code?
The real problem here is the liberal use of #include preprocessor directives. In general, you should only include a header file at the lowest scope at which it is needed and forward-declare everything you can. You simply don't (shouldn't) generally need full class declarations for header files. Header files do not generally need to know about implementation details. You definitely shouldn't been forward-declaring and including the header.
As the code stands in the question at the time of writing this answer, you have a circular dependency on veiculo.h (and also on proprietario.h). As veiculo.h really is needed for the header of its subclass Motocicleta, you should remove the #include directives for the classes you have already forward-declared in each of the headers. You can then include the headers in the source files as needed.

Expected Class-name before { token

I am trying to figure out whey I get an error: expected class-name before { token
Here is the relative source to the error:
#pragma once
#ifndef H_FXACTION
#define H_FXACTION
#include "CP_M_RefCounted.h"
#include "FxTypes.h"
#include "string"
#include "FxString.h"
#include "FxPixels.h"
#include "CP_Rect.h"
#include "FxStreamable.h"
#include "FxPoint.h"
#include "FxPtr.h"
#include "FxImage.h"
#include "FxSleekStreaming.h"
typedef FxID FxActionType;
typedef FxUInt32 FxActionID;
FxActionID FrMakeUniqueActionID(void);
class FxActionData;
class FxActionData : public CP_M_RefCounted
{
public:
FxActionData();
FxActionData(FxBool final) :mFinal(final) { }
virtual ~FxActionData();
I get the error at this line: class FxActionData : public CP_M_RefCounted
What I dont get is why the line: class FxActionData; is there when you are creating the class directly under it. Isn't this a forward declaration?
What things could be going on here?
class FxActionData; is a *forward declaration. It doesn't hurt anything but allows for not dragging in a full header file for, say, just a pointer to a class. It's useless in your case here.
The CP_M_RefCounted is probably a template (or might be declared in a namespace). Look into what's in CP_M_RefCounted.h