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

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.

Related

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.

compiler reports: error: expected unqualified-id before ‘using’

I have two files, homework1.cc and homework1.h.
The top of homework1.cc is
#include <iostream>
#include <fstream>
#include <cstring>
#include <GL/glut.h>
#include "homework1.h"
using namespace std;
and the top of in homework1.h is
#ifndef _rt_H
#define _rt_H
#include <cmath>
#include <vector>
#include <list>
#include <GL/glut.h>
using namespace std;
This is where I use list in homework1.h
class surTriangle
{
float x;
float y;
float z;
list <int> surTriangles;
bool compareVertex(Vector3f anotherVec)
{
if(anotherVec[0] == x && anotherVec[1] == y && anotherVec[2] == z)
return true;
return false;
}
}
When I attempt to compile homework.cc, the compiler reports
homework1.cc:8: error: expected unqualified-id before ‘using’
However when I delete #include <list> or the third piece of code above it compiles successfully.
Would you please help me to find out what's the problem is?
You need a semicolon after class declaration. (the last line of the class fragment of homework1.h). Otherwise using is interpreted as identifier as in
class X {
} x;
You forgot ; at the end of your class definition.
When debugging parse errors, it's useful to consider how the preprocessor works; in your mind (or with gcc -E if you want to actually view it properly) imagine what the code looks like after #include directives have been resolved.
You'd then see that the error falls directly after your class definition, which narrows things down tremendously. The error says that using is "unexpected", which typically indicates an issue terminating/concluding the previous statement. This leads to spotting the missing semicolon, which is a classic typo.

Compile errors when trying to use list in C++

I'm trying to use list in c++, but I get the following error:
1>error C2143: syntax error : missing ';' before '<'
1>error C4430: missing type specifier int assumed. Note: C++ does not support default-int
1>error C2238: unexpected token(s) preceding ';'
With the following code:
#pragma once
#include "Includes.h"
class Polygon
{
public:
Polygon(void);
~Polygon(void);
void addVertice(hgeVector v);
void renderPolygon();
list<hgeVector> vertices;
};
Includes.h:
#ifndef INCLUDES
#define INCLUDES
#define safe_delete(d) if(d) { delete d; d=0; }
#define PI 3.14159
#include <stdio.h>
#include <list>
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
#include "Car.h"
#include "HelperFunctions.h"
#include "config.h"
#include "Polygon.h"
using namespace std;
#endif
Just some general comments...
#define PI 3.14159
Please use M_PI in math.h, which is 3.141592653589793238462643.
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
You should use forward slashes / here, and remove the leading \ before the include.
using namespace std;
Avoid this in header file. This will pollute all other users' global namespace. (Therefore, you should use std::list<hgeVector> vertices; in Polygon.h.)
The issue could be that the line list<hgeVector> vertices is being processed before using namespace std;, and so your compiler does not know what a list (without the std:: namespace qualifier) is. It's not clear to me in exactly what order these statements get processed since your two files include each other, and I don't know precisely how the non-standard #pragma once will handle this.
In any case, try qualifying list<hgeVector> as std::list<hgeVector>
Edit: Assuming #pragma once works just like include guards, then this problem will occur if some other file inlcudes includes.h, but not if some other file includes Polygon.h. If another file includes includes.h, what happens is that includes.h reaches #include <Polygon.h>, and the compiler starts processing Polygon.h. But then when #include <includes.h> is reached inside Polygon.h, nothing is effectively included since the INCLUDES guard is already defined, so you don't get the using namespace std; line before the compiler continues processing the rest of Polygons.h.
In general, try to avoid circular inclusion, and prefer forward-declarations.
I think you have circular "includes". You are including Includes.h in Polygon.h and including Polygon.h in Includes.h.
class template need a full type declaration to instantiate itself. Make sure you have included the header file where hgeVector is declared.
BTW, you have 'using namespace std‘ in your header - this is not a good practice. It will introduce unnecessary names to the current namespace.
Make sure hgeVector is defined.
You may have redefined list somewhere. Try using std::list.
Try something very simple like std::list<int>.
The answer (as Tyler McHenry pointed out) was circular inclusion!
After sorting out my includes I ended up with a compiled code like this (even without std:: infront of list:
#pragma once
#include <list>
#include "D:\Programmering\haffes\hge181\include\hge.h"
#include "D:\Programmering\haffes\hge181\include\hgevector.h"
using namespace std;
using namespace std;
class MyPolygon
{
public:
MyPolygon(void);
~MyPolygon(void);
void addVertice(hgeVector v);
void renderPolygon();
void setHotSpot(hgeVector v);
void translate(hgeVector v);
private:
list<hgeVector> vertices;
hgeVector hotSpot;
bool hotSpotUndef;
};
Thanks a bunch for all the fast and good answers!

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

"Expected constructor, destructor, or type conversion before '<' token"

I'm running into a syntax/parsing error, but I can't seem to locate it.
DataReader.h:11: error: expected constructor, destructor, or type conversion before '<' token
Here is DataReader.h:
#include <fstream>
#include <iostream>
#include <vector>
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
vector<Data*> DataReader(); // This is line 11, where the error is..
And this is the .cpp file:
#include "DataReader.h"
using namespace std;
vector<Data*> DataReader()
{
.....
}
I skipped the content of DataReader() because I think it's irrelevant, but I can post it if needed.
Thanks for any input/suggestions.
In your header file, you need to explicitly use std::vector rather than just vector.
Also, I'm guessing that "Data.h" contains statements of the form:
#ifndef DATA_H
#define DATA_H
...
#endif
That's fine, but you should not use these include guards across #include "Data.h" as well, only within the file itself.
In your header file you need to use std::vector and not plain vector in the declaration of the function DataReader.
The standard include <vector> causes the vector class template to be defined in the std namespace and the declaration in your header file happens before any using namespace std; or using std::vector;.
I think in your header you probably need to write std::vector<Data*> DataReader(); as the using namespace std; is not in scope.
Use std:vector and not vector before Datareader.