Using ComplexSolver in Eigen - c++

I'm trying to use Eigen3 to compute some complex eigenvalues and sofar everything worked, but know I get a weird error:
g++ -I/usr/include/eigen3/ -Icubature/ -I/usr/include/boost/ cubature/hcubature.c main.cpp wavefunction.cpp -o main.x
main.cpp: In function ‘int main(int, char**)’:
main.cpp:103:2: error: ‘ComplexEigenSolver’ was not declared in this scope
ComplexEigenSolver<MatrixXcd> ces;
^~~~~~~~~~~~~~~~~~
main.cpp:103:2: note: suggested alternative:
In file included from /usr/include/eigen3/Eigen/Eigenvalues:35:0,
from /usr/include/eigen3/Eigen/Dense:7,
from main.cpp:4:
/usr/include/eigen3/Eigen/src/Eigenvalues/ComplexEigenSolver.h:45:38: note: ‘Eigen::ComplexEigenSolver’
template<typename _MatrixType> class ComplexEigenSolver
^~~~~~~~~~~~~~~~~~
main.cpp:103:30: error: expected primary-expression before ‘>’ token
ComplexEigenSolver<MatrixXcd> ces;
^
main.cpp:103:32: error: ‘ces’ was not declared in this scope
ComplexEigenSolver<MatrixXcd> ces;
^~~
make: *** [Makefile:2: all] Error 1
Here are my includes:
#include <iostream>
#include <vector>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include "wavefunction.cpp"
#include "cubature.h"
using Eigen::MatrixXcd;
This is the routine where the error occurs:
MatrixXcd hop(5,5);
for(int m1 = -2; m1 <= 2; m1++){
for(int m2 = -2; m2 <= 2; m2++){
hop(m1+2, m2+2) = ....;
}
}
ComplexEigenSolver<MatrixXcd> ces;
Everything works fine, only the last line doesn't work. I don't see the difference between my code and the example given at the Eigen website.
What am I doing wrong?

Related

Possible namespace errors in OpenCV header files

I'm just now getting into OpenCV (4.1.0) in C++ (relatively new to both) and I'm getting an odd error from simply including some header files. (GCC 6.3.0)
Error:
from c:\Users\Logan\Projects\Code\C++\webcamTest.cpp:1:
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:697:14: error: 'recursive_mutex' in namespace 'std' does not name a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:25: error: 'Mutex' is not a member of 'cv'
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
Code:
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
int main()
{
return 0;
}
To me this looks like an error with the way the headers are set up, but perhaps I didn't set something up correctly?
Currently the command I'm using is as follows:
g++ -std=c++11 -g -I C:\\Users\\Logan\\Projects\\Code\\C++\\Includes\\opencv\\build\\include -o C:\\Users\\Logan\\Projects\\Code\\C++\\Bins\\<File name I'm using in the editor>.exe <File I'm using in the editor>

FI_Init_FreeType variable declaration error

I am trying to write a simple program that uses freetype.
code :
#include <ft2build.h>
#include FT_FREETYPE_H
int main()
{
FT_Library library;
int error = FI_Init_FreeType(&library);
}
i am compiling as g++ try1_c++demo.cpp -I/usr/include/freetype2
it gives following error:
try1_c++demo.cpp: In function ‘int main()’:
try1_c++demo.cpp:9: error: ‘FI_Init_FreeType’ was not declared in this scope
how do i solve this error?

C++ Missing Initializer error

I am getting this error during compile time (g++ 4.4.6):
main.cpp: In function ‘int main()’:
main.cpp:27: error: expected initializer before ‘:’ token
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘;’ before ‘for’
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘)’ before ‘for’
main.cpp:33: error: expected initializer before ‘:’ token
main.cpp:36: error: could not convert ‘((list != 0u) ? (list->SortedList::~SortedList(), operator delete(((void*)list))) : 0)’ to ‘bool’
main.cpp:37: error: expected primary-expression before ‘return’
main.cpp:37: error: expected ‘)’ before ‘return’
My code is as follows:
#include <iostream>
#include "Student.h"
#include "SortedList.h"
using namespace std;
int main() {
SortedList *list = new SortedList();
Student create[100];
int num = 100000;
for (Student &x : create) { // <--Line 27
x = new Student(num);
num += 10;
}
for (Student &x : create)
list->insert(&x);
delete list;
return 0;
}
Anybody who possibly knows the source of the error would be of great help. Also, Student and SortedList are objects which are declared in their .h files.
According to this page on GCC's website, range-based for is only available in g++ 4.6 and up, so you'll have to convert your code to a normal for loop or use std::for_each or something, or upgrade your compiler.

c++ compilation : what did i do wrong

I'm new to c++, and I've started a project for my internship where I have use to the Snap library from stanford (http://snap.stanford.edu/). So I have downloaded the library and I am now trying to create my own little programm using it. Sadly i can't seem to be able to compile it :(
Here are the sources :
Makefile :
CXXFLAGS += -std=c++98 -Wall
LDFLAGS += -lrt
Snap.o :
g++ -c $(CXXFLAGS) ../snap/snap/Snap.cpp -I../snap/glib -I../snap/snap -pg
simulation.o : simulation.cpp simulation.h
g++ -g -c $(CXXFLAGS) simulation.cpp
test.o : test.cpp
g++ -g -c $(CXXFLAGS) test.cpp
test : test.o Snap.o simulation.o
g++ -g $(LDFLAGS) test.o Snap.o simulation.o -I../snap/glib -I../snap/snap -lm -o test
simulation.h
#ifndef SIMULATION
#define SIMULATION
#include <vector>
#include "../snap/snap/Snap.h"
class Simulation{
public:
Simulation():score(-1),nNodes(-1),nEdges(-1), dMax(-1){};
Simulation(int nN, int nE, int d);
Simulation(int d, PUNGraph g);
void setDMax(int d){ dMax = d; }
double getScore(){ return score; }
int getNNodes(){ return nNodes; }
int getNEdges(){ return nEdges; }
int getDMax(){ return dMax; }
PUNGraph getGraph(){ return graph; }
std::vector<int> getAlignment(){ return alignment; }
double computeEnergy();
private:
double score;
int nNodes;
int nEdges;
int dMax;
PUNGraph graph;
std::vector<int> alignment;
};
#endif
simulation.cpp
#include "simulation.h"
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include "../snap/snap/Snap.h"
Simulation::Simulation(int nN, int nE, int d){
nNodes = nNodes;
nEdges = nEdges;
dMax = dMax;
graph = TSnap::GenRdnGnm<PUNGraph>(nNodes,nEdges);
for(int i=1; i<=nNodes; i++){
alignment.push_back(i);
}
random_shuffle(alignment.begin(),alignment.begin()+nNodes);
computeEnergy();
}
Simulation::Simulation(int d, PUNGraph g){
nNodes = graph->GetNodes();
nEdges = graph->GetEdges();
dMax = d;
graph = g;
for(int i=1; i<=nNodes; i++){
alignment.push_back(i);
}
random_shuffle(alignment.begin(),alignment.begin()+nNodes);
computeEnergy();
}
double computeEnergy(){
return 0.0;
}
test.cpp
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include "simulation.h"
#include "../snap/snap/Snap.h"
int main(int argc, char** argv){
Simulation sim(5000,30000,30);
}
I don't think my compilation problems come from Snap itself and it might very well be only from my poor knowledge of c++ and how the includes an so on are working.
Here is what I get after running make :
g++ -g -c -std=c++98 -Wall simulation.cpp
In file included from /usr/include/c++/4.5/bits/stl_algo.h:61:0,
from /usr/include/c++/4.5/algorithm:63,
from simulation.cpp:5:
/usr/include/c++/4.5/bits/algorithmfwd.h:347:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:358:41: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:347:5: error: template declaration of ‘const _Tp& std::max’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:358:5: error: template declaration of ‘const _Tp& std::min’
In file included from /usr/include/c++/4.5/algorithm:63:0,
from simulation.cpp:5:
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance)’:
/usr/include/c++/4.5/bits/stl_algo.h:3172:26: error: expected unqualified-id before ‘(’ token
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance, _Compare)’:
/usr/include/c++/4.5/bits/stl_algo.h:3202:26: error: expected unqualified-id before ‘(’ token
simulation.cpp: In constructor ‘Simulation::Simulation(int, int, int)’:
simulation.cpp:11:13: error: ‘GenRdnGnm’ is not a member of ‘TSnap’
simulation.cpp:11:38: error: expected primary-expression before ‘>’ token
simulation.cpp:11:47: warning: left-hand operand of comma has no effect
I'd be very glad if some one could help resolve my problems (and if you feel like giving some c++/programming wisdom in the process i'd be even happier :) )
Ortholle
The Snap library headers contain the unfortunate macro definitions:
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
This will cause problems with code that uses (or defines) std::min and std::max.
You can get around this by making sure to include STL headers before Snap, or possibly by adding
#undef min
#undef max
after including the Snap.h header.
Another problem with your code: What's with all those extraneous #includes? Example: Your test.cpp #includes a whole bunch of stuff it doesn't need. All that test.cpp needs is (or should need) simulation.h. simulation.cpp has a similar problem with far too many #includes.
Don't #include something in a file that isn't used in that file.
(Aside: that random_shuffle in simulation.cpp should be std::random_shuffle).
None of these fixes are going to help with the base problem, which is that the Snap library 'conveniently' defines max and min as macros. You don't need these, so undef them.

MYSQL headers conflict with STL <algorithm> in C++

// File test.cpp
#include <my_global.h>
#include <algorithm>
int main()
{
return 0;
}
Compiled with: g++ -c -I /usr/local/mysql/include/mysql/ test.cpp, where /usr/local/mysql is the mysql install directory.Then the compiler report the following errors:
In file included from /usr/include/c++/4.4/algorithm:61,
from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algobase.h:232:56: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.4/bits/stl_algobase.h:253:56: error: macro "max" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.4/bits/stl_algo.h:61,
from /usr/include/c++/4.4/algorithm:62,
from test.cpp:3:
/usr/include/c++/4.4/bits/algorithmfwd.h:353:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.4/bits/algorithmfwd.h:364:41: error: macro "min" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.4/algorithm:61,
from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: ‘std::min’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected primary-expression before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:237: error: expected unqualified-id before ‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: ‘max’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected primary-expression before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:258: error: expected unqualified-id before ‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:259: error: expected declaration before ‘}’ token
I think that there's some name conflict between my_global.h and algorithm, so I wrap my_global.h in a namespace:
// File test.cpp
namespace MYSQL_NAMESPACE {
#include <my_global.h>
}
#include <algorithm>
int main()
{
return 0;
}
But it doesn't help, the compiler still report the same errors. Then I change the include order as following:
// File test.cpp
#include <algorithm>
#include <my_global.h>
int main()
{
return 0;
}
Every thing goes well now.
Does Anybody Really Know What The Problem It Is?
TKS!
It seems that the mysql header defines a macro min.
#if !defined(max)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
This has been reported to MySQL as bug 28184. The bug is marked as closed, so try updating to the newest version. According to the bug page it should be fixed in version 5.1.23, version 6.0.4 and newer versions.
Apparently the namespace trick does not work because min/max are macros and the preprocessor does not look at namespace scope.
This might fix the problem:
#include <my_global.h>
#undef min
#undef max
#include <algorithm>
The whole thing looks horrible though :)
It looks like my_global.h defines some name used by algorithm as a preprocessor macro, causing compilation to fail. With the ordering that works, you won't be able to use whatever it is that my_global.h clobbers, but your code will at least compile unless you need that feature. Since preprocessor macros are not namespaced, the namespace wrapping will not help, as you have observed.
Therefore, it sounds like my_global.h is broken, but if everything works just use the include order that works and go with it. That's my preferred include order anyway - standard library headers first, followed by external library headers, followed by internal headers.