Multiple Errors When Compiling with g++? - c++

When compiling my code, I'm receiving many different errors such as:
frame.h:5:48: error: expected identifier before '(' token
frame.h:5:17: error: variable or field 'writeFrame' declared void
frame.h:5:17: error: 'GifWriter' was not declared in this scope
frame.h:5:34: error: expected primary-expression before 'double'
These errors more or less apply for all the function parameters.
Here is the header file:
#ifndef frame_h
#define frame_h
void writeFrame(GifWriter* gptr, double delay, (std::vector<uint8_t>& frameOut), ImageData* image1ptr, struct Options mode, int frameNum, bool* runptr);
void runPRGM(ImageData* image1ptr, struct Options mode);
#endif
My .cpp file does include the required libraries and this .h file, and the two functions are declared in the same way. I'm certain the brackets and parentheses are parsed correctly. Other than the function definitions within the .cpp, these are the only instances of the functions' declarations.
What could I potentially be doing wrong? I did read in some other thread that g++ conflicts with certain void functions as well but there wasn't much elaboration on that.

Change (std::vector<uint8_t>& frameOut) to std::vector<uint8_t>& frameOut.
Not sure why you felt the need to put the brackets there, and the first error message is telling you that's the problem.
Also note that you should write header files so that they are self-contained. This means that this header file should have #include <vector> as well as having declarations for GifWriter etc. These may be forward declarations if the circumstances are right.
So the header should look something like this
#ifndef frame_h
#define frame_h
#include <vector> // for vector
#include <cstdint> // for uint8_t
class GifWriter; // forward declarations
class ImageData;
struct Options;
void writeFrame(GifWriter* gptr, double delay, std::vector<uint8_t>& frameOut, ImageData* image1ptr, Options mode, int frameNum, bool* runptr);
void runPRGM(ImageData* image1ptr, Options mode);
#endif
Image assuming that GifWriter, ImageData and Options are classes that you have written and therefore its safe to forward declare them. If not then you should include the header files for those classes here instead.

Related

I'm getting a "redefinition of" error for every method in the class

I'm getting a redefinition error for every method in the class. I tried to put the .cpp code in the header where the "include Algebra.cpp" is, but didn't change a thing.
// This is the .h file
#ifndef ALGEBRA_H
#define ALGEBRA_H
#include <iostream>
template <size_t rows, size_t cols>
class Matrix {
double** matrix;
void debug_print();
public:
Matrix();
Matrix(double (&_matrix)[rows][cols]);
~Matrix();
double calculate_determinant();
};
#include "Algebra.cpp"
#endif
// This is the .cpp file
#include "Algebra.h"
#include <cassert>
template <size_t rows, size_t cols>
Matrix<rows, cols>::Matrix() {...}
// I have defined all the methods below..
First, that template function definition belongs in the header file that gets pulled in wherever it's needed with #include "algebra.h".
The reason you're getting problems with this particular setup is that, as several comments have suggested, you're compiling the .cpp file that contains that template function definition. When the compiler compiles "algebra.cpp", it first sees the #include directive at the top of the file, and pulls in the text from "algebra.h". That's fine as far as it goes, but at the end of "algebra.h" it sees #include "algebra.cpp". So it pulls in the text from there.
Now it's compiling "algebra.cpp" inside of "algebra.cpp". The include guard in "algebra.h" works here, so there is no redefinition of the contents of "algebra.h". And then it sees the definition of Matrix<rows, cols>::Matrix(). Still okay, so far.
Now it comes to the end of the code that comes in through the #include directive, so it continues to compile the code in "algebra.cpp", and it again sees the definition of Matrix<rows, cols>::Matrix(), and that's the redefinition that the compiler is complaining about.
That's why you shouldn't do that.
Some folks like to put definitions of member functions of class templates in a separate file from the header that defines the class template. That's okay, but don't compile that file; it should only be compiled as part of the header that uses it. And as a practical matter, that means giving it an extension that makes it clear that it's not a source file to be compiled on its own. When I've used that approach, I use ".inl" as the extension. If you change the name of "algebra.cpp" to "algebra.inl" and make sure that you're not telling the compiler to compile "algebra.inl" this problem should go away.

Compiler complains about redefinition even with guards present

I'm trying to set up some logging functions for an OpenGL application. GLFW offers the option to register a callback function that is called whenever an error takes place, but because it is a C library it demands that the function be written in C style, i.e. outside of a class. Because of that, I put my logging functions into a namespace defined in file log.h.
#ifndef LOG_H
#define LOG_H
#include <fstream>
namespace gllog{
#define GL_LOG_FILE "gl.log"
bool restart_gl_log(){
//...
}
bool gl_log (const char* message, const char* filename, int line){
//...
}
void glfw_error_callback (int error, const char* description){
//...
}
};
#endif
Even though I added include guards, whenever I include this file from two different files, I get errors like the following:
CMakeFiles/gl4tuts.dir/extended_initialisation/ExtendedInitialisation.cpp.o: In function `gllog::glfw_error_callback(int, char const*)':
ExtendedInitialisation.cpp:(.text+0x340): multiple definition of `gllog::glfw_error_callback(int, char const*)'
CMakeFiles/gl4tuts.dir/hello_triangle/HelloTriangle.cpp.o:HelloTriangle.cpp:(.text+0xa10): first defined here
I'm building with CMake.
What could be the reason? Could the absence of a class be related?
Include guards protect you from including the same header file more than once while compiling the same .cpp file. They don't stop you from including the same header file more than once while compiling different .cpp files -- in fact they're commonly used to make the same class available in different compilation units.
The compiler is not complaining, by the way, the linker is.

Why am I getting linking errors even with header guards? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does this not prevent multiple function declarations?
Global.h
#ifndef Global_h
#define Global_h
#include <iostream>
unsigned char exitStatus;
#endif
OutputHandler.h
#ifndef OutputHandler_h
#define OutputHandler_h
#include "Global.h"
class OutputHandler {
private:
static bool instanceExists;
// more code
#endif
Root.h
#ifndef Root_h
#define Root_h
// declarations
OutputHandler *output;
#endif
Root.cpp
#include "Root.h"
// gets instance of OutputHandler
// more code
I am getting errors regarding exitStatus, static bool instanceExists, and static class output being already defined by Root.obj in OutputHandler.obj. I assume the issue is with including the header file OutputHandler.h in both Root.h and OutputHandler.cpp. Anyone know how to fix this or how to better organize header files?
Because include guards only work at the translation unit level (you can, for this simple case, consider a single C file to be a translation unit).
That means a single C file, if it includes the header file twice, will not process it the second time due to the include guards.
However, if you include the header from two different C files, each of them will get a copy of the variables defined in that header.
Then, when you link them together, you get the duplicates.
The easiest way to get around this problem is to never define things in headers, only declare them.
So, in the header (eg, xyzzy.h), you have:
extern int xyzzy; // declare but don't define.
and in all the C files that want to use that, put:
$include "xyzzy.h"
and, in one of those C files, also put:
int xyzzy; // define it here, once.
You can think of declaration as a simple "I declare that this exists somewhere, just not here", while definition is "I an creating this here and now".
Declare extern usigned char exitStatus in Global.h and define it in one implementation file.
The problem is during the linking phase; include guards in the headers won't help you.
In C, there is the separate concepts of declarations and definitions. Declarations are what are put into headers; they merely state that a particular variable exists. The definition of a variable is where storage is actually allocated for it.
For example, in your Global.h, you have:
#ifndef Global_h
#define Global_h
#include <iostream>
usigned char exitStatus;
#endif
This is defining a variable called exitStatus, and the linker is complaining because any given variable should only be defined in one place in a program. What you need to do is declare it in the header, and then define it in only one place in a source (*.cpp) file. For example, your header should declare exitStatus with:
extern char exitStatus;
and in only one source file, define it with:
char exitStatus;
The situation is similar for output in Root.h, as well as any other place you should be declaring variables in the header file.
See also: http://www.cprogramming.com/declare_vs_define.html

Detecting namespace in c++

Is it possible to detect whether at a given point of code you are in a namespace? In particular, I want to include a warning if a file is being including in the global namespace.
I can give you a hint that generates compilation error if header is not include in global namespace. If I know C++ constructiom which for sure generates compiler warning (other that #warning) then it could be used instead of compilation error.
Put in your header:
template <class A, class B>
struct AreSame { enum { VALUE = -1 }; };
template <class A>
struct AreSame<A,A> { enum { VALUE = 1 }; };
struct TestGlobalNamespace
{
int test_namespace[AreSame<TestGlobalNamespace, ::TestGlobalNamespace>::VALUE];
};
When your header is include in some namespace you'll get an error.
Like in this example:
namespace Some {
struct TestGlobalNamespace
{
int test_namespace[AreSame<TestGlobalNamespace, ::TestGlobalNamespace>::VALUE];
};
}
You'll get:
prog.cpp:17: error: size of array ‘test_namespace’ is negative
[UPDATE]
However the more probably would be this kind of errors:
prog.cpp:17: error: ‘::TestGlobalNamespace’ has not been declared
prog.cpp:17: error: template argument 2 is invalid
Anyway - no one will dare to include your header to namespace other than global.
Good practice is including all headers in global namespace. Open all needed namespaces at the beginning of file and close them before end. Another way will inevitably lead to heap of problems.
** Comment extension **
To prevent unintended inclusion, you can do something like this:
In header:
#ifndef INTERNAL_INCLUDE
#error ...
#endif
When used:
#define INTERNAL_INCLUDE
#include "internal.h"
#undef INTERNAL_INCLUDE
You can do this with the small inconvenience of requiring a second header, to be included in the global namespace before the first.
// stuff_guard.h - include from global namespace to guard stuff.h
#ifndef STUFF_GUARD_H
#define STUFF_GUARD_H
typedef char stuff_guard[1];
#endif
// stuff.h - must be included from non-global namespace, after stuff_guard.h
// No include guard - may be included multiple times, in different namespaces.
#ifndef STUFF_GUARD_H
#error stuff_guard.h must be included before stuff.h
#endif
typedef char stuff_guard[2];
static_assert(sizeof (stuff_guard) != sizeof (::stuff_guard),
"stuff.h must not be included from the global namespace");
// Put your stuff here
This will give reasonably friendly error messages if you break either of these two rules, and a somewhat less friendly error if you include stuff_guard.h from a non-global namespace.
If you're stuck with an old compiler and static_assert isn't available, then you could use BOOST_STATIC_ASSERT, or roll your own.

Define the prototype of a function with a std::vector as input parameter in an header

I'm getting crazy since i'm not able to define the prototype of a function i'm actually using.
What i'm doing is create an header file called func1.hwhere i define this prototype (that's because i need to invoke this function from some other function implemented elsewhere):
void FileVector(std::vector<Files> &,const char*,bool);
Where Filesis a struct defined in func1.cpp
struct Files{
HANDLE h;
WIN32_FIND_DATA info;
} file;
I have also another function that accept a std::vector<Files> & as input parameter but when i try to compile (using Eclipse C++) i'm getting those errors:
/FileVector.h:11:22: error: variable or field 'FileVector' declared void
..\/FileVector.h:11:17: error: 'vector' is not a member of 'std'
..\/FileVector.h:11:29: error: 'Files' was not declared in this scope
I've tryied to include several directive in the header file..for example declaring the struct in the header and including vector header do the trick but this way i got loads of "multiple definitions/first defined here" error.
What can i do?
EDIT
Now my header looks like:
#ifndef FILEVECTOR_H_
#define FILEVECTOR_H_
#include <vector>
#include <windows.h>
struct Files{
HANDLE h;
WIN32_FIND_DATA info;
};
void FileVector(std::vector<Files> &,const char*,bool);
#endif /* FILEVECTOR_H_ */
At this point, i need to declare another prototype in another header:
void ProcessInput(vector<Files>&);
but i can't use the same trick as above cause i'll have to re-define the Files struct. How can i solve this?
Make sure to include <vector> in your header file. You also mention that the definition of Files is within the cpp file, you should move it to the header as well.
Also place proper header guards to avoid multiple definition errors:
#ifndef MY_HEADER_FILE_GUARD
#define MY_HEADER_FILE_GUARD
... your content here ...
#endif /*MY_HEADER_FILE_GUARD*/
Update:
Simply including filevector.h from your new header would do. However, it looks like Files should be defined in a header of its own and included from the two headers that make use of it.