newer gcc gives illegal syntax error in a header file - c++

I've installed the mimetic library according to the INSTALL instructions.
the following main file compiles without a problem with a gcc-c++ 4.1.2,
but when I upgrade to gcc-c++ 4.4.7 I get an error.
mimetic.cpp:
#include <iostream>
#include <mimetic.h>
using namespace std;
using namespace mimetic;
int main()
{
MimeEntity me;
return 0;
}
error
In file included from /usr/local/include/mimetic/rfc822/header.h:18,
from /usr/local/include/mimetic/header.h:11,
from /usr/local/include/mimetic/mimetic.h:18,
from mimetic.cpp:2:
/usr/local/include/mimetic/rfc822/messageid.h:29: error: expected ‘)’ before ‘thread_id’
the header file:
rfc822/messageid.h
#ifndef _MIMETIC_MESSAGEID_H_
#define _MIMETIC_MESSAGEID_H_
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <string>
#include <mimetic/libconfig.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <mimetic/utils.h>
#include <mimetic/os/utils.h>
#include <mimetic/rfc822/fieldvalue.h>
namespace mimetic
{
/// Message-ID field value
/// On Win32 Winsock library must be initialized before using this class.
struct MessageId: public FieldValue
{
MessageId(uint32_t thread_id = 0 ); // <------ line 29
MessageId(const std::string&);
std::string str() const;
void set(const std::string&);
protected:
FieldValue* clone() const;
private:
static unsigned int ms_sequence_number;
std::string m_msgid;
};
}
#endif
is there some compatibility switch for the gcc ?

So, the problem turns out to be poor configuration of the system, which leaves HAVE_STDINT_H not configured, and thus the uint32_t doesn't get defined, and the error occurs.

Related

Struct included in header throws incomplete type when instantiated locally but not when set as return value or vector

Ok, so this problem has been bugging me for a while. I'm creating a c++ program with the libpcap and libnet libraries. It will scan a given country for open ports by SYN'ing with libnet and checking for replies with libpcap. I have a main.cpp file that includes network.h which includes the source files necessary for generating the IPv4 ranges (regex.cpp) and for scanning them (libnet.cpp). Now, my network.h file contains the definition for struct range which is just two uint32_t's representing the starting address of the range and the end.
network.h:
#ifndef NETWORK_H
#define NETWORK_H
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <regex>
#include <fstream>
#include <libnet.h>
#include <pcap.h>
#include "src/regex.cpp"
#include "src/libnet.cpp"
#include "src/libpcap.cpp"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_RED "\x1b[31m"
#define COLOR_RESET "\x1b[0m"
#define SNAP_LEN 1518
typedef struct range {
uint32_t begin;
uint32_t end;
} range;
#endif
And I use struct range as the return type for getRangeByCountry
#include "../network.h"
std::vector<struct range> getRangeByCountry(const char* country){
std::vector<struct range> ranges;
try {
std::ifstream f("csv/geoip.csv");
std::regex re("\"\\d+.\\d+.\\d+.\\d+\",\"\\d+.\\d+.\\d+.\\d+\",\"(\\d+)\",\"(\\d+)\",\"\\w$
while (1){
std::string line;
std::getline(f, line);
std::smatch m;
if(line == ""){break;}
std::regex_match(line, m, re);
if (m[3] == country){
struct range r;
r.begin = std::stoul(std::string(m[1]), NULL, 0);
r.end = std::stoul(std::string(m[2]), NULL, 0);
ranges.push_back(r);
}
}
} catch (...){return ranges;}
return std::move(ranges);
}
But when I compile I get this error:
src/regex.cpp: In function ‘std::vector<range> getRangeByCountry(const
char*)’:
src/regex.cpp:15:17: error: aggregate ‘range r’ has incomplete type and
cannot be defined
struct range r;
Why doesn't the compiler throw an error when I set declare a vector of struct range locally? Or when I set the same as the return type? When I define the struct in the same file as getRangebyCountry() it compiles without error. Is this a problem with my g++ parameters? Here they are just in case: g++ main.cpp -lpcap -lnet --std=c++11
Fixed the problem. #Walter and #IgorTandetnik were right. In network I included a definition for getRangeByCountry() and removed the .cpp includes like so:
#ifndef _NETWORK_H
#define _NETWORK_H
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <regex>
#include <fstream>
#include <libnet.h>
#include <pcap.h>
#define COLOR_GREEN "\x1b[32m"
#define COLOR_RED "\x1b[31m"
#define COLOR_RESET "\x1b[0m"
#define SNAP_LEN 1518
std::vector<struct range> getRangeByCountry(const char*);
typedef struct range {
uint32_t begin;
uint32_t end;
} range;
Then I compiled regex.cpp separately and included it in my compilation of main.cpp like so: g++ main.cpp src/regex.o
But I'm still really shakey on the concept. If I want to include other cpp source files, do I need to compile them separately then include their definition in a header file and their actual definition in a .o when I compile main.cpp?

Parse issue expected ')' C++ compile error

I don't see what's wrong with the following code. WordStore.cxx is defined similarly to have an empty function body. The compiler is complaining that "expected ')'" in the semstore.h function definition. I'm using XCode.
Incidentally, I'm upgrading some ancient (10+ year-old) code to compile on a modern C++ compiler.
/* WordStore.h */
#ifndef WORD_STORE_H
#define WORD_STORE_H
class WordStore
{
public:
WordStore();
};
#endif
// semclass.h
#ifndef SEMCLASS_H
#define SEMCLASS_H
#include <iostream>
using namespace std;
void ReadSemRules(std::istream& stream, WordStore& ws);
#endif
// semclass.cxx
#include <iostream>
#include <string.h>
#include "WordStore.h"
#include "SemClass.h"
using namespace std;
void ReadSemRules(istream& stream, WordStore& ws)
{
}
You have stray unprintable character in your program between the m and the &:
https://godbolt.org/g/gAAoGn
void ReadSemRules(std::istream& stream, WordStore& ws);
^^

C++: Warning inconsistent dll linkage

I have an issue with Visual studio (in C++)
I got a warning and I don't know why because I never call 2 time the same variable.
function: inconsistent dll linkage
warning list: (in french)
I read on microsoft: Compiler Warning (level 1) C4273 but I don't really know if it's my problem because the example is not like mine.
I read also About inconsistent dll linkage (StackOverflow and it tell me that because I use MFC in dll but I didn't check the header MFC.
it's my "PayRespectdll.h"
#pragma once
#ifdef PAYRESPECTDLL_EXPORTS
#define PAYRESPECTDLL_API __declspec(dllexport)
#else
#define PAYRESPECTDLL_API __declspec(dllimport)
#endif
#include <ctime>
#include <time.h>
#include <string>
namespace PayRespectDLL
{
class PayRespect
{
private:
static struct std::tm when;
public:
static PAYRESPECTDLL_API bool is_setup();
static PAYRESPECTDLL_API void setup(std::string date);
static PAYRESPECTDLL_API bool is_possible();
}
}
PayRespectDLL.cpp:
// PayRespectDLL.cpp :
//
#include "stdafx.h"
#include "PayRespectDLL.h"
#include <stdexcept>
#include <time.h>
#include <string>
#include <stdlib.h>
using namespace std;
namespace PayRespectdll
{
bool PayRespect::is_setup()
{
return false;//already_setup;
}
// setup attempt String: hh:mm:ss.
void PayRespect::setup(string date)
{
return;
}
bool PayRespect::is_possible()
{
return true;
}
}
thank you!

odb/pgsql/version.hxx no such file or directory

I'm trying to learn how to use C++ and ODB following this tutorial:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2
I've created a Person.hxx file where there is the declaration of class Person as persistent, then I've got thre files Person-odb: .cxx, .hxx, .ixx
Now I should compile Person-odb.cxx with
g++ -I/usr/lib/odb/i686-linux-gnu/include Person-odb.cxx
but it end with:
fatal error: odb/pgsql/version.hxx: No such file or directory. compilation terminated.
I see that there is a file version.hxx but there's no odb/pgsql directory...
what's wrong?
this is Person.hxx where I have defined the persistent class Person:
#ifndef PERSON_HXX
#define PERSON_HXX
#include <string>
#include <odb/core.hxx>
using namespace std;
#pragma db object
class Person {
private:
Person() {
}
friend class odb::access;
#pragma db id auto
unsigned long id_;
std::string email_;
std::string first_;
std::string last_;
unsigned short age_;
public:
Person(const std::string& first, const std::string& last,
unsigned short age);
/* getters */
const std::string& first() const;
const std::string& last() const;
unsigned short age() const;
const std::string& email() const;
/* setters */
void setAge(unsigned short);
void setFirst(const std::string&);
void setLast(const std::string&);
void setEmail(const std::string&);
};
#endif
then I must compile Person.hxx with odb compiler:
odb -d mysql --generate-query --generate-schema Person.hxx
and I get 4 files Person.odb.hxx, .cxx, .sql, .ixx
this is driver.cxx where I have the main program which persists objects:
#include <memory>
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/mysql/database.hxx>
#include "Person.hxx"
#include "Person-odb.hxx"
using namespace std;
using namespace odb;
int main(int argc, char* argv[]) {
try {
auto_ptr<database> db (new odb::mysql::database (argc, argv));
unsigned long marcoID, loryID, lucaID;
/*Create some persistent Person objects */
Person marco ("Marco", "Di Nicola", 26);
Person luca ("Luca", "La Sala", 22);
Person lory ("Lorenzo", "Vinci", 24);
transaction t (db->begin());
marcoID = db->persist(marco);
lucaID = db->persist(luca);
loryID = db->persist(lory);
t.commit();
} catch (const odb::exception& e) {
cerr << e.what() << endl;
return 1;
}
}
and this is the file Person-odb.hxx
// This file was generated by ODB, object-relational mapping (ORM)
// compiler for C++.
//
#ifndef PERSON_ODB_HXX
#define PERSON_ODB_HXX
#include <odb/version.hxx>
#if (ODB_VERSION != 20200UL)
#error ODB runtime version mismatch
#endif
#include <odb/pre.hxx>
#include "Person.hxx"
#include <memory>
#include <cstddef>
#include <odb/core.hxx>
#include <odb/traits.hxx>
#include <odb/callback.hxx>
#include <odb/wrapper-traits.hxx>
#include <odb/pointer-traits.hxx>
#include <odb/container-traits.hxx>
#include <odb/no-op-cache-traits.hxx>
#include <odb/result.hxx>
#include <odb/simple-object-result.hxx>
#include <odb/details/unused.hxx>
#include <odb/details/shared-ptr.hxx>
namespace odb
{
// Person
template <>
struct class_traits< ::Person >
{
static const class_kind kind = class_object;
};
template <>
class access::object_traits< ::Person >
{
...
#include "Person-odb.ixx"
#include <odb/post.hxx>
#endif // PERSON_ODB_HXX
everything seems to work fine when I perform:
c++ -c Person-odb.cxx
c++ -c driver.cxx
but in the end when I have to link all together with:
c++ -o driver driver.o Person-odb.o -lodb-mysql -lodb
I get:
"undefined reference to `Person::Person(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, unsigned short)'"
What seems to be your problem is that you didn't install ODB. The Installing ODB page should get you up and running.
You see a version.hxx file, but that's an input file for the ODB compilation process, not for including in your programs.
If you did install it, find out where on your system and add that folder to your compiler include path. Your compilation command then becomes
g++ -I/usr/lib/odb/i686-linux-gnu/include -I/path/to/odb/install/include Person-odb.cxx
Following your edits, I think the issue is that you're not linking to the Person object file, only the Person-odb one.
Compile Person.cxx with
g++ -c Person.cxx
and change your linking command to
g++ -o driver driver.o Person.o Person-odb.o -lodb-mysql -lodb
and the error should be fixed.
Please note that I don't know ODB. I'm trying to figure this out from a pure C++ perspective.

boost compile error Mac OS X 10.7.4

I want to use boost on my Mac.
Mac OS X 10.7.4
Xcode 4.5
I installed boost by homebrew.
brew install boost
Boost version is 1.49.0.
And I set up my Xcode project.
Add Header search path.
/usr/local/Cellar/boost/1.49.0/include
Add libraries
libboost * .dylib
When I compile my project, I have many errors. Clang LLVM 1.0 Error.
( I want to upload an image, but I can't upload becase I don't have more than 10 reputation. )
use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?
use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?
too few template arguments for class template '__is_function'
use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?
field has incomplete type 'std::exception_ptr'
expected ';' at end of declaration list
no member named 'memcpy' in namespace 'std::__1'; did you mean 'memcpy'?
no member named 'memcpy' in namespace 'std::__1'; did you mean 'memcpy'?
expected ';' at end of declaration list
expected ';' at end of declaration list
C++ requires a type specifier for all declarations
'operator==' cannot be the name of a variable or data member
use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?
expected ';' at end of declaration
expected unqualified-id
expected ';' at end of declaration list
unknown type name 'nullptr_t'
unknown type name 'nullptr_t'
qualified reference to 'shared_ptr' is a constructor name rather than a type wherever a constructor can be declared
too many errors emitted, stopping now
I checked this question.
XCode with boost "Semantic Issue - undeclared identifier va_start"
But I couldn't find answers.
Please let me know if there is a solution.
The header file using boost is like this.
GLTexture.hpp
#pragma once
#ifndef __GL_TEXTURE_HPP__
#define __GL_TEXTURE_HPP__
// Standard libraries.
#include <iostream>
#include <fstream>
#include <string>
#ifdef _WIN32
#pragma warning (push)
#pragma warning (disable:4819)
#endif
// Smart pointers.
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_array.hpp>
// Foreach macros.
#include <boost/foreach.hpp>
// Regular expression.
#include <boost/regex.hpp>
// Image I/O.
#define png_infopp_NULL (png_infopp)NULL
#define int_p_NULL (int*)NULL
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/jpeg_io.hpp>
#include <boost/gil/extension/io/png_io.hpp>
// File operations,
#include <boost/filesystem.hpp>
// Linear algebra,
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
// String functions.
#include <boost/algorithm/string.hpp>
// Serialization
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/version.hpp>
#ifdef _WIN32
#pragma warning (pop)
#endif
// OpenGL and OpenGL Utility Toolkit
#ifdef _WIN32
#include <GL/glew.h>
#include <GL/GLUT.h>
#elif defined __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#endif
#include "TextureManager.hpp"
#include <time.h>
class TextureManager;
using namespace std;
/// function to convert RGB image to RGBA image.
/// Filling its alpha channel with 255.
/// This function is called from boost::gil::copy_and_convert_pixels()
namespace boost {
namespace gil {
template <> void color_convert<rgb8_pixel_t,rgba8_pixel_t>(const rgb8_pixel_t& src,rgba8_pixel_t& dst);
}
}
class GLTexture
{
public:
//Destructor
~GLTexture();
//Return instance of GLTexture
static boost::shared_ptr<GLTexture> getInstance(const std::string& filename, bool _isVolatile = true);
//Load image file and generate texture
bool LoadFromFile(boost::shared_ptr<GLTexture> texture);
//Bind texture when the texture exists. If the texture doesn't exist, this method generates a texture.
static void bindTexture(boost::shared_ptr<GLTexture> texture);
//Setter of texFileName
void setTextureFile(const std::string filename);
//Setter of volatile Flag
void setIsVolatile(bool _isVolatile);
//Getter of texture ID
inline const GLuint& getTextureID(void)const{
return this->texture;
};
//Overload of operator to sort list
friend bool operator <(const boost::shared_ptr<GLTexture>& texture1, const boost::shared_ptr<GLTexture>& texture2);
friend std::ostream& operator <<(ostream& os, const boost::shared_ptr<GLTexture>& texture);
public:
GLuint texture; //texture id
std::string texFileName; //image file path
int width; //width of image
int height; //height of image
clock_t start, end; //start is the timing of bindTexture, end is the timing of delete, these are used to sort list.
bool isVolatile; //the flag whether this texture is volatile or not. true is volatile, and false is not volatile. To keep texture, this is false.
bool isRead; //the flag whether this texture already exists in texturelist. true means "exists", false means "not exists".
boost::shared_ptr<boost::gil::rgba8_image_t> pixelData_ptr; //smart pointer to contain pixelData
boost::shared_ptr<boost::gil::rgba8_image_t::const_view_t> viewwithAlpha_ptr; //smart pointer to contain viewData
private:
//Forbid copy constructor
GLTexture();
GLTexture(const std::string& imageFilePath, bool _isVolatile = false);
GLTexture(const GLTexture& glTexture);
GLTexture& operator = (const GLTexture& glTexture);
//load image from SSD
bool LoadImage(const std::string& filename);
};
#endif
TextureManager.hpp
#pragma once
#ifndef __TEXTURE_MANAGER_HPP__
#define __TEXTURE_MANAGER_HPP__
// Standard libraries.
#include <iostream>
#include <fstream>
#include <string>
#ifdef _WIN32
#pragma warning (push)
#pragma warning (disable:4819)
#endif
// Smart pointers.
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
// Foreach macros.
#include <boost/foreach.hpp>
// Regular expression.
#include <boost/regex.hpp>
// Image I/O.
#define png_infopp_NULL (png_infopp)NULL
#define int_p_NULL (int*)NULL
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/jpeg_io.hpp>
#include <boost/gil/extension/io/png_io.hpp>
// File operations,
#include <boost/filesystem.hpp>
// Linear algebra,
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
// String functions.
#include <boost/algorithm/string.hpp>
// Serialization
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/version.hpp>
#ifdef _WIN32
#pragma warning (pop)
#endif
// OpenGL and OpenGL Utility Toolkit
#ifdef _WIN32
#include <GL/glew.h>
#include <GL/GLUT.h>
#elif defined __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#endif
#include "GLTexture.hpp"
class GLTexture;
class TextureManager
{
public:
TextureManager(void);
~TextureManager(void);
//Generate Texture
static void genTexture(boost::shared_ptr<GLTexture> texture);
static void setTexture(const boost::shared_ptr<GLTexture> texture);
static void deleteTexture();
static bool checkList(const std::string& filename);
private:
//Forbid copy constructor
TextureManager(const TextureManager& texManager);
TextureManager& operator = (const TextureManager& texManager);
};
#endif
Did you use recursive search path for the headers? If so change it to non-recursive. That might solve the problem.
I had similary problem!I soved the problem that:
Xcode->Buid Setting->C++ language Dialect(GNU++11)
->C++Standard Library(libc++(LLVM C++ with C++ 11)
I needed to add
#include <cstring>
http://en.cppreference.com/w/cpp/string/byte/memcpy