opencv3 knearest object definition error - c++

I'm trying to implement a digi recognition program with c++ and opencv3, when i define a varible using KNearest i get this error:
main.cpp:19:18: error: variable or field 'RunSelfTest' declared void
void RunSelfTest(KNearest& knn2);
^
main.cpp:19:18: error: 'KNearest' was not declared in this scope
main.cpp:19:18: note: suggested alternative:
In file included from c:/OpenCV/build/include/opencv2/ml/ml.hpp:48:0,
from main.cpp:1:
c:/OpenCV/build/include/opencv2/ml.hpp:397:20: note: 'cv::ml::KNearest'
class CV_EXPORTS_W KNearest : public StatModel
this is my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/ml/ml.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
void RunSelfTest(KNearest& knn2);
void AnalyseImage(KNearest knearest);
i totally can't find where is the problem

You want to use cv::ml::KNearest but you're trying to refer to it as KNearest while using namespace cv.
But KNearest is also inside the ml namespace inside cv::. Try this:
ml::KNearest
(Or remove using namespace …; from your code, since it's bad practice anyway, and simply refer to it as cv::ml::KNearest.)

Related

OpenCV 3.1.0 : How to use BackgroundSubtractorMOG class

I have OpenCV 3.1.0 installed.
I want to use BackgroundSubtractorMOG so in my file i have these includes:
//opencv
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/video/background_segm.hpp>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
I declare a pointer to a class of this type:
Ptr<BackgroundSubtractorMOG> pMOG;
And I initialize the pointer like this:
pMOG = createBackgroundSubtractorMOG();
When I try to compile I get this:
/home/edd/Desktop/compvis/FML.cpp:19:5: error: ‘BackgroundSubtractorMOG’ was not declared in this scope
Ptr<BackgroundSubtractorMOG> pMOG; //MOG2 Background subtractor
^
/home/edd/Desktop/compvis/FML.cpp:19:28: error: template argument 1 is invalid
Ptr<BackgroundSubtractorMOG> pMOG; //MOG2 Background subtractor
^
/home/edd/Desktop/compvis/FML.cpp: In function ‘int main(int, char**)’:
/home/edd/Desktop/compvis/FML.cpp:29:42: error: ‘createBackgroundSubtractorMOG’ was not declared in this scope
pMOG = createBackgroundSubtractorMOG(); //MOG approach
^
I tried to look in the documentation. I couldn't figure out what header to include. I couldn't figure out how to instantiate an instance of the class correctly.
What do ?
In the code you've posted, I don't see any statement regarding namespaces. (e.g. using namespace cv in your preamble). Otherwise, you have to preface OpenCV code with cv:: (e.g. cv::Mat frame to declare a Mat object).
EDIT:
#include "opencv2/opencv.hpp"
#include "opencv2/bgsegm.hpp"
...
cv::Ptr<cv::BackgroundSubtractor> mog = cv::bgsegm::createBackgroundSubtractorMOG();
...
And by using the namespace:
#include "opencv2/opencv.hpp"
#include "opencv2/bgsegm.hpp"
using namespace cv
...
Ptr<BackgroundSubtractor> mog = bgsegm::createBackgroundSubtractorMOG();
...
Hope this helps,
MJ

'list' was not declared in this scope

I am new to c++, and I am trying to get a basic program to initialize a list of short unsigned integers. I am compiling and running using scygwin and g++.
Below is the code in the .cpp file:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;
int main (int argc, char* argv[]) {
list<int> events;
return 0;
}
which I run by typing the following command into cygwin terminal:
$ g++ -o test.out test.cpp
However, I get the following compilation errors:
test.cpp: In function ‘int main(int, char**)’: test.cpp:16:1: error:
‘list’ was not declared in this scope list events;
^
test.cpp:16:6: error: expected primary-expression before ‘int’
list events;
^
I am confused about why list is not in the scope, since I am using namespace std? I found a similar question asked about this on a c++ forum, but my problem would be resolved with that. Anyone know what the problem is here?
-Paul
using namespace std; doesn't add any functionality to your code. It just means you don't have to type std:: when referencing things in the std namespace, like std::list.
To actually include the code base for std::list into your program, you need to add:
#include <list>
When in doubt about this kind of thing, doing a google search for cpp reference list will turn up a page like this where you can see: Defined in header <list> at the top.
Here's another question about using namespace std; that may prove useful and why you shouldn't use it. I'll add a little bit to perhaps explain namespaces.
It is common in C++ programs to organize functions into classes and namespaces. Imagine you wrote your own list class to handle certain scenarios. In order to prevent naming conflicts you would put it in a different namespace than std.
namespace MyApp {
class list;
void sort(list&);
}
For the majority of a large code base you might still prefer to use std::list but you need MyApp::list for some things. Using namespaces you can cluster your code and prevent naming conflicts for similar functionality.
Summary
using namespace std; makes it so that if you reference a function or class not in the global namespace it looks for it in the std namespace.
#include <list> actually inserts prototypes (information about how to access the code) in your source file during the preprocessor stage.

BOOST_PHOENIX_ADAPT_FUNCTION causes invalid template error

I am trying to create a lazy function from a template function following the Boost::phoenix documentation. The code looks like this
#include <iostream>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost/phoenix/core/argument.hpp>
using namespace boost;
using namespace boost::phoenix;
namespace demo
{
bool func(double a,double b)
{
return bool(a > b);
}
}
BOOST_PHOENIX_ADAPT_FUNCTION( bool , func , demo::func , 2)
int main(int argc,char **argv)
{
namespace pl = boost::phoenix::placeholders;
auto comperator = func(pl::arg1,pl::arg2);
std::cout<<comperator(1.2,12.4)<<std::endl;
std::cout<<comperator(0.5,0.1)<<std::endl;
}
This is virtually one of the examples from the BOOST documentation. Storing this file as mk_lazy1.cpp and try to compile gives
$ g++ -omk_lazy1 mk_lazy1.cpp
mk_lazy1.cpp:26:1: error: template argument 1 is invalid
mk_lazy1.cpp:26:1: error: expected identifier before ‘::’ token
mk_lazy1.cpp:26:1: error: expected initializer before ‘const’
mk_lazy1.cpp: In function ‘int main(int, char**)’:
mk_lazy1.cpp:31:10: error: ‘comperator’ does not name a type
mk_lazy1.cpp:32:35: error: ‘comperator’ was not declared in this scope
I use gcc-4.7 on a Debian testing system. An honestly I am a bit lost as I have absolutely no idea what is wrong here (as I said, this is virtually a word by word copy of one of the examples provided by the Boost documentation).
Does anyone have a good idea?
Remove using namespaces and all will work fine.
Or write using namespaces AFTER adapt macro and all will work fine too.
Or put macro into unnamed namespace.

Namespaces not found by the compiler

I have several files, listed just below, wherein I define some namespaces and classes. Then in one of the files I am trying to incorporate the classes from the others. The issue is that the compiler seems to not be able to find my namespaces.
charon.cpp
chin.cpp
chout.cpp
main.cpp
My namespaces are charon, charon_in, and charon_out. The main problems come up on a particular file, charon.cpp, so here is that file and chin.cpp too.
The errors:
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/sys/charon.o.d -o build/Debug/GNU-Linux-x86/sys/charon.o sys/charon.cpp
sys/charon.cpp:6:17: error: ‘charon_in’ is not a namespace-name
sys/charon.cpp:6:26: error: expected namespace-name before ‘;’ token
sys/charon.cpp:7:17: error: ‘charon_out’ is not a namespace-name
sys/charon.cpp:7:27: error: expected namespace-name before ‘;’ token
sys/charon.cpp:15:5: error: ‘chout_’ does not name a type
sys/charon.cpp:16:5: error: ‘chin_’ does not name a type
sys/charon.cpp:31:39: error: ‘chin_’ has not been declared
sys/charon.cpp:31:55: error: ‘engine_input’ was not declared in this scope
sys/charon.cpp:32:40: error: ‘chout_’ has not been declared
sys/charon.cpp:32:57: error: ‘engine_output’ was not declared in this scope
charon.cpp
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
using namespace std;
using namespace charon_in;
using namespace charon_out;
namespace charon
{
class charon_
{
private:
chout_ engine_output;
chin_ engine_input;
boost::thread input_thread;
boost::thread output_thread;
void start_threads();
void stop_threads();
public:
//Methods
};
chin.cpp
#include <iostream>
#include <borland/conio.h>
#include <ncurses.h>
#include <boost/thread.hpp>
using namespace std;
using namespace charon;
using namespace charon_out;
namespace charon_in
{
class chin_
{
private:
bool pause;
charon_* engine;
inline iostream grab();
public:
//Methods
};
I triple checked everything. So I am pretty confident the syntax is correct, but obviously I'm missing some key concept otherwise the compiler wouldn't complain.
(I know putting classes in the cpp files isn't a standard thing to do, but I know it is possible to do it so that's why I chose to try.)
I can't see what mistakes I've made. Any help would be appreciated.
You need to include the header file that declares the namespace (or declare it again) before the using directive:
// using namespace test; // Error test is not known to be a namespace
namespace test {}
using namespace test; // Fine -- test is known
As Wayne correctly points out, you probably want to restructure your code differently, including header files that will contain the declarations and can be included in different translation units.
You have the class declarations and the definitons in the .cpp files. You need to move the class declarations to a .h file and include it in the appropriate files that are using the class.
For example, move the following to chin.h and include chin.h in charon.cpp.
namespace charon_in
{
class chin_
{
private:
bool pause;
iostream key_sequence;
deque<char> key_queue;
charon_* engine;
inline iostream grab();
public:
chin_(const charon_& handle);
chin_(const chin_& orig);
~chin_();
void refresh();
bool stop_check();
};
}
Be wary of cyclic dependencies for they can also cause the compiler to not find the namespace you require.
//In rightWing.h
include "leftWing.h"
// code
//In leftWing.h
include "rightWing.h"
// code

Qt Application will not compile

This has been stumping me for a while. I am trying to create a function that takes a hash table, and returns said hash table. However I am getting this error in the header file,
error: ‘string’ was not declared in this scope.
error: template argument 1 is invalid
Here is the header file itself:
#ifndef NAME_SPAWN_H
#define NAME_SPAWN_H
#include <QString>
#include <QHash>
#include <string>
class Name_Spawn
{
public:
Name_Spawn();
void initalize();
private:
QString int_2_str(int);
void seed();
QHash<string,QString> setTable(QHash<string,QString>);
};
#endif // NAME_SPAWN_H
As you can see, string has been declared. Any ideas? I am at my wits end.
The real name of string is std::string. Try using that instead.
(You can leave off the std:: qualifier only if there's a using namespace std; directive in scope. But it's a good habit not to put using namespace std; in header files.)