OpenCV Mat 'expected ")"' in method parameter - c++

I am currently developing a Mac application which involves mixing C++ and Objective C. I am trying to separate part of the application into a cpp source file and I can't get the damn thing to compile.
Bear in mind I am very new to using c++ so forgive me if I have made a really stupid error.
I've managed to pare it down to the most minimal example I possibly can. For some reason when I use the Mat data type within a method declaration, I get
expected ')'
My header file
#ifndef MATCHTEMPLATES_H
#define MATCHTEMPLATES_H
#include "opencv2/opencv.hpp"
class Matcher {
public:
Matcher(cv::Mat& template);
};
#endif
my source file
#include "Matcher.hpp"
using namespace cv;
Matcher::Matcher(Mat& template) { }
compile commands
clang++ -isystem /usr/local/Cellar/opencv3/3.1.0_3/include/opencv -isystem /usr/local/Cellar/opencv3/3.1.0_3/include -o CMakeFiles/Tracker.dir/source/cpp/Matcher.cpp.o -c {project location}/source/cpp/Matcher.cpp

Yeah, stupid mistake.
Apparently 'template' is a reserved word in c++
Thanks Sebastian Hoffman for pointing that out...

Related

Getting Started with ANTLR4 and C++

First time question asker here, so bear with me. So I've got a grammar file from the grammars repository that I'm trying to use with C++. (Developing on macOS). I have no issue generating the lexer and parser using ANTLR. But after that, I have no idea how to run/use the resulting .cpp and .h files. I understand that there is an antlr runtime that I must download, and I have done so from the antlr.org website (gives me two folders, antlr4-runtime and lib), but my novice understanding of C++ seems to be preventing me from getting any further than that. How do I use the runtime to work with these files? I'm not using an IDE, just g++ from the command line. Thank you for any help!
I found this guide helpful: Getting Started with ANTLR in C++. (Ah, saw #ggorlen's comment after.)
If you scroll down on that page to a little past halfway, there's a section titled How to Use ANTLR in C++. I think that's where you are.
I'll copy that example over as SO generally prefers this. Say this is your main.cpp:
#include <iostream>
#include "antlr4-runtime/antlr4-runtime.h"
#include "antlr4-runtime/SceneLexer.h"
#include "antlr4-runtime/SceneParser.h"
#include "ImageVisitor.h"
using namespace std;
using namespace antlr4;
int main(int argc, const char* argv[]) {
std::ifstream stream;
stream.open("input.scene");
ANTLRInputStream input(stream);
SceneLexer lexer(&input);
CommonTokenStream tokens(&lexer);
SceneParser parser(&tokens);
SceneParser::FileContext* tree = parser.file();
ImageVisitor visitor;
Scene scene = visitor.visitFile(tree);
scene.draw();
return 0;
}
You want to include your lexer and parser .h (header) files instead of the SceneLexer/Parser in the example, and also include antlr4-runtime.h. Then run g++ on all your .cpp files, e.g.
$ g++ main.cpp YourLexer.h YourParser.h

Is it possible to undefine a class in C++ (Visual Studio)?

I know, it may seem strange, but my goal is to undefine a class in C++. The root of the problem is in combining TinyXML2 and Boost unit tests.
Contents of the header file (Configuration.h) which is being tested:
...
#include <tinyxml2.h>
...
And this is the contents of my configurationTests.h file:
#include "unitTests.h"
#include "Configuration.h"
BOOST_AUTO_TEST_SUITE(configuration_test)
BOOST_AUTO_TEST_CASE(basic) {
...
}
BOOST_AUTO_TEST_SUITE_END( )
When I try to compile my tests, I'm getting an error:
error C2371: 'XMLDocument' : redefinition; different basic
types c:\program files (x86)\windows kits\8.0\include\um\msxml.h 10085
Inside this file (msxml.h) on line 10085 we have this class definition:
class DECLSPEC_UUID("CFC399AF-D876-11d0-9C10-00C04FC99C8E")
XMLDocument;
When I remove those two lines, my tests do compile and everything seems fine. Of course, this is not a solution, but that fact prooves that something inside Boost unit tests library includes msxml.h and somehow leads to conflict with TinyXML2 library.
I tried different solutions found in Google (like writing "#define WIN32_LEAN_AND_MEAN"), removing "using namespace tinyxml2" and making changes inside tinyxml2.cpp - nothing actually helps.
So, my question is simple: can I undefine (unload?) previously defined class in compile time in some tricky way? I tried "#undef XMLDocument", "#define XMLDocument 1" + "#undef XMLDocument" - nothing works.
Update: Actually, I kinda solved the problem by writing "#define MSXML_LIBRARY_DEFINED" on the first line of configurationTests.h. But still, I would love to know an answer to this topic question.
I think you have used default namespace for tinyxml2.
Try to delete using namespace tinyxml2 and then use it like in this example:
tinyxml2::XMLDocument xDoc;
try this:
namespace your_name_space
{
#include <tinyxml2.h>
}
From now all classes inside the tinyxml2 are hidden by your namespace.
The namespece must be declared also for the tinyxml2.cpp file.

Trying to use functions in a header/cpp file

I have two files:
hello.h and hello.cpp
hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
using namespace std;
void PrintMessage();
#endif
hello.cpp
#include <iostream>
#include "hello.h"
using namespace std;
void PrintMessage()
{
cout << "I want to be displayed!";
}
Now, I want to use PrintMessage() in a new .cpp file, but I keep getting an error message. This is what I'm doing:
printingmessage.cpp
#include <iostream>
#include "hello.h"
using namespace std;
int main()
{
PrintMessage();
return 0;
}
Am I just doing something blatantly wrong? I do have all of them in the same folder; I assume it has something to do with Dev-C++ (what I'm using to write/compile/run), but I can't figure it out. Anyone have any ideas?
I created a folder on my desktop, put all three files inside, and I tried to compile my printingmessage.cpp file exactly as it is. This is the error I'm getting:
[Linker error] Undefined reference to 'PrintMessage()'
i don't know dev C++ , but i would strongly advise if you do any serious coding to learn/move to the terminal and use make files, or a newer IDE such as visual studios.
heres a short script you can run save it as bash.sh
something like this
g++ hello.cpp -O2 -g -c
g++ hello.o printmessage.cpp -Wall -O2 -o print
then run it with ./print
I assume it has something to do with Dev-C++ (what I'm using to
write/compile/run), but I can't figure it out.
I guess so, too. Behind the scenes, the following things have to happen:
Both files get compiled. This creates a *.obj file for every *.cpp file, and uses the header.
The object files are linked against one another and possibly against required libraries.
Your problem lies in the “one another” part of the second step: the code compiles all right, but linking fails. The header file is irrelevant at that point. More precisely, the linker invocation for printingmessage.obj contains a reference to a function which isn't defined in that object file or any of the default libraries. Most likely the problem is due to the *.cpp files not being part of the same project. You need to create a multi-source-file project where you can link multiple object files. How you do that with Dev-C++ is probably somewhere in their manuals.

How do you include the C++ standard library in xcode 4.3

I'm trying to use C++ code in my ios app (I really don't like objective C, I'm going to use it only when necessary [view control, etc]) and while everything seems to be working I get an error in the following test file.
#ifndef prueba3_GlobalStatic_h
#define prueba3_GlobalStatic_h
#include <string>
using namespace std;
class GlobalStatic
{
public:
GlobalStatic();
~GlobalStatic();
string foo();
private:
int integerValue;
};
#endif
When I try to build the project the IDE gives me the following error:
" 'string' file not found"
I've looked around but cannot find a conclusive answer; any help would be appreciated. In essence, how do I get the standard library working?
One cause of missing c++ headers is including them from an objective-c context as opposed to from objective-c++ -- and you can't use the c++ stl from c! The easier solution is to simply change all your .m files to .mm, .mm will send them to the objective-c++ compiler.

Yet another linker issue

I'm having a linking issue with a basic C++ program. No, I'm not including .cpp files!
This is what's happening.
main.cpp:
#include "header.h"
#include <iostream>
int main() {
std::cout << "Hello!";
}
header.h:
#ifndef _HEADER_H
#define _HEADER_H
class Something {
public:
printContents();
};
#endif
something.cpp:
#include "header.h"
#include <iostream>
Something::printContents() {
cout << "This class's Contents!!";
}
What's happening is that I get a compiler error going: multiple definitions of some standard C function, such as strtod:
g++ -o ... main.o
build/....main.o: In function
`strtod':
../MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h:318:
multiple definition of `strtod'
build/..something.o:...something.cpp:(.text+0x0):
first defined here collect2: ld
returned 1 exit status
If I get rid of #include <iostream> in one of the two occasions and get rid of the couts, it will compile. What's going on? I'm using g++ and NetBeans to compile.
I tried in the command line:
g++ *.h *.cpp -o program
and the same thing happened.
Please note that _HEADER_H is an illegal name in C++ user code - names beginning with the underscore and an uppercase letter are reserved for the C++ implementation. This does not normally cause noticeable problems, but when you use what may be a common name in the implementation like HEADER in this context, it well might.
Modify,
Something::printContents()
{
std::cout << "This class's Contents!!";
}
NOTE: Specify the return datatype.
One of your problems is right here:
I tried in the command line: g++ *.h
*.cpp -o program
Don't pass your header files... Try something like this:
g++ *.cpp -o program
I could not reproduce your exact problem. I get this to compile and link nicely with the following few notes:
Add a void return type to the printContents-function (So it says void printContents(); in the header and void Something::printContents() { in the implementation-file)
Use std::cout rather than just cout. cout is not defined in the scope it is used
Make sure header.h ends with a blank line
Use HEADER_H rather than _HEADER_H (like Neil Butterworth says)
I use the command line g++ main.cpp something.cpp to compile.
I see a couple of problems. You shuold define the returning value of the function
printContents()
and you must write
std::cout
if you don't write
using namespace std;
The problem was in a multi-installation of MinGW. I had one already installed, and when I got Qt on my computer, it had installed it's own MinGW. Bummer, I ported the code to my university's servers and it ran OK.
Bummer!!
Thanks everybody for the help, I will definitely follow your guidelines in the future.
Header names - no underscores
Correct return type
Real code in the forums!
Leo Bruzzaniti