OpenCV errors when including it's headers in C++ - c++

I'm trying to use OpenCV into an existing project; when I add the header #include <opencv2/opencv.hpp> to my source.cc file (just the header, the rest of the code makes no difference), I get the following error.
make all
Building file: source.cc
Invoking: GCC C++ Compiler
g++ -I../__GXX_EXPERIMENTAL_CXX0X__ -I/usr/include/eigen3 -O3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"source.d" -MT"source.d" -o "source.o" "source.cc"
In file included from /usr/local/include/opencv2/core.hpp:3105:0,
from /usr/local/include/opencv2/opencv.hpp:46,
from source.cc:13:
/usr/local/include/opencv2/core/operations.hpp: In member function ‘bool cv::internal::Matx_FastSolveOp<_Tp, m, n>::operator()(const cv::Matx<_Tp, m, m>&, const cv::Matx<_Tp, m, n>&, cv::Matx<_Tp, m, n>&, int) const’:
/usr/local/include/opencv2/core/operations.hpp:131:18: error: missing template arguments before ‘(’ token
return LU(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n) != 0;
^
make: *** [MapMaker_target_init.o] Error 1
The code looks something like this:
MMaker.cc >>>
#include "MMaker.h"
#include "IMatcher.h"
#include <cvd/image_ref.h>
#include <cvd/vector_image_ref.h>
#include <cvd/vision.h>
#include <cvd/image_interpolate.h>
#include <opencv2/opencv.hpp> // <<<<<<<<<< This is the evil line
bool MMaker::init(AR::IMatcher &matcher)
{
//actually empty
}
MMaker.h >>>
#ifndef __MMAKER_H
#define __MMAKER_H
#include <queue>
namespace AR { class IMatcher; }
class MMaker: protected CVD::Thread
{
public:
bool init(AR::IMatcher &matcher);
// ... more stuff ...
}
//...
IMatcher.h >>>
#ifndef __IMATCHER_H
#define __IMATCHER_H
#include <vector>
#include <string>
namespace cv { class Mat; }
namespace AR
{
class IMatcher
{
// ... IMatcher members declaration ...
}
}
IMatcher.cc >>>
#include "IMatcher.h"
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
// ... IMatcher members implementation ...
I know this is related to how/which files are included or their order perhaps, but haven't got to find what's missing.

Related

ldfcn.h gives 0 instead of true or false

i have dummy.cpp
#include <iostream>
#ifndef EXPORT_API
#define EXPORT_API __attribute__ ((visibility("default")))
#endif
extern "C"{
using namespace std;
bool dum = true;
int main(){};
};
and main.cpp
#include <iostream>
#include <dlfcn.h>
#include <string>
using namespace std;
int main()
{
void *test = dlopen("./dummy", RTLD_NOW);
//bool sus = reinterpret_cast<void(*)()>(dlsym(test , "turd"));
bool* give =(bool*) dlsym(test, "dum");
cout<<give<<"refrence";
};
and i compile them with
g++ dummy.cpp -o dummy
g++ main.cpp -o main -ldl
but when i run ./exe
i get 0refrence and i dont know how to fix this or what the issue is i also have tried making it a function and returning it but that didn't work
solved it with gcc -shared -o dummy.so -fPIC dummy.cpp to get a proper so file
and if yours has a function like int foo(){} or something
use std::invoke in your .sofile and then call it like
bool* give = (bool *) dlsym(test, "dum");
auto answr = *give;
where answr is the return val

g++ : was not declared in this scope despite included header

I am sure this is a trivial error. Nontheless I cannot find an error or a solution on Stackoverflow.
I have received above mentioned error for struct Transition, seemingly declared here :
Transition.h:
#ifndef ZOCK_TRANSITION_H
#define ZOCK_TRANSITION_H
#include <iostream>
#include "vec.h"
#include "dir.h"
#include "Board.h"
using std::ostream;
using std::endl;
struct Transition
{
vec fromv;
dir fromd;
vec tov;
dir tod;
Transition();
Transition(vec fromv, dir fromd, vec tov, dir tod);
friend ostream& operator<< (ostream& os, cost Transition& t);
};
Transition swap(Transition& t) const;
#endif// ZOCK_TRANSITION_H
Error Message is :
g++ -std=c++14 -I./inc -c src/Transition.cpp -o build/debug/Transition.o -g3
In file included from ./inc/Board.h:21:0,
from ./inc/Transition.h:19,
from src/Transition.cpp:12:
./inc/Map.h:29:9: error: ‘Transition’ was not declared in this scope
vector<Transition> transitions;
So lets look into Map.h:
#ifndef ZOCK_MAP_H
#define ZOCK_MAP_H
#include <string>
#include <vector>
#include "Transition.h"
using std::string;
using std::vector;
class Map
{
friend class Board;
private:
int height, width;
char ** fields;
vector<Transition> transitions;
public:
enum readfrom
{
str = 0,
file ,
NUM_READFROM //Leave last !
};
Map(string i, readfrom p);
};
#endif// ZOCK_MAP_H
It is unlikely that it is a compiler or linker error, since it seems every file was found correctly. Everything seems to be included correctly, so I am at a dead end.
What dows cause the error or what are common mistakes causing the error ?

ctime std:: namespace conflict

I have project with many C and C++ files. I try to add thread-safe queue.
In my header:
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
// Some code..
When I try to compile this, its fault with this errors:
In file included from /usr/include/c++/4.9/chrono:41:0,
from /usr/include/c++/4.9/mutex:39,
from queue.hpp:4,
from main.cpp:24:
/usr/include/c++/4.9/ctime:60:11: error: '::clock_t' has not been declared
using ::clock_t;
/usr/include/c++/4.9/condition_variable:161:23: error: 'time_t' in namespace 'std' does not name a type
static_cast<std::time_t>(__s.time_since_epoch().count()),
As I understand it, compiler try to find std::time_*, but why? And how fix it?
Thanks!
UPD: main.cpp
#include "gpu.hpp" //Error here
int main(int argc, char const *argv[]) {
return 0;
}
gpu.hpp
#pragma once
#include "filter.hpp"
#include "queue.hpp" //Error here
#include <nvcuvid.h>
#include <avformat.h>
#include <vector>
queue.hpp
#pragma once
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
template<typename T>
class CQueue
{
std::queue<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_cond;
// ...
First error message:
In file included from queue.hpp:3:0,
from gpu.hpp:3,
from main-test.cpp:2:
/usr/include/c++/4.9/ctime:60:11: error: '::clock_t' has not been declared
using ::clock_t;
Makefile:
FFMPEG_INCLUDES := -I$(FFMPEG_PATH) $(FFMPEG_PATH)/libavutil ...
$(OBJECTS_DIRS)/app-main-test.o: src/app/main-test.cpp
$(CXX) $(CXXFLAGS) $(FFMPEG_INCLUDES) $(CUDA_INCLUDES) -o $# -c $<
The problem was in my Makefile.
I has include path to each ffmpeg folder. FFMPEG_INCLUDES := -I$(FFMPEG_PATH) $(FFMPEG_PATH)/libavutil ... FFMPEG have time.c in ffmpeg/libavutil It causes conflict with ctime.
I replaced #include <log.h> to #include<libavutil/log.h> and fixed include path in makefile FFMPEG_INCLUDES := -I$(FFMPEG_PATH)
Thank you #user2807083 for help.

error: ‘boost::array’ is not a type

I have created a simple class as follows. What I am attempting to do here is to convert boost::array to a string so that it can be printed out or used for some other purpose.
#include <iostream>
#include <exception>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include <sstream>
#include <iomanip>
class buffer_manager
{
public:
buffer_manager()
{
}
~buffer_manager()
{
}
std::string message_buffer(boost::array buf)
{
message = boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(result));
return message;
}
private:
boost::array<unsigned char, 4096> recv_buf = {{0}};
std::string message;
};
While compiling I seem to get a strange error.
g++ -std=c++11 -g -Wall -pedantic -c buffer_manager.cpp -o buffer_manager.o
buffer_manager.cpp:26:39: error: ‘boost::array’ is not a type
std::string message_buffer(boost::array buf)
^
buffer_manager.cpp: In member function ‘std::string buffer_manager::message_buffer(int)’:
buffer_manager.cpp:28:89: error: ‘result’ was not declared in this scope
message = boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(result));
^
buffer_manager.cpp:28:95: error: ‘back_inserter’ was not declared in this scope
message = boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(result));
^
buffer_manager.cpp:28:95: note: suggested alternative:
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from buffer_manager.cpp:1:
/usr/include/c++/4.9/bits/stl_iterator.h:480:5: note: ‘std::back_inserter’
back_inserter(_Container& __x)
^
Makefile:15: recipe for target 'buffer_manager.o' failed
Since boost::array is a template type, you need to have a template parameter when you use the boost::array as a parameter to a function. I recommend using a typedef:
typedef boost::array<unsigned char, 4096> My_Array_Type;
//...
std::string message_buffer(My_Array_Type& buf);
//...
My_Array_Type rec_buf;
Edit 1:
In your situation, you may find the std::vector a better choice. Also, a typedef will be convenient here also.

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.